@@ -77,25 +77,23 @@ def handle_message(self, msg: Message) -> None:
77
77
78
78
def _handle_workflow_message (self , msg : WorkflowMessage ) -> None :
79
79
"""WorkflowMessages signal the need to start (or stop) a workflow using its
80
- 'action' string field (one of 'START' or 'START ').
80
+ 'action' string field (one of 'START' or 'STOP ').
81
81
The message contains a 'running_workflow' field that contains the UUID
82
82
of an existing RunningWorkflow record in the DM. Using this
83
83
we can locate the Workflow record and interrogate that to identify which
84
84
step (or steps) to launch (run) first."""
85
85
assert msg
86
86
87
87
_LOGGER .info ("WorkflowMessage:\n %s" , str (msg ))
88
- assert msg .action in ["START" , "STOP" ]
88
+ if msg .action not in ["START" , "STOP" ]:
89
+ _LOGGER .error ("Ignoring unsupported action (%s)" , msg .action )
90
+ return
89
91
90
92
r_wfid = msg .running_workflow
91
93
if msg .action == "START" :
92
94
self ._handle_workflow_start_message (r_wfid )
93
95
else :
94
- # STOP is not implemented yet and probably not for some time.
95
- # So just log and ignore for now!
96
- _LOGGER .warning (
97
- "Got STOP action for %s - but it's not implemented yet!" , r_wfid
98
- )
96
+ self ._handle_workflow_stop_message (r_wfid )
99
97
100
98
def _handle_workflow_start_message (self , r_wfid : str ) -> None :
101
99
"""Logic to handle a START message. This is the beginning of a new
@@ -142,6 +140,40 @@ def _handle_workflow_start_message(self, r_wfid: str) -> None:
142
140
# and error, stopping it. There will be no Pod event as the launch has failed.
143
141
self ._launch (rwf = rwf_response , rwfs_id = r_wfsid , step = first_step )
144
142
143
+ def _handle_workflow_stop_message (self , r_wfid : str ) -> None :
144
+ """Logic to handle a STOP message."""
145
+ # Do nothing if the running workflow has already stopped.
146
+ rwf_response , _ = self ._wapi_adapter .get_running_workflow (
147
+ running_workflow_id = r_wfid
148
+ )
149
+ _LOGGER .debug (
150
+ "API.get_running_workflow(%s) returned: -\n %s" , r_wfid , str (rwf_response )
151
+ )
152
+ if not rwf_response :
153
+ _LOGGER .debug ("Running workflow does not exist (%s)" , r_wfid )
154
+ return
155
+ elif rwf_response ["done" ] is True :
156
+ _LOGGER .debug ("Running workflow already stopped (%s)" , r_wfid )
157
+ return
158
+
159
+ # For this version all we can do is check that no steps are running.
160
+ # If no steps are running we can safely mark the running workflow as stopped.
161
+ response , _ = self ._wapi_adapter .get_running_steps (running_workflow_id = r_wfid )
162
+ _LOGGER .debug (
163
+ "API.get_running_steps(%s) returned: -\n %s" , r_wfid , str (response )
164
+ )
165
+ if response :
166
+ if count := response ["count" ]:
167
+ msg : str = "1 step is" if count == 1 else f"{ count } steps are"
168
+ _LOGGER .debug ("Ignoring STOP for %s. %s still running" , r_wfid , msg )
169
+ else :
170
+ self ._wapi_adapter .set_running_workflow_done (
171
+ running_workflow_id = r_wfid ,
172
+ success = False ,
173
+ error_num = 1 ,
174
+ error_msg = "User stopped" ,
175
+ )
176
+
145
177
def _handle_pod_message (self , msg : PodMessage ) -> None :
146
178
"""Handles a PodMessage. This is a message that signals the completion of a
147
179
prior step Job within an existing running workflow.
0 commit comments