Blame view

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

from celery.task import Task
9774228b   haribo   Date: 22/06/2016
4
import observation_manager
ddf59dd4   haribo   Remaniement :
5
from common.models import *
5b5566ab   haribo   added celery
6

ddf59dd4   haribo   Remaniement :
7
from devices import Telescope as Tel
5b5566ab   haribo   added celery
8

9774228b   haribo   Date: 22/06/2016
9
import time
9774228b   haribo   Date: 22/06/2016
10
import scheduler
5b5566ab   haribo   added celery
11

00fa2456   haribo   Debug adaption code
12
13
from decimal import Decimal

1b341a0f   haribo   Date: 19/07/2016
14
TIMESTAMP_JD = 2440587.500000
9774228b   haribo   Date: 22/06/2016
15
DAILY_SECOND = 1 / 86400
9b5bad52   haribo   Commented all the...
16
MAX_WAIT = 180
7a79e25b   haribo   Date: 19/05/2016
17

77816f10   haribo   Workflow implemen...
18
class execute_sequence(Task):
9b5bad52   haribo   Commented all the...
19
20
21
22
23
24
25
26
27
    '''
        Task to handle the execution of ONE sequence
        It receives the shs' id in parameter.
        It prepairs the instruments and cut the sequence in plan. Then the execute_plan tasks are created.
        
        If possible, it advances the sequence start.
        If the sequence can't start within MAX_WAIT seconds, it relaunches a scheduling
    '''

5b5566ab   haribo   added celery
28

7a79e25b   haribo   Date: 19/05/2016
29
    def run(self, shs_pk):
9774228b   haribo   Date: 22/06/2016
30
31
32
        # on ne supprime JAMAIS un séquence en cours, donc je peux la virer des tasks dès le début
        TaskId.objects.filter(task_id=self.request.id).delete()

7a79e25b   haribo   Date: 19/05/2016
33
34
        shs = ScheduleHasSequences.objects.get(pk=shs_pk)
        sequence = shs.sequence
bb45cd4a   haribo   Date: 25/05/2016
35

9774228b   haribo   Date: 22/06/2016
36
37
        if sequence.status != Sequence.OBSERVABLE:
            return
bbf6e698   haribo   Date: 14/06/2016
38

7a79e25b   haribo   Date: 19/05/2016
39
        message = 'Start sequence ' + str(sequence.pk) + ' execution'
77816f10   haribo   Workflow implemen...
40
        Log.objects.create(agent='Majordome', message=message)
9774228b   haribo   Date: 22/06/2016
41

7a79e25b   haribo   Date: 19/05/2016
42
        print("execute_sequence : ", sequence.pk)
7a79e25b   haribo   Date: 19/05/2016
43

9774228b   haribo   Date: 22/06/2016
44
        countdown = self.get_countdown(shs, sequence)
7a79e25b   haribo   Date: 19/05/2016
45

9b5bad52   haribo   Commented all the...
46
        if countdown > MAX_WAIT * DAILY_SECOND:
9774228b   haribo   Date: 22/06/2016
47
48
49
            scheduler.tasks.scheduling.delay(first_schedule=True, alert=False)  # TODO : changer le first_schedule à False
            return

6a1a30ed   carens_p   grammar for instr...
50
        tel = Tel.TelescopeController()
9774228b   haribo   Date: 22/06/2016
51
52
53

        tel.set("SPEED", 10.0, 10.0, 10.0)
        tel.set("COORDS", 104.0, 12.0, 88.0)
6a1a30ed   carens_p   grammar for instr...
54
        tel.set("COORDS_FRAME", "Radec")
9774228b   haribo   Date: 22/06/2016
55
56
        tel.set("TRACKING_SPEED", 0.3, 0.3, 0.3)
        tel.set("ACCEL", 1.0, 1.0, 1.0)
6a1a30ed   carens_p   grammar for instr...
57
        tel.set("ROTATOR", "Tracking")
9774228b   haribo   Date: 22/06/2016
58
        tel.set("FOCUS", 23562.0)
6a1a30ed   carens_p   grammar for instr...
59
        tel.set("MOVE_MODE", "GotoTrack")
9774228b   haribo   Date: 22/06/2016
60
61
62
63

        tel.do("START")

        countdown = 1  # TODO: à virer, juste pour pouvoir tester
7a79e25b   haribo   Date: 19/05/2016
64

77816f10   haribo   Workflow implemen...
65
        plans_results = []
9774228b   haribo   Date: 22/06/2016
66
67
68

        if sequence.albums.filter(detector__device__name="Cagire").exists():
            for plan in sequence.albums.get(detector__device__name="Cagire").plans.all():
