From 23a3272ef6a465f263bc73a4d1d03e4bf022af30 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Wed, 27 Nov 2024 14:55:33 +0000
Subject: [PATCH] Add cancelled task state

---
 arkindex/ponos/models.py                          |  8 ++++++++
 arkindex/ponos/serializers.py                     | 10 ++++++++--
 arkindex/ponos/tests/tasks/test_partial_update.py |  1 +
 arkindex/ponos/tests/tasks/test_update.py         |  1 +
 4 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/arkindex/ponos/models.py b/arkindex/ponos/models.py
index f64685581b..c139b7d9ce 100644
--- a/arkindex/ponos/models.py
+++ b/arkindex/ponos/models.py
@@ -172,6 +172,12 @@ class State(Enum):
     State where a task that entered the Stopping state has successfully stopped.
     """
 
+    Cancelled = "cancelled"
+    """
+    State where a task has been stopped because it has exceeded a resource limit (time, CPU, etc.).
+    This limit may have been enforced by Arkindex or by a third party.
+    """
+
 
 # States where a task is considered final.
 # Once a task reaches a final state, its state should no longer change.
@@ -180,6 +186,7 @@ FINAL_STATES = (
     State.Failed,
     State.Error,
     State.Stopped,
+    State.Cancelled,
 )
 
 # States where a task could start, be running, or still be using some resources, on an agent
@@ -197,6 +204,7 @@ ACTIVE_STATES = (
 # the process is set as running to prevent retrying and allow stopping.
 STATES_ORDERING = [
     State.Running,
+    State.Cancelled,
     State.Failed,
     State.Error,
     State.Stopping,
diff --git a/arkindex/ponos/serializers.py b/arkindex/ponos/serializers.py
index 59085cd3d5..ce6bd6f6cb 100644
--- a/arkindex/ponos/serializers.py
+++ b/arkindex/ponos/serializers.py
@@ -36,7 +36,8 @@ class TaskLightSerializer(serializers.ModelSerializer):
 
                 Pending ⟶ Running ⟶ Completed
                   └⟶ Error   ├⟶ Failed
-                             └⟶ Error
+                             ├⟶ Error
+                             └⟶ Cancelled
                 Stopping ⟶ Stopped
                   └⟶ Error
 
@@ -81,7 +82,12 @@ class TaskLightSerializer(serializers.ModelSerializer):
             allowed_transitions = {
                 State.Unscheduled: [State.Pending],
                 State.Pending: [State.Running, State.Error],
-                State.Running: [State.Completed, State.Failed, State.Error],
+                State.Running: [
+                    State.Completed,
+                    State.Failed,
+                    State.Error,
+                    State.Cancelled,
+                ],
                 State.Stopping: [State.Stopped, State.Error],
             }
             if user.mode == AgentMode.Slurm:
diff --git a/arkindex/ponos/tests/tasks/test_partial_update.py b/arkindex/ponos/tests/tasks/test_partial_update.py
index 0306ea5ad6..e43cc217ae 100644
--- a/arkindex/ponos/tests/tasks/test_partial_update.py
+++ b/arkindex/ponos/tests/tasks/test_partial_update.py
@@ -49,6 +49,7 @@ class TestTaskPartialUpdate(FixtureAPITestCase):
             (State.Running, State.Completed),
             (State.Running, State.Failed),
             (State.Running, State.Error),
+            (State.Running, State.Cancelled),
             (State.Stopping, State.Stopped),
             (State.Stopping, State.Error),
         )
diff --git a/arkindex/ponos/tests/tasks/test_update.py b/arkindex/ponos/tests/tasks/test_update.py
index eee58e157d..97029d9324 100644
--- a/arkindex/ponos/tests/tasks/test_update.py
+++ b/arkindex/ponos/tests/tasks/test_update.py
@@ -59,6 +59,7 @@ class TestTaskUpdate(FixtureAPITestCase):
             (State.Running, State.Completed),
             (State.Running, State.Failed),
             (State.Running, State.Error),
+            (State.Running, State.Cancelled),
             (State.Stopping, State.Stopped),
             (State.Stopping, State.Error),
         )
-- 
GitLab