aboutsummaryrefslogtreecommitdiff
path: root/src/core_modules/cron.py
blob: 9ad6cb07dfddfe59e9f6c8043ab4d8d390913b72 (about) (plain) (blame)
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
import datetime, time
from src import ModuleManager, utils

class Module(ModuleManager.BaseModule):
    def on_load(self):
        now = datetime.datetime.utcnow()
        next_minute = now.replace(minute=now.minute+1, second=0, microsecond=0)
        until = time.time()+(next_minute-now).total_seconds()
        self.timers.add("cron", self._minute, 60, until)

    def _minute(self, timer):
        now = datetime.datetime.utcnow().replace(second=0, microsecond=0)
        timer.redo()

        current_schedule = [now.minute, now.hour]

        events = self.events.on("cron")
        for cron in events.get_hooks():
            schedule = cron.get_kwarg("schedule").split(" ")

            due = True
            for i, schedule_part in enumerate(schedule):
                current_part = current_schedule[i]
                if schedule_part.startswith("*/"):
                    schedule_step = int(schedule_part.split("*/", 1)[1])
                    if (current_part%schedule_step) == 0:
                        continue
                elif schedule_part == "*":
                    continue
                elif int(current_part) == schedule_part:
                    continue

                due = False
                break

            if due:
                cron.call(events.make_event())