tasks.py
2.33 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
62
63
64
65
66
67
68
69
70
71
from __future__ import absolute_import
from celery.task import Task
from common.RequestBuilder import RequestBuilder
from django.templatetags.static import static
import socket
from pyrosapp.models import Request, Sequence, Log
import time
from celery.utils.timer2 import Timer
import os, shutil
import observation_manager
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 ...
print("Task en attente de signal")
self.server_socket.accept()
print("signal reçu")
self.alert_received()
def alert_received(self):
# 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
with open("alert_manager/simulation_sequences", 'r') as sequences_file:
sequences = sequences_file.readlines()
Log.objects.create(agent='Alert manager', message='Simulation sequences file read')
sequences = [sequence.strip('\n') for sequence in sequences]
request_builder = RequestBuilder()
request_builder.start_new_request()
for sequence in sequences:
request_builder.add_sequence(sequence)
request_builder.validate_request()
Log.objects.create(agent='Alert manager', message='Request built')
class change_obs_strategy(Task):
def run(self):
time.sleep(5)
print("change_obs_strategy")