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

TMSS-1806: moved SchedulerSubsystemActivator into Subsystem Activator nested class for reuse.

parent 188754b2
No related branches found
No related tags found
1 merge request!933TMSS-1806
...@@ -94,7 +94,8 @@ class Scheduler: ...@@ -94,7 +94,8 @@ class Scheduler:
self._scheduling_thread_running = False self._scheduling_thread_running = False
self._do_schedule_event = Event() self._do_schedule_event = Event()
super().__init__() super().__init__()
SchedulerSubsystemActivator.deactivate() # make sure initial status is idle # make sure initial status is idle
models.Subsystem.Activator('scheduler').deactivate()
def start_scheduling(self): def start_scheduling(self):
'''Prepares the scheduling_units, performs one full schedule computation, and start the scheduling loop and wait for a trigger.''' '''Prepares the scheduling_units, performs one full schedule computation, and start the scheduling loop and wait for a trigger.'''
...@@ -206,7 +207,7 @@ class Scheduler: ...@@ -206,7 +207,7 @@ class Scheduler:
def schedule_fixed_time_scheduling_units(self): def schedule_fixed_time_scheduling_units(self):
''' Schedule all schedulable fixed_time scheduling units.''' ''' Schedule all schedulable fixed_time scheduling units.'''
with SchedulerSubsystemActivator(): with models.Subsystem.Activator('scheduler'):
# exclude units for inactive projects # exclude units for inactive projects
mark_scheduling_units_for_inactive_projects_unschedulable() mark_scheduling_units_for_inactive_projects_unschedulable()
...@@ -248,7 +249,7 @@ class Scheduler: ...@@ -248,7 +249,7 @@ class Scheduler:
def do_dynamic_schedule(self) -> models.SchedulingUnitBlueprint: def do_dynamic_schedule(self) -> models.SchedulingUnitBlueprint:
'''do a full update of the schedule: schedule next scheduling unit and assign start stop times to remaining schedulable scheduling units''' '''do a full update of the schedule: schedule next scheduling unit and assign start stop times to remaining schedulable scheduling units'''
with SchedulerSubsystemActivator(): with models.Subsystem.Activator('scheduler'):
logger.info("Updating (dynamic) schedule....") logger.info("Updating (dynamic) schedule....")
scheduled_unit = self.schedule_next_scheduling_unit() scheduled_unit = self.schedule_next_scheduling_unit()
...@@ -816,34 +817,3 @@ def cancel_running_observation_if_needed_and_possible(candidate: ScoredSchedulin ...@@ -816,34 +817,3 @@ def cancel_running_observation_if_needed_and_possible(candidate: ScoredSchedulin
else: else:
logger.info('NOT cancelling subtask pk=%s trigger_priority=%s for triggered scheduling_unit pk=%s trigger_priority=%s because its priority is too low' % logger.info('NOT cancelling subtask pk=%s trigger_priority=%s for triggered scheduling_unit pk=%s trigger_priority=%s because its priority is too low' %
(obs.pk, obs.project.trigger_priority, candidate.scheduling_unit.pk, candidate.scheduling_unit.project.trigger_priority)) (obs.pk, obs.project.trigger_priority, candidate.scheduling_unit.pk, candidate.scheduling_unit.project.trigger_priority))
class SchedulerSubsystemActivator:
'''Helper class to activate/deactivate the 'scheduler' subsystem entry in the database.
By using the 'with'-context manager, it is guaranteed to deactivate the setting upon scope exit (thus also by exceptions)'''
@staticmethod
def activate():
'''activate the 'scheduler' subsystem status'''
try:
scheduler_subsytem = models.Subsystem.objects.get(name='scheduler')
scheduler_subsytem.status = models.SubsystemStatus.objects.get(value=models.SubsystemStatus.Choices.ACTIVE.value)
scheduler_subsytem.save()
except Exception as e:
logger.warning("Could not update scheduler subsystem status: %s", e)
@staticmethod
def deactivate():
'''deactivate the 'scheduler' subsystem status (idle)'''
try:
scheduler_subsytem = models.Subsystem.objects.get(name='scheduler')
scheduler_subsytem.status = models.SubsystemStatus.objects.get(value=models.SubsystemStatus.Choices.IDLE.value)
scheduler_subsytem.save()
except Exception as e:
logger.warning("Could not update scheduler subsystem status: %s", e)
def __enter__(self):
self.activate()
def __exit__(self, exc_type, exc_val, exc_tb):
self.deactivate()
...@@ -283,6 +283,37 @@ class Subsystem(NamedCommonPK): ...@@ -283,6 +283,37 @@ class Subsystem(NamedCommonPK):
# superclass's name is primary key # superclass's name is primary key
status = ForeignKey('SubsystemStatus', null=False, on_delete=PROTECT) status = ForeignKey('SubsystemStatus', null=False, on_delete=PROTECT)
class Activator:
'''Helper class to activate/deactivate a subsystem entry in the database.
By using the 'with'-context manager, it is guaranteed to deactivate the setting upon scope exit (thus also by exceptions)'''
def __init__(self, subsystem_name: str):
self.subsystem_name = subsystem_name
def __update_state(self, status: str):
'''update the subsystem's status'''
try:
scheduler_subsytem = Subsystem.objects.get(name=self.subsystem_name)
scheduler_subsytem.status = SubsystemStatus.objects.get(value=status)
scheduler_subsytem.save()
except Exception as e:
logger.warning("Could not update %s subsystem status: %s", self.subsystem_name, e)
def activate(self):
'''activate the subsystem status'''
self.__update_state(SubsystemStatus.Choices.ACTIVE.value)
def deactivate(self):
'''deactivate the subsystem status'''
self.__update_state(SubsystemStatus.Choices.IDLE.value)
def __enter__(self):
'''activate the subsystem status upon entering the with-context'''
self.activate()
def __exit__(self, exc_type, exc_val, exc_tb):
'''deactivate the subsystem status upon exiting the with-context'''
self.deactivate()
class TaskConnectorType(Model): class TaskConnectorType(Model):
''' Describes the data type & format combinations a Task can accept or produce. The "role" is used to distinguish ''' Describes the data type & format combinations a Task can accept or produce. The "role" is used to distinguish
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment