first draft on cron module

This commit is contained in:
jesopo 2019-12-11 15:43:17 +00:00
parent ff050b3583
commit 60db9ff389

37
src/core_modules/cron.py Normal file
View file

@ -0,0 +1,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())