From b9c4cefb59c297602d367e7e689a2552e12eb1ce Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 15 Jan 2018 22:44:57 +0100 Subject: [PATCH] Better format for crons --- platypush/config/__init__.py | 12 ++++-------- platypush/cron/scheduler.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index 359bc4945a..a8fe8011b3 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -76,10 +76,9 @@ class Config(object): self.event_hooks = {} self.procedures = {} self.constants = {} - self.cronjobs = [] + self.cronjobs = {} self._init_constants() - self._init_cronjobs() self._init_components() @@ -116,6 +115,9 @@ class Config(object): elif key.startswith('event.hook.'): hook_name = '.'.join(key.split('.')[2:]) self.event_hooks[hook_name] = self._config[key] + elif key.startswith('cron.'): + cron_name = '.'.join(key.split('.')[1:]) + self.cronjobs[cron_name] = self._config[key] elif key.startswith('procedure.'): tokens = key.split('.') async = True if tokens[1] == 'async' else False @@ -135,12 +137,6 @@ class Config(object): self.constants[key] = value - def _init_cronjobs(self): - if 'cron' in self._config: - for job in self._config['cron']['jobs']: - self.cronjobs.append(job) - - @staticmethod def get_backends(): global _default_config_instance diff --git a/platypush/cron/scheduler.py b/platypush/cron/scheduler.py index bc21be32c5..17af74308f 100644 --- a/platypush/cron/scheduler.py +++ b/platypush/cron/scheduler.py @@ -11,6 +11,7 @@ class Cronjob(Thread): def __init__(self, name, cron_expression, actions, *args, **kwargs): super().__init__() self.cron_expression = cron_expression + self.name = name self.actions = [] for action in actions: @@ -56,15 +57,14 @@ class CronScheduler(Thread): super().__init__() self.jobs_config = jobs logging.info('Cron scheduler initialized with {} jobs' - .format(len(self.jobs_config))) + .format(len(self.jobs_config.keys()))) @classmethod - def _build_job(cls, job_config): - if isinstance(job_config, dict): - job = Cronjob(cron_expression=job_config['cron_expression'], - name=job_config['name'], - actions=job_config['actions']) + def _build_job(cls, name, config): + if isinstance(config, dict): + job = Cronjob(name=name, cron_expression=config['cron_expression'], + actions=config['actions']) assert isinstance(job, Cronjob) return job @@ -74,8 +74,8 @@ class CronScheduler(Thread): logging.info('Running cron scheduler') while True: - for job_config in self.jobs_config: - job = self._build_job(job_config) + for (job_name, job_config) in self.jobs_config.items(): + job = self._build_job(name=job_name, config=job_config) if job.should_run(): job.start()