Better format for crons

This commit is contained in:
Fabio Manganiello 2018-01-15 22:44:57 +01:00
parent 14b511034f
commit b9c4cefb59
2 changed files with 12 additions and 16 deletions

View file

@ -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

View file

@ -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()