From 2c639e54bc65ed5984c47bc4c73f68ba59825b69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Tue, 27 Feb 2024 17:39:58 +0100
Subject: [PATCH] TMSS-2934: allow workflow to finish on SU error/cancelled
 statuses

---
 .../services/workflow_service/lib/workflow_service.py     | 8 ++++----
 .../src/tmss/workflowapp/flows/schedulingunitflow.py      | 4 +++-
 SAS/TMSS/backend/src/tmss/workflowapp/signals/__init__.py | 2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/SAS/TMSS/backend/services/workflow_service/lib/workflow_service.py b/SAS/TMSS/backend/services/workflow_service/lib/workflow_service.py
index 9b62dfa8a65..eec9d50281b 100644
--- a/SAS/TMSS/backend/services/workflow_service/lib/workflow_service.py
+++ b/SAS/TMSS/backend/services/workflow_service/lib/workflow_service.py
@@ -32,7 +32,7 @@ class SchedulingUnitEventMessageHandler(TMSSEventMessageHandler):
     def onSchedulingUnitBlueprintStatusChanged(self, id: int, status: str):
         try:
             # import here and not at top of module because we need the django.setup() to be run first, either from this module's main, or from the TMSSTestEnvironment
-            from lofar.sas.tmss.tmss.workflowapp.signals import scheduling_unit_blueprint_schedulable_signal, scheduling_unit_blueprint_scheduled_signal, scheduling_unit_blueprint_observing_signal, scheduling_unit_blueprint_observed_signal, scheduling_unit_blueprint_ingested_signal, scheduling_unit_blueprint_finished_signal
+            from lofar.sas.tmss.tmss.workflowapp.signals import scheduling_unit_blueprint_schedulable_signal, scheduling_unit_blueprint_scheduled_signal, scheduling_unit_blueprint_observing_signal, scheduling_unit_blueprint_observed_signal, scheduling_unit_blueprint_ingested_signal, scheduling_unit_blueprint_done_signal
             from lofar.sas.tmss.tmss.workflowapp.models import SchedulingUnitTask
             from lofar.sas.tmss.tmss.tmssapp.models import SchedulingUnitBlueprint, SchedulingUnitStatus, TaskType, SubtaskType
 
@@ -53,9 +53,9 @@ class SchedulingUnitEventMessageHandler(TMSSEventMessageHandler):
             elif status == SchedulingUnitStatus.Choices.INGESTED.value:
                 logger.info("workflow scheduling_unit_blueprint id=%s is fully ingested, signalling workflow...", id)
                 scheduling_unit_blueprint_ingested_signal.send(sender=self.__class__, scheduling_unit_blueprint=scheduling_unit_blueprint)
-            elif status == SchedulingUnitStatus.Choices.FINISHED.value:
-                logger.info("workflow scheduling_unit_blueprint id=%s is fully finished, signalling workflow...", id)
-                scheduling_unit_blueprint_finished_signal.send(sender=self.__class__, scheduling_unit_blueprint=scheduling_unit_blueprint)
+            elif status in SchedulingUnitStatus.DONE_STATUS_VALUES:
+                logger.info("workflow scheduling_unit_blueprint id=%s is fully done, signalling workflow...", id)
+                scheduling_unit_blueprint_done_signal.send(sender=self.__class__, scheduling_unit_blueprint=scheduling_unit_blueprint)
         except Exception as e:
             logger.exception("Exception in workflow onSchedulingUnitBlueprintStatusChanged: %s" % (str(e),))
 
diff --git a/SAS/TMSS/backend/src/tmss/workflowapp/flows/schedulingunitflow.py b/SAS/TMSS/backend/src/tmss/workflowapp/flows/schedulingunitflow.py
index e51e1d8180d..e3973a04ce1 100644
--- a/SAS/TMSS/backend/src/tmss/workflowapp/flows/schedulingunitflow.py
+++ b/SAS/TMSS/backend/src/tmss/workflowapp/flows/schedulingunitflow.py
@@ -262,7 +262,7 @@ class SchedulingUnitFlow(Flow):
     wait_finished = (
         Conditional(
           this.is_SUB_finished_state_or_beyond,
-          scheduling_unit_blueprint_finished_signal,
+          scheduling_unit_blueprint_done_signal,
           task_loader=this.get_scheduling_unit_task
         )
         .Next(this.end)
@@ -356,6 +356,8 @@ class SchedulingUnitFlow(Flow):
             condition = scheduling_unit.status.value in SchedulingUnitStatus.ACTIVE_OR_FINISHED_STATUS_VALUES
         elif expected_status == SchedulingUnitStatus.Choices.SCHEDULABLE.value:
             condition = scheduling_unit.status.value in SchedulingUnitStatus.SCHEDULABLE_ACTIVE_OR_FINISHED_STATUS_VALUES
+        elif expected_status == SchedulingUnitStatus.Choices.FINISHED.value:
+            condition = scheduling_unit.status.value in SchedulingUnitStatus.DONE_STATUS_VALUES
 
         return condition
 
diff --git a/SAS/TMSS/backend/src/tmss/workflowapp/signals/__init__.py b/SAS/TMSS/backend/src/tmss/workflowapp/signals/__init__.py
index 5e9ac2c06c7..ac09eb418ff 100644
--- a/SAS/TMSS/backend/src/tmss/workflowapp/signals/__init__.py
+++ b/SAS/TMSS/backend/src/tmss/workflowapp/signals/__init__.py
@@ -5,6 +5,6 @@ scheduling_unit_blueprint_scheduled_signal = django.dispatch.Signal()
 scheduling_unit_blueprint_observing_signal = django.dispatch.Signal()
 scheduling_unit_blueprint_observed_signal = django.dispatch.Signal()
 scheduling_unit_blueprint_ingested_signal = django.dispatch.Signal()
-scheduling_unit_blueprint_finished_signal = django.dispatch.Signal()
+scheduling_unit_blueprint_done_signal = django.dispatch.Signal()
 
 
-- 
GitLab