Blame view

src/scheduler/tasks.py 2.26 KB
5b5566ab   haribo   added celery
1
2
3
from __future__ import absolute_import

from celery.task import Task
eecfb779   haribo   Date: 26/05/2016
4
import majordome
77816f10   haribo   Workflow implemen...
5
from scheduler.models import Scheduler
7a79e25b   haribo   Date: 19/05/2016
6
from pyrosapp.models import *
5b5566ab   haribo   added celery
7

77816f10   haribo   Workflow implemen...
8
9
10
from decimal import Decimal
import math
import itertools
5b5566ab   haribo   added celery
11

7a79e25b   haribo   Date: 19/05/2016
12
class scheduling(Task):
77816f10   haribo   Workflow implemen...
13

eecfb779   haribo   Date: 26/05/2016
14
15
    def run(self, first_schedule=False, alert=False):

eea995c9   haribo   Date: 16/06/2016
16
        '''First, we delete scheduled sequences (and plans in case of alert) '''
eecfb779   haribo   Date: 26/05/2016
17
        if alert is True:
9774228b   haribo   Date: 22/06/2016
18
            majordome.TaskManager.delete_pending_alert()
eecfb779   haribo   Date: 26/05/2016
19
        else:
9774228b   haribo   Date: 22/06/2016
20
            majordome.TaskManager.delete_pending_routine()
eea995c9   haribo   Date: 16/06/2016
21

77816f10   haribo   Workflow implemen...
22
23
        Log.objects.create(agent='Scheduler', message='Start schedule')
        self.scheduler = Scheduler()
7a79e25b   haribo   Date: 19/05/2016
24
25
26
        if first_schedule is True:
            self.determine_schedule_limits()
        self.scheduler.make_schedule(first_schedule)
eea995c9   haribo   Date: 16/06/2016
27

7a79e25b   haribo   Date: 19/05/2016
28
        schedule = Schedule.objects.order_by("-created")[0]
eecfb779   haribo   Date: 26/05/2016
29
        shs_list = schedule.shs.filter(status=Sequence.PENDING)
77816f10   haribo   Workflow implemen...
30
        Log.objects.create(agent='Scheduler', message='Scheduling finished')
eea995c9   haribo   Date: 16/06/2016
31
32

        ''' We create an execute_sequence task for every scheduled sequence '''
7a79e25b   haribo   Date: 19/05/2016
33
        for shs in shs_list:
eecfb779   haribo   Date: 26/05/2016
34
            res = majordome.tasks.execute_sequence.delay(shs.pk)
bb45cd4a   haribo   Date: 25/05/2016
35
            TaskId.objects.create(task_id=res.id, task="execute_sequence")
7a79e25b   haribo   Date: 19/05/2016
36

77816f10   haribo   Workflow implemen...
37
38
39
40
41
42
43
44
45
46
47
    def determine_schedule_limits(self):
        sequences = Sequence.objects.all()
        ''' Find the most recurrent night in all sequences '''
        days = {}
        for sequence in sequences:
            truncated = math.floor(float(sequence.jd1))
            if truncated in days.keys():
                days[truncated] += 1
            else:
                days[truncated] = 1
        maximum_truncated = max(days.keys(), key=(lambda key: days[key]))
7a79e25b   haribo   Date: 19/05/2016
48

77816f10   haribo   Workflow implemen...
49
        ''' Kick all the sequences not in this night '''
7a79e25b   haribo   Date: 19/05/2016
50
51
        sequences = [sequence for sequence in sequences if math.floor(
            float(sequence.jd1)) == maximum_truncated]
77816f10   haribo   Workflow implemen...
52
53
54
55
56
57
58
59
60

        ''' Set schedule limits as the smallest JD1 and the biggest JD2 '''
        plan_start = -1
        plan_end = -1
        for sequence in sequences:
            if plan_start == -1 or sequence.jd1 < plan_start:
                plan_start = sequence.jd1
            if plan_end == -1 or sequence.jd2 > plan_end:
                plan_end = sequence.jd2
7a79e25b   haribo   Date: 19/05/2016
61
        self.scheduler.set_night_limits(Decimal(plan_start), Decimal(plan_end))