|
1
|
|
|
"""Module responsible to handle schedulers.""" |
|
2
|
2 |
|
from uuid import uuid4 |
|
3
|
|
|
|
|
4
|
2 |
|
from apscheduler.schedulers.background import BackgroundScheduler |
|
5
|
2 |
|
from apscheduler.triggers.cron import CronTrigger |
|
6
|
2 |
|
from pytz import utc |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
2 |
|
class CircuitSchedule: |
|
10
|
|
|
"""Schedule events.""" |
|
11
|
|
|
|
|
12
|
2 |
|
def __init__(self, **kwargs): |
|
13
|
|
|
"""Create a CircuitSchedule object.""" |
|
14
|
2 |
|
self._id = kwargs.get('id', uuid4().hex) |
|
15
|
2 |
|
self.date = kwargs.get('date', None) |
|
16
|
2 |
|
self.interval = kwargs.get('interval', None) |
|
17
|
2 |
|
self.frequency = kwargs.get('frequency', None) |
|
18
|
2 |
|
self.action = kwargs.get('action', 'create') |
|
19
|
|
|
|
|
20
|
2 |
|
@property |
|
21
|
|
|
def id(self): # pylint: disable=invalid-name |
|
22
|
|
|
"""Return this EVC's ID.""" |
|
23
|
2 |
|
return self._id |
|
24
|
|
|
|
|
25
|
2 |
|
def as_dict(self): |
|
26
|
|
|
"""Return a dictionary representing an circuit schedule object.""" |
|
27
|
2 |
|
circuit_scheduler_dict = {'id': self.id, 'action': self.action} |
|
28
|
|
|
|
|
29
|
2 |
|
if self.date: |
|
30
|
|
|
circuit_scheduler_dict['date'] = self.date |
|
31
|
2 |
|
if self.frequency: |
|
32
|
2 |
|
circuit_scheduler_dict['frequency'] = self.frequency |
|
33
|
2 |
|
if self.interval: |
|
34
|
2 |
|
circuit_scheduler_dict['interval'] = self.interval |
|
35
|
|
|
|
|
36
|
2 |
|
return circuit_scheduler_dict |
|
37
|
|
|
|
|
38
|
2 |
|
@classmethod |
|
39
|
|
|
def from_dict(cls, data): |
|
40
|
|
|
"""Return a CircuitSchedule object from dict.""" |
|
41
|
2 |
|
return cls(**data) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
2 |
|
class Scheduler: |
|
45
|
|
|
"""Class to schedule the circuits rules.""" |
|
46
|
|
|
|
|
47
|
2 |
|
def __init__(self): |
|
48
|
|
|
"""Create a new schedule structure.""" |
|
49
|
2 |
|
self.scheduler = BackgroundScheduler(timezone=utc) |
|
50
|
2 |
|
self.scheduler.start() |
|
51
|
|
|
|
|
52
|
2 |
|
def shutdown(self): |
|
53
|
|
|
"""Shutdown the scheduler.""" |
|
54
|
2 |
|
self.scheduler.shutdown(wait=False) |
|
55
|
|
|
|
|
56
|
2 |
|
def add(self, circuit): |
|
57
|
|
|
"""Add all circuit_scheduler from specific circuit.""" |
|
58
|
2 |
|
for circuit_scheduler in circuit.circuit_scheduler: |
|
59
|
2 |
|
data = {'id': circuit_scheduler.id} |
|
60
|
2 |
|
action = None |
|
61
|
|
|
|
|
62
|
2 |
|
if circuit_scheduler.action == 'create': |
|
63
|
2 |
|
action = circuit.deploy |
|
64
|
2 |
|
elif circuit_scheduler.action == 'remove': |
|
65
|
2 |
|
action = circuit.remove |
|
66
|
|
|
|
|
67
|
2 |
|
if circuit_scheduler.date: |
|
68
|
2 |
|
data.update({'run_date': circuit_scheduler.date}) |
|
69
|
2 |
|
self.scheduler.add_job(action, 'date', **data) |
|
70
|
|
|
else: |
|
71
|
2 |
|
data.update({'start_date': circuit.start_date, |
|
72
|
|
|
'end_date': circuit.end_date}) |
|
73
|
|
|
|
|
74
|
2 |
|
if circuit_scheduler.interval: |
|
75
|
2 |
|
data.update(circuit_scheduler.interval) |
|
76
|
2 |
|
self.scheduler.add_job(action, 'interval', **data) |
|
77
|
|
|
|
|
78
|
2 |
|
elif circuit_scheduler.frequency: |
|
79
|
2 |
|
cron = CronTrigger.from_crontab(circuit_scheduler.frequency, |
|
80
|
|
|
timezone=utc) |
|
81
|
2 |
|
self.scheduler.add_job(action, cron, **data) |
|
82
|
|
|
|
|
83
|
2 |
|
def remove(self, circuit): |
|
84
|
|
|
"""Remove all scheduler from a circuit.""" |
|
85
|
|
|
for job in circuit.circuit_scheduler: |
|
86
|
|
|
self.cancel_job(job.id) |
|
87
|
|
|
|
|
88
|
2 |
|
def cancel_job(self, circuit_scheduler_id): |
|
89
|
|
|
"""Cancel a specific job from scheduler.""" |
|
90
|
|
|
self.scheduler.remove_job(circuit_scheduler_id) |
|
91
|
|
|
|