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
|
|
|
|
61
|
2 |
|
if circuit_scheduler.action == 'create': |
62
|
2 |
|
action = circuit.deploy |
63
|
2 |
|
elif circuit_scheduler.action == 'remove': |
64
|
2 |
|
action = circuit.remove |
65
|
|
|
else: |
66
|
|
|
continue |
67
|
|
|
|
68
|
2 |
|
if circuit_scheduler.date: |
69
|
2 |
|
data.update({'run_date': circuit_scheduler.date}) |
70
|
2 |
|
self.scheduler.add_job(action, 'date', **data) |
71
|
|
|
else: |
72
|
2 |
|
data.update({'start_date': circuit.start_date, |
73
|
|
|
'end_date': circuit.end_date}) |
74
|
|
|
|
75
|
2 |
|
if circuit_scheduler.interval: |
76
|
2 |
|
data.update(circuit_scheduler.interval) |
77
|
2 |
|
self.scheduler.add_job(action, 'interval', **data) |
78
|
|
|
|
79
|
2 |
|
elif circuit_scheduler.frequency: |
80
|
2 |
|
cron = CronTrigger.from_crontab(circuit_scheduler.frequency, |
81
|
|
|
timezone=utc) |
82
|
2 |
|
self.scheduler.add_job(action, cron, **data) |
83
|
|
|
|
84
|
2 |
|
def cancel_job(self, circuit_scheduler_id): |
85
|
|
|
"""Cancel a specific job from scheduler.""" |
86
|
|
|
self.scheduler.remove_job(circuit_scheduler_id) |
87
|
|
|
|