@@ -49,57 +49,66 @@ def handle(self, context: DurableOrchestrationContext):
49
49
suspended = False
50
50
51
51
fn_output = self .fn (self .durable_context )
52
+
52
53
# If `fn_output` is not an Iterator, then the orchestrator
53
54
# function does not make use of its context parameter. If so,
54
55
# `fn_output` is the return value instead of a generator
55
- if isinstance (fn_output , Iterator ):
56
- self .generator = fn_output
57
-
58
- else :
56
+ if not isinstance (fn_output , Iterator ):
59
57
orchestration_state = OrchestratorState (
60
58
is_done = True ,
61
59
output = fn_output ,
62
60
actions = self .durable_context .actions ,
63
61
custom_status = self .durable_context .custom_status )
64
- return orchestration_state .to_json_string ()
65
- try :
66
- generation_state = self ._generate_next (None )
67
-
68
- while not suspended :
69
- self ._add_to_actions (generation_state )
70
-
71
- if should_suspend (generation_state ):
72
- orchestration_state = OrchestratorState (
73
- is_done = False ,
74
- output = None ,
75
- actions = self .durable_context .actions ,
76
- custom_status = self .durable_context .custom_status )
77
- suspended = True
78
- continue
79
-
80
- if (isinstance (generation_state , Task )
81
- or isinstance (generation_state , TaskSet )) and (
82
- generation_state .is_faulted ):
83
- generation_state = self .generator .throw (
84
- generation_state .exception )
85
- continue
86
-
87
- self ._reset_timestamp ()
88
- generation_state = self ._generate_next (generation_state )
89
-
90
- except StopIteration as sie :
91
- orchestration_state = OrchestratorState (
92
- is_done = True ,
93
- output = sie .value ,
94
- actions = self .durable_context .actions ,
95
- custom_status = self .durable_context .custom_status )
96
- except Exception as e :
97
- orchestration_state = OrchestratorState (
98
- is_done = False ,
99
- output = None , # Should have no output, after generation range
100
- actions = self .durable_context .actions ,
101
- error = str (e ),
102
- custom_status = self .durable_context .custom_status )
62
+
63
+ else :
64
+ self .generator = fn_output
65
+ try :
66
+ generation_state = self ._generate_next (None )
67
+
68
+ while not suspended :
69
+ self ._add_to_actions (generation_state )
70
+
71
+ if should_suspend (generation_state ):
72
+
73
+ # The `is_done` field should be False here unless
74
+ # `continue_as_new` was called. Therefore,
75
+ # `will_continue_as_new` essentially "tracks"
76
+ # whether or not the orchestration is done.
77
+ orchestration_state = OrchestratorState (
78
+ is_done = self .durable_context .will_continue_as_new ,
79
+ output = None ,
80
+ actions = self .durable_context .actions ,
81
+ custom_status = self .durable_context .custom_status )
82
+ suspended = True
83
+ continue
84
+
85
+ if (isinstance (generation_state , Task )
86
+ or isinstance (generation_state , TaskSet )) and (
87
+ generation_state .is_faulted ):
88
+ generation_state = self .generator .throw (
89
+ generation_state .exception )
90
+ continue
91
+
92
+ self ._reset_timestamp ()
93
+ generation_state = self ._generate_next (generation_state )
94
+
95
+ except StopIteration as sie :
96
+ orchestration_state = OrchestratorState (
97
+ is_done = True ,
98
+ output = sie .value ,
99
+ actions = self .durable_context .actions ,
100
+ custom_status = self .durable_context .custom_status )
101
+ except Exception as e :
102
+ orchestration_state = OrchestratorState (
103
+ is_done = False ,
104
+ output = None , # Should have no output, after generation range
105
+ actions = self .durable_context .actions ,
106
+ error = str (e ),
107
+ custom_status = self .durable_context .custom_status )
108
+
109
+ # No output if continue_as_new was called
110
+ if self .durable_context .will_continue_as_new :
111
+ orchestration_state ._output = None
103
112
104
113
return orchestration_state .to_json_string ()
105
114
@@ -108,9 +117,13 @@ def _generate_next(self, partial_result):
108
117
gen_result = self .generator .send (partial_result .result )
109
118
else :
110
119
gen_result = self .generator .send (None )
120
+
111
121
return gen_result
112
122
113
123
def _add_to_actions (self , generation_state ):
124
+ # Do not add new tasks to action if continue_as_new was called
125
+ if self .durable_context .will_continue_as_new :
126
+ return
114
127
if (isinstance (generation_state , Task )
115
128
and hasattr (generation_state , "action" )):
116
129
self .durable_context .actions .append ([generation_state .action ])
0 commit comments