From ae0f912d6ac35a73f39a66a1d0e33a5ae5e0906c Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Tue, 13 Apr 2021 19:48:07 +0200 Subject: [PATCH] TMSS-666: added convenience methods and actions to cancel task/scheduling_unit blueprint --- SAS/TMSS/backend/src/tmss/tmssapp/tasks.py | 18 ++++++++++++- .../tmss/tmssapp/viewsets/specification.py | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py b/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py index eceb99e688c..38276a789a3 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py @@ -1,6 +1,6 @@ from lofar.sas.tmss.tmss.exceptions import * from lofar.sas.tmss.tmss.tmssapp import models -from lofar.sas.tmss.tmss.tmssapp.subtasks import unschedule_subtasks_in_task_blueprint +from lofar.sas.tmss.tmss.tmssapp.subtasks import unschedule_subtasks_in_task_blueprint, cancel_subtask from lofar.sas.tmss.tmss.tmssapp.models.specification import TaskBlueprint, SchedulingUnitBlueprint from lofar.sas.tmss.tmss.tmssapp.subtasks import create_and_schedule_subtasks_from_task_blueprint, create_subtasks_from_task_blueprint, schedule_independent_subtasks_in_task_blueprint, update_subtasks_start_times_for_scheduling_unit from lofar.common.datetimeutils import round_to_minute_precision @@ -422,3 +422,19 @@ def unschedule_subtasks_in_scheduling_unit_blueprint(scheduling_unit_blueprint: scheduling_unit_blueprint.refresh_from_db() return scheduling_unit_blueprint + +def cancel_task_blueprint(task_blueprint: TaskBlueprint) -> TaskBlueprint: + '''Convenience method: cancel all subtasks in the task_blueprint''' + for subtask in task_blueprint.subtasks.all(): + cancel_subtask(subtask) + 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''' + for task_blueprint in scheduling_unit_blueprint.task_blueprints.all(): + cancel_task_blueprint(task_blueprint) + scheduling_unit_blueprint.refresh_from_db() + return scheduling_unit_blueprint diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py index b8eefe04f4f..c60756ca161 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py @@ -15,6 +15,7 @@ from rest_framework.response import Response from rest_framework.decorators import permission_classes from rest_framework.permissions import IsAuthenticated from rest_framework.decorators import action +from rest_framework.response import Response as RestResponse from drf_yasg.utils import swagger_auto_schema from drf_yasg.openapi import Parameter @@ -825,6 +826,19 @@ class SchedulingUnitBlueprintViewSet(LOFARViewSet): # result is list of dict so thats why return JsonResponse(result, safe=False) + @swagger_auto_schema(responses={200: 'The cancelled version of this scheduling_unit', + 403: 'forbidden', + 500: 'The subtask scheduling_unit not be cancelled'}, + operation_description="Try to cancel this scheduling_unit.") + @action(methods=['get'], detail=True, url_name="cancel") + def cancel(self, request, pk=None): + scheduling_unit_blueprint = get_object_or_404(models.SchedulingUnitBlueprint, pk=pk) + from lofar.sas.tmss.tmss.tmssapp.tasks import cancel_scheduling_unit_blueprint + scheduling_unit_blueprint = cancel_scheduling_unit_blueprint(scheduling_unit_blueprint) + serializer = self.get_serializer(scheduling_unit_blueprint) + return RestResponse(serializer.data) + + class SchedulingUnitBlueprintExtendedViewSet(SchedulingUnitBlueprintViewSet): serializer_class = serializers.SchedulingUnitBlueprintExtendedSerializer @@ -1044,6 +1058,18 @@ class TaskBlueprintViewSet(LOFARViewSet): serializer = self.get_serializer(successors, many=True) return Response(serializer.data) + @swagger_auto_schema(responses={200: 'The cancelled version of this task', + 403: 'forbidden', + 500: 'The subtask task not be cancelled'}, + operation_description="Try to cancel this task.") + @action(methods=['get'], detail=True, url_name="cancel") + def cancel(self, request, pk=None): + task_blueprint = get_object_or_404(models.SchedulingUnitBlueprint, pk=pk) + from lofar.sas.tmss.tmss.tmssapp.tasks import cancel_task_blueprint + task_blueprint = cancel_task_blueprint(task_blueprint) + serializer = self.get_serializer(task_blueprint) + return RestResponse(serializer.data) + class TaskBlueprintNestedViewSet(LOFARNestedViewSet): queryset = models.TaskBlueprint.objects.all() -- GitLab