tasks.py
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import absolute_import
from celery.task import Task
import majordome
from scheduler.models import Scheduler
from pyrosapp.models import *
from decimal import Decimal
import math
import itertools
class scheduling(Task):
def run(self, first_schedule=False, alert=False):
''' First, we delete scheduled sequences (and plans in case of alert) '''
if alert is True:
majordome.TaskManager.delete_pending_tasks(['execute_sequence', 'execute_plan'])
else:
majordome.TaskManager.delete_pending_tasks(['execute_sequence'])
Log.objects.create(agent='Scheduler', message='Start schedule')
self.scheduler = Scheduler()
if first_schedule is True:
self.determine_schedule_limits()
self.scheduler.make_schedule(first_schedule)
print("nb schedules %d" % len(Schedule.objects.all()))
schedule = Schedule.objects.order_by("-created")[0]
print(schedule.created, len(schedule.sequences.all()))
shs_list = schedule.shs.filter(status=Sequence.PENDING)
print("Nb of shs %d, planned %d" % (len(schedule.shs.all()), len(shs_list)))
Log.objects.create(agent='Scheduler', message='Scheduling finished')
for shs in shs_list:
res = majordome.tasks.execute_sequence.delay(shs.pk)
print("add exseq : ", res.id)
TaskId.objects.create(task_id=res.id, task="execute_sequence")
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]))
''' Kick all the sequences not in this night '''
sequences = [sequence for sequence in sequences if math.floor(
float(sequence.jd1)) == maximum_truncated]
''' 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
self.scheduler.set_night_limits(Decimal(plan_start), Decimal(plan_end))