Passed
Pull Request — master (#112)
by Rogerio
01:44
created

build.scheduler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 86
ccs 48
cts 50
cp 0.96
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

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