From 3fe66b15edbcb5231fc2497a033e9efbdef41c96 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Thu, 20 Aug 2020 15:46:10 +0200
Subject: [PATCH] TMSS-190: set start_time for scheduling (sub)tasks)

---
 SAS/TMSS/services/scheduling/lib/dynamic_scheduling.py | 5 ++++-
 SAS/TMSS/src/tmss/tmssapp/subtasks.py                  | 6 ++++--
 SAS/TMSS/src/tmss/tmssapp/tasks.py                     | 4 ++--
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/SAS/TMSS/services/scheduling/lib/dynamic_scheduling.py b/SAS/TMSS/services/scheduling/lib/dynamic_scheduling.py
index 5e0bc5ba3cc..ab8b0d09069 100644
--- a/SAS/TMSS/services/scheduling/lib/dynamic_scheduling.py
+++ b/SAS/TMSS/services/scheduling/lib/dynamic_scheduling.py
@@ -35,6 +35,7 @@ from lofar.sas.tmss.tmss.tmssapp import models
 from lofar.sas.tmss.tmss.tmssapp.tasks import schedule_independent_subtasks_in_scheduling_unit_blueprint
 from lofar.sas.tmss.tmss.tmssapp.subtasks import shift_successors_until_after_stop_time
 from lofar.sas.tmss.client.tmssbuslistener import *
+from lofar.sas.tmss.tmss.exceptions import *
 
 def schedule_next_scheduling_unit() -> models.SchedulingUnitBlueprint:
     defined_independend_subtasks = models.Subtask.independent_subtasks().filter(state__value='defined')
@@ -48,7 +49,9 @@ def schedule_next_scheduling_unit() -> models.SchedulingUnitBlueprint:
     remaining_scheduling_units = scheduling_units[1:]
 
     # schedule the first one!
-    scheduled_scheduling_unit = schedule_independent_subtasks_in_scheduling_unit_blueprint(first_scheduling_unit)
+    # it can start running now + the needed lofar startup time of 1~2 minutes.
+    start_time = datetime.utcnow() + timedelta(seconds=90)
+    scheduled_scheduling_unit = schedule_independent_subtasks_in_scheduling_unit_blueprint(first_scheduling_unit, start_time=start_time)
 
     # update the starttimes of the remaining ones (so they form queue, and can be visualized in a timeline)
     prev_scheduling_unit = scheduled_scheduling_unit # keep track of the previous
diff --git a/SAS/TMSS/src/tmss/tmssapp/subtasks.py b/SAS/TMSS/src/tmss/tmssapp/subtasks.py
index d9970ff6c7d..e1f0d17a1ae 100644
--- a/SAS/TMSS/src/tmss/tmssapp/subtasks.py
+++ b/SAS/TMSS/src/tmss/tmssapp/subtasks.py
@@ -837,12 +837,14 @@ def create_and_schedule_subtasks_from_task_blueprint(task_blueprint: TaskBluepri
     return schedule_independent_subtasks_in_task_blueprint(task_blueprint)
 
 
-def schedule_independent_subtasks_in_task_blueprint(task_blueprint: TaskBlueprint) -> [Subtask]:
+def schedule_independent_subtasks_in_task_blueprint(task_blueprint: TaskBlueprint, start_time: datetime=None) -> [Subtask]:
     '''Convenience method: Schedule (and return) the subtasks in the task_blueprint that are not dependend on any predecessors'''
     independent_subtasks = list(Subtask.independent_subtasks().filter(task_blueprint_id=task_blueprint.id, state__value=SubtaskState.Choices.DEFINED.value).all())
 
     for subtask in independent_subtasks:
-        schedule_subtask_and_update_successor_start_times(subtask)
+        if start_time is not None:
+            subtask.start_time = start_time
+            schedule_subtask_and_update_successor_start_times(subtask)
 
     return independent_subtasks
 
diff --git a/SAS/TMSS/src/tmss/tmssapp/tasks.py b/SAS/TMSS/src/tmss/tmssapp/tasks.py
index ea0272de574..0e5e8eb630a 100644
--- a/SAS/TMSS/src/tmss/tmssapp/tasks.py
+++ b/SAS/TMSS/src/tmss/tmssapp/tasks.py
@@ -285,12 +285,12 @@ def create_task_blueprints_and_subtasks_and_schedule_subtasks_from_scheduling_un
     return scheduling_unit_blueprint
 
 
-def schedule_independent_subtasks_in_scheduling_unit_blueprint(scheduling_unit_blueprint: SchedulingUnitBlueprint) -> models.SchedulingUnitBlueprint:
+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'''
     task_blueprints = list(scheduling_unit_blueprint.task_blueprints.all())
 
     for task_blueprint in task_blueprints:
-        schedule_independent_subtasks_in_task_blueprint(task_blueprint)
+        schedule_independent_subtasks_in_task_blueprint(task_blueprint, start_time=start_time)
 
     scheduling_unit_blueprint.refresh_from_db()
     return scheduling_unit_blueprint
-- 
GitLab