Blame view

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

from celery.task import Task
77816f10   haribo   Workflow implemen...
4
from common.RequestBuilder import RequestBuilder
5b5566ab   haribo   added celery
5
6
7

from django.templatetags.static import static
import socket
77816f10   haribo   Workflow implemen...
8
from pyrosapp.models import Request, Sequence, Log
5b5566ab   haribo   added celery
9
10
11

import time

77816f10   haribo   Workflow implemen...
12
13
14
15
16
from celery.utils.timer2 import Timer

import os, shutil
import observation_manager

5b5566ab   haribo   added celery
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
IP = '127.0.0.1'
PORT = 31569

class alert_listener(Task):
    '''
        Launched at server start
        Listens to VOEvents, and create appropriate request when an event is received
    ''' 


    def run(self):
        print("run")
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.setblocking(True)
        self.server_socket.bind((IP, PORT))
        self.server_socket.listen(12)
        while True:
            #il faudra que tout ça soit dans un try ...
77816f10   haribo   Workflow implemen...
36
            print("Task en attente de signal")
5b5566ab   haribo   added celery
37
            self.server_socket.accept()
77816f10   haribo   Workflow implemen...
38
            print("signal reçu")
5b5566ab   haribo   added celery
39
40
41
            self.alert_received()

    def alert_received(self):
77816f10   haribo   Workflow implemen...
42
43
44
45
46
47
48
49
50
51
52
53
        # je supprime les sequences et requetes déjà existantes
        Sequence.objects.all().delete()
        Request.objects.all().delete()

        Log.objects.create(agent='Alert manager', message='Alert received')
        
        for file_name in os.listdir(observation_manager.tasks.IMAGES_FOLDER):
            if file_name != "empty":
                file_path = os.path.join(observation_manager.tasks.IMAGES_FOLDER, file_name)
                os.unlink(file_path)
        
        # j'ouvre le fichier des séquences de simulation, et j le lis ligne par ligne
5b5566ab   haribo   added celery
54
55
        with open("alert_manager/simulation_sequences", 'r') as sequences_file:
            sequences = sequences_file.readlines()
77816f10   haribo   Workflow implemen...
56
57
58

        Log.objects.create(agent='Alert manager', message='Simulation sequences file read')

5b5566ab   haribo   added celery
59
        sequences = [sequence.strip('\n') for sequence in sequences]
77816f10   haribo   Workflow implemen...
60
61
        request_builder = RequestBuilder()
        request_builder.start_new_request()
5b5566ab   haribo   added celery
62
        for sequence in sequences:
77816f10   haribo   Workflow implemen...
63
64
65
            request_builder.add_sequence(sequence)
        request_builder.validate_request()
        Log.objects.create(agent='Alert manager', message='Request built')
5b5566ab   haribo   added celery
66
67
68
69
70
71

class change_obs_strategy(Task):

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