diff --git a/SAS/TMSS/src/tmss/tmssapp/subtasks.py b/SAS/TMSS/src/tmss/tmssapp/subtasks.py index 8255000a46f6e87263e044081fe48f87b80342ed..149232d2f4b53dd33b6945980a202ac3892f2768 100644 --- a/SAS/TMSS/src/tmss/tmssapp/subtasks.py +++ b/SAS/TMSS/src/tmss/tmssapp/subtasks.py @@ -462,6 +462,39 @@ def schedule_subtask(subtask: Subtask) -> Subtask: # ... and re-raise the original exception raise +def unschedule_subtask(subtask: Subtask) -> Subtask: + '''unschedule the given subtask, removing all output dataproducts, and setting its state back to 'defined'.''' + if subtask.state.value != SubtaskState.Choices.SCHEDULED.value: + raise SubtaskSchedulingException("Cannot unschedule subtask id=%d because it is not SCHEDULED. Current state=%s" % (subtask.pk, subtask.state.value)) + + try: + subtask.state = SubtaskState.objects.get(value=SubtaskState.Choices.UNSCHEDULING.value) + subtask.save() + + for output in subtask.outputs.all(): + output.dataproducts.all().delete() + #TODO: delete dataproduct transforms + + subtask.state = SubtaskState.objects.get(value=SubtaskState.Choices.DEFINED.value) + subtask.save() + except Exception as e: + try: + # set the subtask to state 'ERROR'... + subtask.state = SubtaskState.objects.get(value=SubtaskState.Choices.ERROR.value) + subtask.save() + except Exception as e2: + logger.error(e2) + finally: + # ... and re-raise the original exception + raise + +def unschedule_subtasks_in_task_blueprint(task_blueprint: TaskBlueprint): + '''Convenience method: Unschedule (and return) all scheduled subtasks in the task_blueprint''' + scheduled_subtasks = list(task_blueprint.subtasks.filter(state__value=SubtaskState.Choices.SCHEDULED.value).all()) + for subtask in scheduled_subtasks: + unschedule_subtask(subtask) + + def schedule_subtask_and_update_successor_start_times(subtask: Subtask) -> Subtask: scheduled_subtask = schedule_subtask(subtask) shift_successors_until_after_stop_time(scheduled_subtask) diff --git a/SAS/TMSS/src/tmss/tmssapp/tasks.py b/SAS/TMSS/src/tmss/tmssapp/tasks.py index 9522164a455352144278cac2029e1a7a2132a2c4..9f6b129b42a7b6ece4084e65252263e6b61f5b8e 100644 --- a/SAS/TMSS/src/tmss/tmssapp/tasks.py +++ b/SAS/TMSS/src/tmss/tmssapp/tasks.py @@ -1,7 +1,7 @@ from lofar.sas.tmss.tmss.exceptions import * from lofar.sas.tmss.tmss.tmssapp import models from lofar.sas.tmss.tmss.tmssapp.models.specification import TaskBlueprint, SchedulingUnitBlueprint, TaskDraft, SchedulingRelationPlacement -from lofar.sas.tmss.tmss.tmssapp.subtasks import create_and_schedule_subtasks_from_task_blueprint +from lofar.sas.tmss.tmss.tmssapp.subtasks import create_and_schedule_subtasks_from_task_blueprint, unschedule_subtasks_in_task_blueprint 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 from lofar.messaging.messagebus import ToBus, DEFAULT_BROKER, DEFAULT_BUSNAME @@ -286,7 +286,7 @@ def create_task_blueprints_and_subtasks_and_schedule_subtasks_from_scheduling_un def schedule_independent_subtasks_in_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBlueprint, start_time: datetime=None) -> models.SchedulingUnitBlueprint: - '''Convenience method: Schedule the subtasks in the task_blueprint that are not dependend on predecessors''' + '''Convenience method: Schedule the subtasks in the scheduling_unit_blueprint that are not dependend on predecessors''' task_blueprints = list(scheduling_unit_blueprint.task_blueprints.all()) for task_blueprint in task_blueprints: @@ -294,3 +294,15 @@ def schedule_independent_subtasks_in_scheduling_unit_blueprint(scheduling_unit_b scheduling_unit_blueprint.refresh_from_db() return scheduling_unit_blueprint + + +def unschedule_subtasks_in_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBlueprint) -> models.SchedulingUnitBlueprint: + '''Convenience method: Unschedule all scheduled subtasks in the scheduling_unit_blueprint''' + task_blueprints = list(scheduling_unit_blueprint.task_blueprints.all()) + + for task_blueprint in task_blueprints: + unschedule_subtasks_in_task_blueprint(task_blueprint) + + scheduling_unit_blueprint.refresh_from_db() + return scheduling_unit_blueprint +