Skip to content
Snippets Groups Projects
Commit f2dbdd77 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Only allow tasks in a final state to have a finished date

parent 9a92975c
No related branches found
No related tags found
1 merge request!2503Only allow tasks in a final state to have a finished date
# Generated by Django 5.0.8 on 2025-01-07 10:11
from django.db import migrations, models
from arkindex.ponos.models import State
# Copy the FINAL_STATES here so that if we ever change them,
# Django will detect it and require a new migration
FINAL_STATES = (
State.Completed,
State.Failed,
State.Error,
State.Stopped,
State.Cancelled,
)
def clear_unexpected_finish_dates(apps, schema_editor):
Task = apps.get_model("ponos", "Task")
Task.objects.exclude(state__in=FINAL_STATES).exclude(finished=None).update(finished=None)
class Migration(migrations.Migration):
dependencies = [
("ponos", "0013_task_ttl"),
("process", "0046_workerrun_ttl"),
]
operations = [
migrations.RunPython(
clear_unexpected_finish_dates,
reverse_code=migrations.RunPython.noop,
elidable=True,
),
migrations.AddConstraint(
model_name="task",
constraint=models.CheckConstraint(
check=models.Q(finished=None) | models.Q(state__in=FINAL_STATES),
name="task_finished_requires_final_state",
violation_error_message="Only tasks in a final state can have a finish date set.",
),
),
]
......@@ -394,6 +394,11 @@ class Task(models.Model):
name="task_finished_after_started",
violation_error_message="The task finish date must not be earlier than the task start date.",
),
models.CheckConstraint(
check=Q(finished=None) | Q(state__in=FINAL_STATES),
name="task_finished_requires_final_state",
violation_error_message="Only tasks in a final state can have a finish date set.",
),
]
def __str__(self) -> str:
......
......@@ -83,6 +83,7 @@ class TestTaskPartialUpdate(FixtureAPITestCase):
for (state_from, state_to) in self.docker_task_transitions:
with self.subTest(state_from=state_from, state_to=state_to):
self.task1.state = state_from
self.task1.finished = None
self.task1.save()
resp = self.client.patch(
reverse("api:task-details", args=[self.task1.id]),
......@@ -114,6 +115,7 @@ class TestTaskPartialUpdate(FixtureAPITestCase):
for (state_from, state_to) in self.slurm_task_transitions:
with self.subTest(state_from=state_from, state_to=state_to):
self.task1.state = state_from
self.task1.finished = None
self.task1.save()
resp = self.client.patch(
reverse("api:task-details", args=[self.task1.id]),
......
......@@ -501,6 +501,7 @@ class TestTaskUpdate(FixtureAPITestCase):
for (state_from, state_to) in self.docker_task_transitions:
with self.subTest(state_from=state_from, state_to=state_to):
self.task1.state = state_from
self.task1.finished = None
self.task1.save()
resp = self.client.put(
reverse("api:task-details", args=[self.task1.id]),
......@@ -532,6 +533,7 @@ class TestTaskUpdate(FixtureAPITestCase):
for (state_from, state_to) in self.slurm_task_transitions:
with self.subTest(state_from=state_from, state_to=state_to):
self.task1.state = state_from
self.task1.finished = None
self.task1.save()
resp = self.client.put(
reverse("api:task-details", args=[self.task1.id]),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment