1
- from typing import List
1
+ from datetime import datetime
2
+ from typing import List , Optional , Any
3
+
2
4
from ..models .Task import Task
3
5
from ..models .TaskSet import TaskSet
6
+ from ..models .actions import Action
4
7
5
8
6
9
def task_all (tasks : List [Task ]):
@@ -16,31 +19,57 @@ def task_all(tasks: List[Task]):
16
19
TaskSet
17
20
A Durable Task Set that reports the state of running all of the tasks within it.
18
21
"""
19
- all_actions = []
20
- results = []
22
+ # Args for constructing the output TaskSet
23
+ is_played = True
24
+ is_faulted = False
21
25
is_completed = True
22
- complete_time = None
23
- faulted = []
26
+
27
+ actions : List [Action ] = []
28
+ results : List [Any ] = []
29
+
30
+ exception : Optional [str ] = None
31
+ end_time : Optional [datetime ] = None
32
+
24
33
for task in tasks :
34
+ # Add actions and results
25
35
if isinstance (task , TaskSet ):
26
- for action in task .actions :
27
- all_actions .append (action )
36
+ actions .extend (task .actions )
28
37
else :
29
- all_actions .append (task .action )
38
+ # We know it's an atomic Task
39
+ actions .append (task .action )
30
40
results .append (task .result )
31
41
32
- if task .is_faulted :
33
- faulted .append (task .exception )
42
+ # Record first exception, if it exists
43
+ if task .is_faulted and not is_faulted :
44
+ is_faulted = True
45
+ exception = task .exception
34
46
47
+ # If any task is not played, TaskSet is not played
48
+ if not task ._is_played :
49
+ is_played = False
50
+
51
+ # If any task is incomplete, TaskSet is incomplete
52
+ # If the task is complete, we can update the end_time
35
53
if not task .is_completed :
36
54
is_completed = False
55
+ elif end_time is None :
56
+ end_time = task .timestamp
37
57
else :
38
- complete_time = task .timestamp if complete_time is None \
39
- else max ([task .timestamp , complete_time ])
40
-
41
- if len (faulted ) > 0 :
42
- return TaskSet (is_completed , all_actions , results , is_faulted = True , exception = faulted [0 ])
43
- if is_completed :
44
- return TaskSet (is_completed , all_actions , results , False , complete_time )
45
- else :
46
- return TaskSet (is_completed , all_actions , None )
58
+ end_time = max ([task .timestamp , end_time ])
59
+
60
+ # Incomplete TaskSets do not have results or end-time
61
+ if not is_completed :
62
+ results = None
63
+ end_time = None
64
+
65
+ # Construct TaskSet
66
+ taskset = TaskSet (
67
+ is_completed = is_completed ,
68
+ actions = actions ,
69
+ result = results ,
70
+ is_faulted = is_faulted ,
71
+ timestamp = end_time ,
72
+ exception = exception ,
73
+ is_played = is_played
74
+ )
75
+ return taskset
0 commit comments