ad85da7c   haribo   Date: 28/06/2016
69
                res = observation_manager.tasks.execute_plan_nir.apply_async((plan.id, countdown))
9774228b   haribo   Date: 22/06/2016
70
71
72
73
74
                TaskId.objects.create(task_id=res.id, task="execute_plan")
                plans_results.append(res)

        if sequence.albums.filter(detector__device__name="Visible camera").exists():
            for plan in sequence.albums.get(detector__device__name="Visible camera").plans.all():
ad85da7c   haribo   Date: 28/06/2016
75
                res = observation_manager.tasks.execute_plan_vis.apply_async((plan.id, countdown))
9774228b   haribo   Date: 22/06/2016
76
77
78
79
80
81
82
                TaskId.objects.create(task_id=res.id, task="execute_plan")
                plans_results.append(res)

        shs.status = Sequence.EXECUTING
        sequence.status = Sequence.EXECUTING
        shs.save()
        sequence.save()
7a79e25b   haribo   Date: 19/05/2016
83

77816f10   haribo   Workflow implemen...
84
        for plan_result in plans_results:
bb45cd4a   haribo   Date: 25/05/2016
85
            try:
9774228b   haribo   Date: 22/06/2016
86
87
88
89
90
91
                while plan_result.ready() == False:
                    print("not finished")
                    time.sleep(4)
                print("result : %s " % (plan_result.status,))
            except Exception as e:
                print("exception : %s" % (str(e),))
eecfb779   haribo   Date: 26/05/2016
92
                shs.status = Sequence.CANCELLED
9774228b   haribo   Date: 22/06/2016
93
                shs.save()
eecfb779   haribo   Date: 26/05/2016
94
                sequence.status = Sequence.CANCELLED
9774228b   haribo   Date: 22/06/2016
95
                sequence.save()
eecfb779   haribo   Date: 26/05/2016
96
97
98
99
                return

        shs.status = Sequence.EXECUTED
        sequence.status = Sequence.EXECUTED
9774228b   haribo   Date: 22/06/2016
100
101
        shs.save()
        sequence.save()
7a79e25b   haribo   Date: 19/05/2016
102
        message = 'Finished sequence ' + str(sequence.pk) + ' execution'
77816f10   haribo   Workflow implemen...
103
        Log.objects.create(agent='Majordome', message=message)
5b5566ab   haribo   added celery
104

9774228b   haribo   Date: 22/06/2016
105
106
107
108
109
110
111
    def get_countdown(self, shs, sequence):
        """
            Gets the time before the expected start of the execution.
            If it is > 10s, tries to get the sequence ahead according the JD1
        """
        countdown = 0

00fa2456   haribo   Debug adaption code
112
        current_time = Decimal(time.time() / 86400 + TIMESTAMP_JD)
9774228b   haribo   Date: 22/06/2016
113
114
115
116
117
118
119
120
121
122
123
        time_before_exec = shs.tsp - current_time

        if time_before_exec > 10 * DAILY_SECOND:
            time_before_jd1 = sequence.jd1 - current_time
            if time_before_jd1 < 0:
                pass
            elif time_before_jd1 < time_before_exec:
                countdown = time_before_jd1
            else:
                countdown = time_before_exec
        return countdown
7a79e25b   haribo   Date: 19/05/2016
124

5b5566ab   haribo   added celery
125
class system_pause(Task):
9b5bad52   haribo   Commented all the...
126
127
128
129
    '''
        Task called by the monitoring in case of problem.
        It stops the system and the instruments.
    '''
5b5566ab   haribo   added celery
130
131
132
133
134
135
136

    def run(self):
        time.sleep(5)
        print("system_pause")


class system_restart(Task):
9b5bad52   haribo   Commented all the...
137
138
139
140
    '''
        Task called by the monitoring when there is no more problem.
        Should just make a scheduling.
    '''
5b5566ab   haribo   added celery
141
142
143
144
145
146
    def run(self):
        time.sleep(5)
        print("system_restart")


class change_obs_conditions(Task):
9b5bad52   haribo   Commented all the...
147
148
149
150
151
    '''
        Task called by the monitoring when the obs condition have changed.
        It reads them in the DB and changes the sequences status in consequence.
        If needed, relaunches a scheduling
    '''
5b5566ab   haribo   added celery
152
    def run(self):
9b5bad52   haribo   Commented all the...
153
        # important : penser à rendre les quotas aux users
5b5566ab   haribo   added celery
154
155
        time.sleep(5)
        print("change_obs_conditions")