From 76334e7e5b02767a041919c6fed72f976c125907 Mon Sep 17 00:00:00 2001
From: Charlotte Hausman <chausman@nrao.edu>
Date: Fri, 19 Feb 2021 13:05:42 -0500
Subject: [PATCH] fix CapabilityEngine to actually load the capability
 execution it's executing and then properly release the engine after
 completion

---
 .../workspaces/test/test_capability_engine.py | 19 +++++++++++++++++++
 .../capability/services/capability_engine.py  | 19 +++++++++++++------
 .../capability/services/capability_queue.py   |  6 +++---
 .../capability/services/interfaces.py         |  5 ++++-
 4 files changed, 39 insertions(+), 10 deletions(-)
 create mode 100644 shared/workspaces/test/test_capability_engine.py

diff --git a/shared/workspaces/test/test_capability_engine.py b/shared/workspaces/test/test_capability_engine.py
new file mode 100644
index 000000000..59da7bcd9
--- /dev/null
+++ b/shared/workspaces/test/test_capability_engine.py
@@ -0,0 +1,19 @@
+from workspaces.capability.services.capability_info import CapabilityInfo
+from workspaces.capability.services.capability_engine import CapabilityEngine
+
+pytest_plugins = ["testing.utils.conftest"]
+
+"""
+Tests for CapabilityEngine
+"""
+
+def test_load_engine():
+    pass
+
+
+def test_execute():
+    pass
+
+
+def test_submit_workflow_request():
+    pass
diff --git a/shared/workspaces/workspaces/capability/services/capability_engine.py b/shared/workspaces/workspaces/capability/services/capability_engine.py
index a8a99f930..4aa361035 100644
--- a/shared/workspaces/workspaces/capability/services/capability_engine.py
+++ b/shared/workspaces/workspaces/capability/services/capability_engine.py
@@ -22,17 +22,24 @@ class CapabilityEngine(CapabilityEngineIF):
     ):
         self.capability_info = capability_info
         self.workflow_service = workflow_service
+        self.execution = None
 
-    def execute(self, execution: CapabilityExecutionIF):
+    def load_engine(self, execution: CapabilityExecutionIF):
         """
-        Communicate with workflow service and send a request to run a workflow with given settings
+        Load capability execution of interest into capability engine for access and execution
+        :param execution: execution to be executed by this engine
+        :return:
+        """
+        self.execution = execution
 
-        :param execution:
+    def execute(self):
+        """
+        Communicate with workflow service and send a request to run a workflow with given settings
         """
-        step_sequence = CapabilitySequence.from_str(execution.steps)
+        step_sequence = CapabilitySequence.from_str(self.execution.steps)
         # Grab value of current step (workflow name)
-        cur_step = step_sequence[int(execution.current_step)]
-        cur_step.execute(self, execution)
+        cur_step = step_sequence[int(self.execution.current_step)]
+        cur_step.execute(self, self.execution)
 
     def submit_workflow_request(
         self, execution_id: int, workflow_name: str, workflow_args: dict, files: List[AbstractFile]
diff --git a/shared/workspaces/workspaces/capability/services/capability_queue.py b/shared/workspaces/workspaces/capability/services/capability_queue.py
index cf3caa4a4..1762579f2 100644
--- a/shared/workspaces/workspaces/capability/services/capability_queue.py
+++ b/shared/workspaces/workspaces/capability/services/capability_queue.py
@@ -71,9 +71,10 @@ class CapabilityQueue(CapabilityQueueIF):
                 if engine:
                     execution = self.capability_info.lookup_execution(execution.id)
                     # Start engine
-                    engine.execute(execution)
+                    engine.load_engine(execution)
                     # Move engine to in-use list
                     self.engine_list.in_use.append(engine)
+                    engine.execute()
                 else:
                     # FIXME: Logging
                     print("No available engines. Try again later.")
@@ -87,7 +88,7 @@ class CapabilityQueue(CapabilityQueueIF):
         :return: Corresponding engine
         """
         for engine in self.engine_list.in_use:
-            if engine.execution_id == execution_id:
+            if engine.execution.id == execution_id:
                 # Found correct engine
                 return engine
 
@@ -99,7 +100,6 @@ class CapabilityQueue(CapabilityQueueIF):
         """
         engine = self.find_engine(execution_id)
         self.engine_list.in_use.remove(engine)
-        engine.execution_id = None
         engine.execution = None
         self.engine_list.available.put(engine)
 
diff --git a/shared/workspaces/workspaces/capability/services/interfaces.py b/shared/workspaces/workspaces/capability/services/interfaces.py
index 82fb5c7b4..89f2312ad 100644
--- a/shared/workspaces/workspaces/capability/services/interfaces.py
+++ b/shared/workspaces/workspaces/capability/services/interfaces.py
@@ -101,9 +101,12 @@ class CapabilityEngineIF(ABC):
     """
     Executes a prepare and run workflow step of a capability
     """
+    @abstractmethod
+    def load_engine(self, execution: CapabilityExecutionIF):
+        pass
 
     @abstractmethod
-    def execute(self, execution: CapabilityExecutionIF):
+    def execute(self):
         pass
 
     @abstractmethod
-- 
GitLab