-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
task_runner.py
1038 lines (884 loc) · 39.2 KB
/
task_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
import copy
import itertools
import threading
from functools import partial, wraps
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
List,
NamedTuple,
Optional,
Set,
Sized,
Tuple,
Union,
)
import pendulum
import prefect
from prefect import config
from prefect.core import Edge, Task
from prefect.engine import signals
from prefect.engine.result import NoResult, Result
from prefect.engine.result_handlers import JSONResultHandler, ResultHandler
from prefect.engine.runner import ENDRUN, Runner, call_state_handlers
from prefect.engine.state import (
Cached,
Cancelled,
Failed,
Looped,
Mapped,
Paused,
Pending,
Resume,
Retrying,
Running,
Scheduled,
Skipped,
State,
Submitted,
Success,
TimedOut,
TriggerFailed,
)
from prefect.utilities.executors import run_with_heartbeat
if TYPE_CHECKING:
from prefect.engine.result_handlers import ResultHandler
TaskRunnerInitializeResult = NamedTuple(
"TaskRunnerInitializeResult", [("state", State), ("context", Dict[str, Any])]
)
class TaskRunner(Runner):
"""
TaskRunners handle the execution of Tasks and determine the State of a Task
before, during and after the Task is run.
In particular, through the TaskRunner you can specify the states of any upstream dependencies
and what state the Task should be initialized with.
Args:
- task (Task): the Task to be run / executed
- state_handlers (Iterable[Callable], optional): A list of state change handlers
that will be called whenever the task changes state, providing an
opportunity to inspect or modify the new state. The handler
will be passed the task runner instance, the old (prior) state, and the new
(current) state, with the following signature: `state_handler(TaskRunner, old_state, new_state) -> Optional[State]`;
If multiple functions are passed, then the `new_state` argument will be the
result of the previous handler.
- result_handler (ResultHandler, optional): the handler to use for
retrieving and storing state results during execution (if the Task doesn't already have one);
if not provided here or by the Task, will default to the one specified in your config
"""
def __init__(
self,
task: Task,
state_handlers: Iterable[Callable] = None,
result_handler: "ResultHandler" = None,
):
self.context = prefect.context.to_dict()
self.task = task
self.result_handler = (
task.result_handler
or result_handler
or prefect.engine.get_default_result_handler_class()()
)
super().__init__(state_handlers=state_handlers)
def __repr__(self) -> str:
return "<{}: {}>".format(type(self).__name__, self.task.name)
def call_runner_target_handlers(self, old_state: State, new_state: State) -> State:
"""
A special state handler that the TaskRunner uses to call its task's state handlers.
This method is called as part of the base Runner's `handle_state_change()` method.
Args:
- old_state (State): the old (previous) state
- new_state (State): the new (current) state
Returns:
- State: the new state
"""
self.logger.debug(
"Task '{name}': Handling state change from {old} to {new}".format(
name=prefect.context.get("task_full_name", self.task.name),
old=type(old_state).__name__,
new=type(new_state).__name__,
)
)
for handler in self.task.state_handlers:
new_state = handler(self.task, old_state, new_state) or new_state
return new_state
def initialize_run( # type: ignore
self, state: Optional[State], context: Dict[str, Any]
) -> TaskRunnerInitializeResult:
"""
Initializes the Task run by initializing state and context appropriately.
If the task is being retried, then we retrieve the run count from the initial Retry
state. Otherwise, we assume the run count is 1. The run count is stored in context as
task_run_count.
Also, if the task is being resumed through a `Resume` state, updates context to have `resume=True`.
Args:
- state (Optional[State]): the initial state of the run
- context (Dict[str, Any]): the context to be updated with relevant information
Returns:
- tuple: a tuple of the updated state, context, upstream_states, and inputs objects
"""
state, context = super().initialize_run(state=state, context=context)
if isinstance(state, Retrying):
run_count = state.run_count + 1
else:
run_count = state.context.get("task_run_count", 1)
if isinstance(state, Resume):
context.update(resume=True)
if hasattr(state, "cached_inputs"):
if "_loop_count" in (state.cached_inputs or {}): # type: ignore
loop_context = {
"task_loop_count": state.cached_inputs.pop( # type: ignore
"_loop_count"
) # type: ignore
.to_result()
.value,
"task_loop_result": state.cached_inputs.pop( # type: ignore
"_loop_result"
) # type: ignore
.to_result()
.value,
}
context.update(loop_context)
context.update(
task_run_count=run_count,
task_name=self.task.name,
task_tags=self.task.tags,
task_slug=self.task.slug,
)
context.setdefault("checkpointing", config.flows.checkpointing)
context.update(logger=self.task.logger)
return TaskRunnerInitializeResult(state=state, context=context)
def run(
self,
state: State = None,
upstream_states: Dict[Edge, State] = None,
context: Dict[str, Any] = None,
executor: "prefect.engine.executors.Executor" = None,
) -> State:
"""
The main endpoint for TaskRunners. Calling this method will conditionally execute
`self.task.run` with any provided inputs, assuming the upstream dependencies are in a
state which allow this Task to run.
Args:
- state (State, optional): initial `State` to begin task run from;
defaults to `Pending()`
- upstream_states (Dict[Edge, State]): a dictionary
representing the states of any tasks upstream of this one. The keys of the
dictionary should correspond to the edges leading to the task.
- context (dict, optional): prefect Context to use for execution
- executor (Executor, optional): executor to use when performing
computation; defaults to the executor specified in your prefect configuration
Returns:
- `State` object representing the final post-run state of the Task
"""
upstream_states = upstream_states or {}
context = context or {}
map_index = context.setdefault("map_index", None)
context["task_full_name"] = "{name}{index}".format(
name=self.task.name,
index=("" if map_index is None else "[{}]".format(map_index)),
)
if executor is None:
executor = prefect.engine.get_default_executor_class()()
# if mapped is true, this task run is going to generate a Mapped state. It won't
# actually run, but rather spawn children tasks to map over its inputs. We
# detect this case by checking for:
# - upstream edges that are `mapped`
# - no `map_index` (which indicates that this is the child task, not the parent)
mapped = any([e.mapped for e in upstream_states]) and map_index is None
task_inputs = {} # type: Dict[str, Any]
try:
# initialize the run
state, context = self.initialize_run(state, context)
# run state transformation pipeline
with prefect.context(context):
if prefect.context.get("task_loop_count") is None:
self.logger.info(
"Task '{name}': Starting task run...".format(
name=context["task_full_name"]
)
)
# check to make sure the task is in a pending state
state = self.check_task_is_ready(state)
# check if the task has reached its scheduled time
state = self.check_task_reached_start_time(state)
# Tasks never run if the upstream tasks haven't finished
state = self.check_upstream_finished(
state, upstream_states=upstream_states
)
# check if any upstream tasks skipped (and if we need to skip)
state = self.check_upstream_skipped(
state, upstream_states=upstream_states
)
# if the task is mapped, process the mapped children and exit
if mapped:
state = self.run_mapped_task(
state=state,
upstream_states=upstream_states,
context=context,
executor=executor,
)
state = self.wait_for_mapped_task(state=state, executor=executor)
self.logger.debug(
"Task '{name}': task has been mapped; ending run.".format(
name=context["task_full_name"]
)
)
raise ENDRUN(state)
# retrieve task inputs from upstream and also explicitly passed inputs
task_inputs = self.get_task_inputs(
state=state, upstream_states=upstream_states
)
# check to see if the task has a cached result
state = self.check_task_is_cached(state, inputs=task_inputs)
# check if the task's trigger passes
# triggers can raise Pauses, which require task_inputs to be available for caching
# so we run this after the previous step
state = self.check_task_trigger(state, upstream_states=upstream_states)
# set the task state to running
state = self.set_task_to_running(state)
# run the task
state = self.get_task_run_state(
state, inputs=task_inputs, timeout_handler=executor.timeout_handler
)
# cache the output, if appropriate
state = self.cache_result(state, inputs=task_inputs)
# check if the task needs to be retried
state = self.check_for_retry(state, inputs=task_inputs)
state = self.check_task_is_looping(
state,
inputs=task_inputs,
upstream_states=upstream_states,
context=context,
executor=executor,
)
# for pending signals, including retries and pauses we need to make sure the
# task_inputs are set
except (ENDRUN, signals.PrefectStateSignal) as exc:
if exc.state.is_pending() or exc.state.is_failed():
exc.state.cached_inputs = task_inputs or {} # type: ignore
state = exc.state
except Exception as exc:
msg = "Task '{name}': unexpected error while running task: {exc}".format(
name=context["task_full_name"], exc=repr(exc)
)
self.logger.exception(msg)
state = Failed(message=msg, result=exc)
if prefect.context.get("raise_on_exception"):
raise exc
# to prevent excessive repetition of this log
# since looping relies on recursively calling self.run
# TODO: figure out a way to only log this one single time instead of twice
if prefect.context.get("task_loop_count") is None:
# wrapping this final log in prefect.context(context) ensures
# that any run-context, including task-run-ids, are respected
with prefect.context(context):
self.logger.info(
"Task '{name}': finished task run for task with final state: '{state}'".format(
name=context["task_full_name"], state=type(state).__name__
)
)
return state
@call_state_handlers
def check_upstream_finished(
self, state: State, upstream_states: Dict[Edge, State]
) -> State:
"""
Checks if the upstream tasks have all finshed.
Args:
- state (State): the current state of this task
- upstream_states (Dict[Edge, Union[State, List[State]]]): the upstream states
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if upstream tasks are not finished.
"""
all_states = set() # type: Set[State]
for edge, upstream_state in upstream_states.items():
# if the upstream state is Mapped, and this task is also mapped,
# we want each individual child to determine if it should
# proceed or not based on its upstream parent in the mapping
if isinstance(upstream_state, Mapped) and not edge.mapped:
all_states.update(upstream_state.map_states)
else:
all_states.add(upstream_state)
if not all(s.is_finished() for s in all_states):
self.logger.debug(
"Task '{name}': not all upstream states are finished; ending run.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
return state
@call_state_handlers
def check_upstream_skipped(
self, state: State, upstream_states: Dict[Edge, State]
) -> State:
"""
Checks if any of the upstream tasks have skipped.
Args:
- state (State): the current state of this task
- upstream_states (Dict[Edge, State]): the upstream states
Returns:
- State: the state of the task after running the check
"""
all_states = set() # type: Set[State]
for edge, upstream_state in upstream_states.items():
# if the upstream state is Mapped, and this task is also mapped,
# we want each individual child to determine if it should
# skip or not based on its upstream parent in the mapping
if isinstance(upstream_state, Mapped) and not edge.mapped:
all_states.update(upstream_state.map_states)
else:
all_states.add(upstream_state)
if self.task.skip_on_upstream_skip and any(s.is_skipped() for s in all_states):
self.logger.debug(
"Task '{name}': Upstream states were skipped; ending run.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(
state=Skipped(
message=(
"Upstream task was skipped; if this was not the intended "
"behavior, consider changing `skip_on_upstream_skip=False` "
"for this task."
)
)
)
return state
@call_state_handlers
def check_task_trigger(
self, state: State, upstream_states: Dict[Edge, State]
) -> State:
"""
Checks if the task's trigger function passes.
Args:
- state (State): the current state of this task
- upstream_states (Dict[Edge, Union[State, List[State]]]): the upstream states
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if the trigger raises an error
"""
all_states = set() # type: Set[State]
for upstream_state in upstream_states.values():
if isinstance(upstream_state, Mapped):
all_states.update(upstream_state.map_states)
else:
all_states.add(upstream_state)
try:
if not self.task.trigger(all_states):
raise signals.TRIGGERFAIL(message="Trigger failed")
except signals.PrefectStateSignal as exc:
self.logger.debug(
"Task '{name}': {signal} signal raised during execution.".format(
name=prefect.context.get("task_full_name", self.task.name),
signal=type(exc).__name__,
)
)
if prefect.context.get("raise_on_exception"):
raise exc
raise ENDRUN(exc.state)
# Exceptions are trapped and turned into TriggerFailed states
except Exception as exc:
self.logger.exception(
"Task '{name}': unexpected error while evaluating task trigger: {exc}".format(
exc=repr(exc),
name=prefect.context.get("task_full_name", self.task.name),
)
)
if prefect.context.get("raise_on_exception"):
raise exc
raise ENDRUN(
TriggerFailed(
"Unexpected error while checking task trigger: {}".format(
repr(exc)
),
result=exc,
)
)
return state
@call_state_handlers
def check_task_is_ready(self, state: State) -> State:
"""
Checks to make sure the task is ready to run (Pending or Mapped).
Args:
- state (State): the current state of this task
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if the task is not ready to run
"""
# the task is ready
if state.is_pending():
return state
# the task is mapped, in which case we still proceed so that the children tasks
# are generated (note that if the children tasks)
elif state.is_mapped():
self.logger.debug(
"Task '{name}': task is mapped, but run will proceed so children are generated.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
return state
# this task is already running
elif state.is_running():
self.logger.debug(
"Task '{name}': task is already running.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
elif state.is_cached():
return state
# this task is already finished
elif state.is_finished():
self.logger.debug(
"Task '{name}': task is already finished.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
# this task is not pending
else:
self.logger.debug(
"Task '{name}' is not ready to run or state was unrecognized ({state}).".format(
name=prefect.context.get("task_full_name", self.task.name),
state=state,
)
)
raise ENDRUN(state)
@call_state_handlers
def check_task_reached_start_time(self, state: State) -> State:
"""
Checks if a task is in a Scheduled state and, if it is, ensures that the scheduled
time has been reached. Note: Scheduled states include Retry states.
Args:
- state (State): the current state of this task
Returns:
- State: the state of the task after performing the check
Raises:
- ENDRUN: if the task is Scheduled with a future scheduled time
"""
if isinstance(state, Scheduled):
if state.start_time and state.start_time > pendulum.now("utc"):
self.logger.debug(
"Task '{name}': start_time has not been reached; ending run.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
return state
def get_task_inputs(
self, state: State, upstream_states: Dict[Edge, State]
) -> Dict[str, Result]:
"""
Given the task's current state and upstream states, generates the inputs for this task.
Upstream state result values are used. If the current state has `cached_inputs`, they
will override any upstream values which are `NoResult`.
Args:
- state (State): the task's current state.
- upstream_states (Dict[Edge, State]): the upstream state_handlers
Returns:
- Dict[str, Result]: the task inputs
"""
task_inputs = {} # type: Dict[str, Result]
handlers = {} # type: Dict[str, ResultHandler]
for edge, upstream_state in upstream_states.items():
# construct task inputs
if edge.key is not None:
handlers[edge.key] = handler = getattr(
edge.upstream_task, "result_handler", None
)
task_inputs[ # type: ignore
edge.key
] = upstream_state._result.to_result( # type: ignore
handler
) # type: ignore
if state.is_pending() and state.cached_inputs is not None: # type: ignore
task_inputs.update(
{
k: r.to_result(handlers.get(k))
for k, r in state.cached_inputs.items() # type: ignore
if task_inputs.get(k, NoResult) == NoResult
}
)
return task_inputs
@call_state_handlers
def check_task_is_cached(self, state: State, inputs: Dict[str, Result]) -> State:
"""
Checks if task is cached and whether the cache is still valid.
Args:
- state (State): the current state of this task
- inputs (Dict[str, Result]): a dictionary of inputs whose keys correspond
to the task's `run()` arguments.
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if the task is not ready to run
"""
if state.is_cached():
assert isinstance(state, Cached) # mypy assert
sanitized_inputs = {key: res.value for key, res in inputs.items()}
if self.task.cache_validator(
state, sanitized_inputs, prefect.context.get("parameters")
):
state._result = state._result.to_result(self.task.result_handler)
return state
else:
state = Pending("Cache was invalid; ready to run.")
if self.task.cache_for is not None:
candidate_states = prefect.context.caches.get(
self.task.cache_key or self.task.name, []
)
sanitized_inputs = {key: res.value for key, res in inputs.items()}
for candidate in candidate_states:
if self.task.cache_validator(
candidate, sanitized_inputs, prefect.context.get("parameters")
):
candidate._result = candidate._result.to_result(
self.task.result_handler
)
return candidate
if self.task.cache_for is not None:
self.logger.warning(
"Task '{name}': can't use cache because it "
"is now invalid".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
return state or Pending("Cache was invalid; ready to run.")
def run_mapped_task(
self,
state: State,
upstream_states: Dict[Edge, State],
context: Dict[str, Any],
executor: "prefect.engine.executors.Executor",
) -> State:
"""
If the task is being mapped, submits children tasks for execution. Returns a `Mapped` state.
Args:
- state (State): the current task state
- upstream_states (Dict[Edge, State]): the upstream states
- context (dict, optional): prefect Context to use for execution
- executor (Executor): executor to use when performing computation
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if the current state is not `Running`
"""
map_upstream_states = []
# we don't know how long the iterables are, but we want to iterate until we reach
# the end of the shortest one
counter = itertools.count()
# infinite loop, if upstream_states has any entries
while True and upstream_states:
i = next(counter)
states = {}
try:
for edge, upstream_state in upstream_states.items():
# if the edge is not mapped over, then we simply take its state
if not edge.mapped:
states[edge] = upstream_state
# if the edge is mapped and the upstream state is Mapped, then we are mapping
# over a mapped task. In this case, we take the appropriately-indexed upstream
# state from the upstream tasks's `Mapped.map_states` array.
# Note that these "states" might actually be futures at this time; we aren't
# blocking until they finish.
elif edge.mapped and upstream_state.is_mapped():
states[edge] = upstream_state.map_states[i] # type: ignore
# Otherwise, we are mapping over the result of a "vanilla" task. In this
# case, we create a copy of the upstream state but set the result to the
# appropriately-indexed item from the upstream task's `State.result`
# array.
else:
states[edge] = copy.copy(upstream_state)
# if the current state is already Mapped, then we might be executing
# a re-run of the mapping pipeline. In that case, the upstream states
# might not have `result` attributes (as any required results could be
# in the `cached_inputs` attribute of one of the child states).
# Therefore, we only try to get a result if EITHER this task's
# state is not already mapped OR the upstream result is not None.
if not state.is_mapped() or upstream_state.result != NoResult:
upstream_result = Result(
upstream_state.result[i],
result_handler=upstream_state._result.result_handler, # type: ignore
)
states[edge].result = upstream_result
elif state.is_mapped():
if i >= len(state.map_states): # type: ignore
raise IndexError()
# only add this iteration if we made it through all iterables
map_upstream_states.append(states)
# index error means we reached the end of the shortest iterable
except IndexError:
break
def run_fn(
state: State, map_index: int, upstream_states: Dict[Edge, State]
) -> State:
map_context = context.copy()
map_context.update(map_index=map_index)
with prefect.context(self.context):
return self.run(
upstream_states=upstream_states,
# if we set the state here, then it will not be processed by `initialize_run()`
state=state,
context=map_context,
executor=executor,
)
# generate initial states, if available
if isinstance(state, Mapped):
initial_states = list(state.map_states) # type: List[Optional[State]]
else:
initial_states = []
initial_states.extend([None] * (len(map_upstream_states) - len(initial_states)))
current_state = Mapped(
message="Preparing to submit {} mapped tasks.".format(len(initial_states)),
map_states=initial_states, # type: ignore
)
state = self.handle_state_change(old_state=state, new_state=current_state)
if state is not current_state:
return state
# map over the initial states, a counter representing the map_index, and also the mapped upstream states
map_states = executor.map(
run_fn, initial_states, range(len(map_upstream_states)), map_upstream_states
)
self.logger.debug(
"{} mapped tasks submitted for execution.".format(len(map_states))
)
new_state = Mapped(
message="Mapped tasks submitted for execution.", map_states=map_states
)
return self.handle_state_change(old_state=state, new_state=new_state)
@call_state_handlers
def wait_for_mapped_task(
self, state: State, executor: "prefect.engine.executors.Executor"
) -> State:
"""
Blocks until a mapped state's children have finished running.
Args:
- state (State): the current `Mapped` state
- executor (Executor): the run's executor
Returns:
- State: the new state
"""
if state.is_mapped():
assert isinstance(state, Mapped) # mypy assert
state.map_states = executor.wait(state.map_states)
return state
@call_state_handlers
def set_task_to_running(self, state: State) -> State:
"""
Sets the task to running
Args:
- state (State): the current state of this task
Returns:
- State: the state of the task after running the check
Raises:
- ENDRUN: if the task is not ready to run
"""
if not state.is_pending():
self.logger.debug(
"Task '{name}': can't set state to Running because it "
"isn't Pending; ending run.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
new_state = Running(message="Starting task run.")
return new_state
@run_with_heartbeat
@call_state_handlers
def get_task_run_state(
self,
state: State,
inputs: Dict[str, Result],
timeout_handler: Optional[Callable] = None,
) -> State:
"""
Runs the task and traps any signals or errors it raises.
Also checkpoints the result of a successful task, if `task.checkpoint` is `True`.
Args:
- state (State): the current state of this task
- inputs (Dict[str, Result], optional): a dictionary of inputs whose keys correspond
to the task's `run()` arguments.
- timeout_handler (Callable, optional): function for timing out
task execution, with call signature `handler(fn, *args, **kwargs)`. Defaults to
`prefect.utilities.executors.timeout_handler`
Returns:
- State: the state of the task after running the check
Raises:
- signals.PAUSE: if the task raises PAUSE
- ENDRUN: if the task is not ready to run
"""
if not state.is_running():
self.logger.debug(
"Task '{name}': can't run task because it's not in a "
"Running state; ending run.".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
raise ENDRUN(state)
try:
self.logger.debug(
"Task '{name}': Calling task.run() method...".format(
name=prefect.context.get("task_full_name", self.task.name)
)
)
timeout_handler = (
timeout_handler or prefect.utilities.executors.timeout_handler
)
raw_inputs = {k: r.value for k, r in inputs.items()}
result = timeout_handler(
self.task.run, timeout=self.task.timeout, **raw_inputs
)
except KeyboardInterrupt:
self.logger.exception("Interrupt signal raised, cancelling task run.")
state = Cancelled(message="Interrupt signal raised, cancelling task run.")
return state
# inform user of timeout
except TimeoutError as exc:
if prefect.context.get("raise_on_exception"):
raise exc
state = TimedOut(
"Task timed out during execution.", result=exc, cached_inputs=inputs
)
return state
except signals.LOOP as exc:
new_state = exc.state
assert isinstance(new_state, Looped)
new_state.result = Result(
value=new_state.result, result_handler=self.result_handler
)
new_state.message = exc.state.message or "Task is looping ({})".format(
new_state.loop_count
)
return new_state
result = Result(value=result, result_handler=self.result_handler)
state = Success(result=result, message="Task run succeeded.")
## only checkpoint tasks if checkpointing is turned on
if (
state.is_successful()
and prefect.context.get("checkpointing") is True
and self.task.checkpoint is True
):
state._result.store_safe_value()
return state
@call_state_handlers
def cache_result(self, state: State, inputs: Dict[str, Result]) -> State:
"""
Caches the result of a successful task, if appropriate. Alternatively,
if the task is failed, caches the inputs.
Tasks are cached if:
- task.cache_for is not None
- the task state is Successful
- the task state is not Skipped (which is a subclass of Successful)
Args:
- state (State): the current state of this task
- inputs (Dict[str, Result], optional): a dictionary of inputs whose keys correspond
to the task's `run()` arguments.
Returns:
- State: the state of the task after running the check
"""
if state.is_failed():
state.cached_inputs = inputs # type: ignore
if (
state.is_successful()
and not state.is_skipped()
and self.task.cache_for is not None
):
expiration = pendulum.now("utc") + self.task.cache_for
cached_state = Cached(
result=state._result,
cached_inputs=inputs,
cached_result_expiration=expiration,
cached_parameters=prefect.context.get("parameters"),
message=state.message,
)
return cached_state
return state
@call_state_handlers
def check_for_retry(self, state: State, inputs: Dict[str, Result]) -> State:
"""
Checks to see if a FAILED task should be retried.
Args:
- state (State): the current state of this task
- inputs (Dict[str, Result], optional): a dictionary of inputs whose keys correspond
to the task's `run()` arguments.
Returns:
- State: the state of the task after running the check
"""
if state.is_failed():
run_count = prefect.context.get("task_run_count", 1)
if prefect.context.get("task_loop_count") is not None:
loop_context = {
"_loop_count": Result(
value=prefect.context["task_loop_count"],
result_handler=JSONResultHandler(),
),
"_loop_result": Result(
value=prefect.context.get("task_loop_result"),
result_handler=self.result_handler,
),
}
inputs.update(loop_context)
if run_count <= self.task.max_retries:
start_time = pendulum.now("utc") + self.task.retry_delay
msg = "Retrying Task (after attempt {n} of {m})".format(
n=run_count, m=self.task.max_retries + 1
)
retry_state = Retrying(
start_time=start_time,
cached_inputs=inputs,
message=msg,
run_count=run_count,
)
return retry_state
return state
def check_task_is_looping(
self,
state: State,
inputs: Dict[str, Result] = None,
upstream_states: Dict[Edge, State] = None,
context: Dict[str, Any] = None,
executor: "prefect.engine.executors.Executor" = None,
) -> State: