Skip to content
Snippets Groups Projects
Commit 42571256 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-854: added convenience methods to mark all cancelled/error (sub)tasks as obsolete

parent 715b9117
No related branches found
No related tags found
3 merge requests!634WIP: COBALT commissioning delta,!495TMSS-854: OBSOLETE state,!481Draft: SW-971 SW-973 SW-975: Various fixes to build LOFAR correctly.
......@@ -344,6 +344,11 @@ class SubtaskAllowedStateTransitions(Model):
allowed_new_states = SubtaskAllowedStateTransitions.allowed_new_states(old_state)
return list(SubtaskState.objects.exclude(value__in=[s.value for s in allowed_new_states]).exclude(pk=old_state.pk).all())
@staticmethod
def allowed_old_states(new_state: SubtaskState) -> [SubtaskState]:
'''get a list of all states we are allowed to transition from to the given new_state'''
return [transition.old_state for transition in SubtaskAllowedStateTransitions.objects.filter(new_state=new_state).all()]
class SubtaskStateLog(BasicCommon):
"""
......
......@@ -448,6 +448,16 @@ def cancel_task_blueprint(task_blueprint: TaskBlueprint) -> TaskBlueprint:
return task_blueprint
def mark_task_blueprint_as_obsolete(task_blueprint: TaskBlueprint) -> TaskBlueprint:
'''Convenience method: mark all cancelled/error subtasks in the task_blueprint as obsolete'''
obsolete_state = models.SubtaskState.objects.get(value=models.SubtaskState.Choices.OBSOLETE.value)
transitionable_states = models.SubtaskAllowedStateTransitions.allowed_old_states(obsolete_state)
for subtask in task_blueprint.subtasks.filter(state__in=transitionable_states):
subtask.state = obsolete_state
subtask.save()
task_blueprint.refresh_from_db()
return task_blueprint
def cancel_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBlueprint) -> SchedulingUnitBlueprint:
'''Convenience method: cancel all subtasks in the task_blueprints in the scheduling_unit_blueprint'''
......@@ -456,6 +466,16 @@ def cancel_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBl
scheduling_unit_blueprint.refresh_from_db()
return scheduling_unit_blueprint
def mark_scheduling_unit_blueprint_as_obsolete(scheduling_unit_blueprint: SchedulingUnitBlueprint) -> SchedulingUnitBlueprint:
'''Convenience method: mark all subtasks in the task_blueprints in the scheduling_unit_blueprint as obsolete'''
for task_blueprint in scheduling_unit_blueprint.task_blueprints.all():
mark_task_blueprint_as_obsolete(task_blueprint)
scheduling_unit_blueprint.refresh_from_db()
return scheduling_unit_blueprint
def create_cleanuptask_for_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBlueprint) -> models.SchedulingUnitBlueprint:
'''create a cleanuptask for the given scheduling_unit which will cleanup all output dataproducts from tasks in this scheduling_unit which aren't already cleaned up'''
......
......@@ -898,6 +898,19 @@ class SchedulingUnitBlueprintViewSet(LOFARViewSet):
return RestResponse(serializer.data)
@swagger_auto_schema(responses={200: 'The obsolete version of this scheduling_unit',
403: 'forbidden',
500: 'The scheduling_unit could not be marked as obsolete. Was it in a state from which it is allowed to mark it as obsolete?'},
operation_description="Try to mark this scheduling_unit as obsolete (when the status is cancelled/error)")
@action(methods=['post'], detail=True, url_name="mark_as_obsolete")
def mark_as_obsolete(self, request, pk=None):
scheduling_unit_blueprint = get_object_or_404(models.SchedulingUnitBlueprint, pk=pk)
from lofar.sas.tmss.tmss.tmssapp.tasks import mark_scheduling_unit_blueprint_as_obsolete
scheduling_unit_blueprint = mark_scheduling_unit_blueprint_as_obsolete(scheduling_unit_blueprint)
serializer = self.get_serializer(scheduling_unit_blueprint)
return RestResponse(serializer.data)
@swagger_auto_schema(responses={200: "All Subtasks in this SchedulingUnitBlueprint",
403: 'forbidden'},
operation_description="Get all subtasks for this scheduling_unit")
......@@ -1159,6 +1172,19 @@ class TaskBlueprintViewSet(LOFARViewSet):
return RestResponse(serializer.data)
@swagger_auto_schema(responses={200: 'The obsolete version of this task_blueprint',
403: 'forbidden',
500: 'The task_blueprint could not be marked as obsolete. Was it in a state from which it is allowed to mark it as obsolete?'},
operation_description="Try to mark this task_blueprint as obsolete (when the status is cancelled/error)")
@action(methods=['post'], detail=True, url_name="mark_as_obsolete")
def mark_as_obsolete(self, request, pk=None):
task_blueprint = get_object_or_404(models.TaskBlueprint, pk=pk)
from lofar.sas.tmss.tmss.tmssapp.tasks import mark_task_blueprint_as_obsolete
task_blueprint = mark_task_blueprint_as_obsolete(task_blueprint)
serializer = self.get_serializer(task_blueprint)
return RestResponse(serializer.data)
class TaskBlueprintNestedViewSet(LOFARNestedViewSet):
queryset = models.TaskBlueprint.objects.all()
serializer_class = serializers.TaskBlueprintSerializer
......
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