From 877ff79f869d97616d84db95406fcce6eeddd8b7 Mon Sep 17 00:00:00 2001
From: Charlotte Hausman <chausman@nrao.edu>
Date: Thu, 13 Apr 2023 17:14:40 -0400
Subject: [PATCH] catch the case where directory no longer exists

---
 .../test/test_remote_processing_service.py    |  3 ++-
 .../services/remote_processing_service.py     | 21 ++++++++++++-------
 .../workflow/services/workflow_service.py     |  5 ++++-
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/shared/workspaces/test/test_remote_processing_service.py b/shared/workspaces/test/test_remote_processing_service.py
index 845f1f8ee..a48fe15d8 100644
--- a/shared/workspaces/test/test_remote_processing_service.py
+++ b/shared/workspaces/test/test_remote_processing_service.py
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Workspaces.  If not, see <https://www.gnu.org/licenses/>.
 from datetime import datetime
-from unittest.mock import patch
+from unittest.mock import patch, MagicMock
 
 from test.test_workflow_info import FakeWorkflowInfo
 from workspaces.system.schema import AbstractFile
@@ -92,6 +92,7 @@ class TestCapoInjector:
     @patch("pathlib.Path.unlink")
     @patch("glob.glob")
     @patch("os.listdir")
+    @patch("pathlib.Path.exists", MagicMock(return_value=True))
     def test_clear_subspace(self, mock_os, mock_glob, mock_remove):
         injector.clear_subspace()
         assert mock_os.call_count == 1
diff --git a/shared/workspaces/workspaces/workflow/services/remote_processing_service.py b/shared/workspaces/workspaces/workflow/services/remote_processing_service.py
index 9e23b5f34..33e115e9b 100644
--- a/shared/workspaces/workspaces/workflow/services/remote_processing_service.py
+++ b/shared/workspaces/workspaces/workflow/services/remote_processing_service.py
@@ -115,12 +115,17 @@ class CapoInjector:
         path.write_bytes(subspace.content)
         logger.info(f"Writing capo subspace file to {self.dir_path.__str__()}")
 
-    def clear_subspace(self):
+    def clear_subspace(self) -> bool:
         logger.info(f"Clearing capo subspace file from {self.dir_path.__str__()}...")
-        for file in os.listdir(self.dir_path):
-            if file.endswith(".properties"):
-                Path.unlink(self.dir_path / file)
-
-        check = glob.glob("*.properties")
-        if check is None:
-            logger.info("Capo subspace cleared successfully.")
+        if self.dir_path.exists():
+            for file in os.listdir(self.dir_path):
+                if file.endswith(".properties"):
+                    Path.unlink(self.dir_path / file)
+
+            check = glob.glob("*.properties")
+            if check is None:
+                logger.info("Capo subspace cleared successfully.")
+            return True
+        else:
+            logger.info(f"Directory {self.dir_path.__str__()} has already been cleaned.")
+            return False
diff --git a/shared/workspaces/workspaces/workflow/services/workflow_service.py b/shared/workspaces/workspaces/workflow/services/workflow_service.py
index b284005fd..be1cce6d6 100644
--- a/shared/workspaces/workspaces/workflow/services/workflow_service.py
+++ b/shared/workspaces/workspaces/workflow/services/workflow_service.py
@@ -1141,7 +1141,10 @@ class WorkflowMessageHandler:
 
         if injector.is_remote_workflow():
             logger.debug("Cleaning remote workflow")
-            injector.clear_subspace()
+            result = injector.clear_subspace()
+            if result is False:
+                # the processing directory somehow disappeared, mark as cleaned to avoid further errors
+                request.cleaned = True
 
     @staticmethod
     def clean_workflow(request: WorkflowRequest):
-- 
GitLab