@@ -77,25 +77,23 @@ def handle_message(self, msg: Message) -> None:
7777
7878 def _handle_workflow_message (self , msg : WorkflowMessage ) -> None :
7979 """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 ').
8181 The message contains a 'running_workflow' field that contains the UUID
8282 of an existing RunningWorkflow record in the DM. Using this
8383 we can locate the Workflow record and interrogate that to identify which
8484 step (or steps) to launch (run) first."""
8585 assert msg
8686
8787 _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
8991
9092 r_wfid = msg .running_workflow
9193 if msg .action == "START" :
9294 self ._handle_workflow_start_message (r_wfid )
9395 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 )
9997
10098 def _handle_workflow_start_message (self , r_wfid : str ) -> None :
10199 """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:
142140 # and error, stopping it. There will be no Pod event as the launch has failed.
143141 self ._launch (rwf = rwf_response , rwfs_id = r_wfsid , step = first_step )
144142
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+
145177 def _handle_pod_message (self , msg : PodMessage ) -> None :
146178 """Handles a PodMessage. This is a message that signals the completion of a
147179 prior step Job within an existing running workflow.
0 commit comments