TaskManager.py 1.61 KB
from common.models import *

import time
from devices import Telescope, CameraVIS, CameraNIR


def delete_pending_tasks(task_names, terminate=False):
    """
        Deletes the pending tasks given in task_names, getting their ids in DB.
        Removes them from db.
        :param task_names: array of tasks names to delete 
    """

    # TODO : il faudra aller plus loin, et dire aux instruments de s'arrĂȘter

    for task_name in task_names:
        tasks = TaskId.objects.filter(task=task_name)
        for task in tasks:
            print("je revoke : ", task.task_id)
            # TODO:
            ###revoke(task.task_id, terminate=terminate)
            task.delete()

        """ The sleep is in case of sequence stopping :
            if it was creating a plan_exec, the creation must be finished before we try to delete it """
        time.sleep(1)


def delete_pending_routine():
    """
        It is called in case of a routine registered
        It deletes the pending execute_sequence
    """

    delete_pending_tasks(["execute_sequence"], terminate=False)
    tel = Telescope.TelescopeController()
    tel.do("ABORT")


def delete_pending_alert():
    """
        It is called in case of an alert registered
        It deletes the pending execute_sequence, and the pending + executing plans
    """

    delete_pending_tasks(["execute_sequence"], terminate=False)
    delete_pending_tasks(["execute_plan"], terminate=False)

    tel = Telescope.TelescopeController()
    VIScam = CameraVIS.VISCameraController()
    NIRcam = CameraNIR.NIRCameraController()
    tel.do("ABORT")
    VIScam.do("ABORT")
    NIRcam.do("ABORT")