From e1f919bd20e55ac8efcc8016d102ef9e8d413d82 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 18 Dec 2021 04:19:57 +0100 Subject: [PATCH] feat(scheduler): Add function to add scheduled tasks programmatically --- flaschengeist/plugins/scheduler.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/flaschengeist/plugins/scheduler.py b/flaschengeist/plugins/scheduler.py index 2a4af6a..7f52db7 100644 --- a/flaschengeist/plugins/scheduler.py +++ b/flaschengeist/plugins/scheduler.py @@ -17,19 +17,25 @@ class __Task: _scheduled_tasks: dict[__Task] = dict() +def add_scheduled(id: str, function, replace=False, **kwargs): + if id not in _scheduled_tasks or replace: + _scheduled_tasks[id] = __Task(function, **kwargs) + logger.info(f"Registered task: {id}") + else: + logger.debug(f"Skipping already registered task: {id}") + + def scheduled(id: str, replace=False, **kwargs): """ kwargs: days, hours, minutes """ def real_decorator(function): - if id not in _scheduled_tasks or replace: - logger.info(f"Registered task: {id}") - _scheduled_tasks[id] = __Task(function, **kwargs) - else: - logger.debug(f"Skipping already registered task: {id}") + add_scheduled(id, function, replace, **kwargs) return function + if not isinstance(id, str): + raise TypeError return real_decorator @@ -75,9 +81,11 @@ class SchedulerPlugin(Plugin): ) task.function() changed = True + else: + logger.debug(f"Skip task {id}, is scheduled for {last_run + task.interval}") if changed: # Remove not registered tasks for id in status.keys(): if id not in _scheduled_tasks.keys(): del status[id] - self.set_setting("status", status) + self.set_setting("status", status)