tasks.py
1.78 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
from __future__ import absolute_import
from celery.task import Task
from majordome.tasks import execute_sequence
from scheduler.models import Scheduler
from pyrosapp.models import Sequence, Log
from decimal import Decimal
import math
import itertools
class scheduling(Task):
def run(self):
Log.objects.create(agent='Scheduler', message='Start schedule')
self.scheduler = Scheduler()
self.determine_schedule_limits()
self.scheduler.make_schedule()
sequences = Sequence.objects.filter(status=Sequence.PLANNED).order_by("tsp")
Log.objects.create(agent='Scheduler', message='Scheduling finished')
for sequence in sequences:
execute_sequence.delay(sequence.pk)
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))