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.event_hooks = {}
self.procedures = {} self.procedures = {}
self.constants = {} self.constants = {}
self.cronjobs = [] self.cronjobs = {}
self._init_constants() self._init_constants()
self._init_cronjobs()
self._init_components() self._init_components()
@ -116,6 +115,9 @@ class Config(object):
elif key.startswith('event.hook.'): elif key.startswith('event.hook.'):
hook_name = '.'.join(key.split('.')[2:]) hook_name = '.'.join(key.split('.')[2:])
self.event_hooks[hook_name] = self._config[key] 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.'): elif key.startswith('procedure.'):
tokens = key.split('.') tokens = key.split('.')
async = True if tokens[1] == 'async' else False async = True if tokens[1] == 'async' else False
@ -135,12 +137,6 @@ class Config(object):
self.constants[key] = value 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 @staticmethod
def get_backends(): def get_backends():
global _default_config_instance global _default_config_instance

View file

@ -11,6 +11,7 @@ class Cronjob(Thread):
def __init__(self, name, cron_expression, actions, *args, **kwargs): def __init__(self, name, cron_expression, actions, *args, **kwargs):
super().__init__() super().__init__()
self.cron_expression = cron_expression self.cron_expression = cron_expression
self.name = name
self.actions = [] self.actions = []
for action in actions: for action in actions:
@ -56,15 +57,14 @@ class CronScheduler(Thread):
super().__init__() super().__init__()
self.jobs_config = jobs self.jobs_config = jobs
logging.info('Cron scheduler initialized with {} jobs' logging.info('Cron scheduler initialized with {} jobs'
.format(len(self.jobs_config))) .format(len(self.jobs_config.keys())))
@classmethod @classmethod
def _build_job(cls, job_config): def _build_job(cls, name, config):
if isinstance(job_config, dict): if isinstance(config, dict):
job = Cronjob(cron_expression=job_config['cron_expression'], job = Cronjob(name=name, cron_expression=config['cron_expression'],
name=job_config['name'], actions=config['actions'])
actions=job_config['actions'])
assert isinstance(job, Cronjob) assert isinstance(job, Cronjob)
return job return job
@ -74,8 +74,8 @@ class CronScheduler(Thread):
logging.info('Running cron scheduler') logging.info('Running cron scheduler')
while True: while True:
for job_config in self.jobs_config: for (job_name, job_config) in self.jobs_config.items():
job = self._build_job(job_config) job = self._build_job(name=job_name, config=job_config)
if job.should_run(): if job.should_run():
job.start() job.start()