From 4f0ffb3dc5862a7e47cdc97fe940dbd663b1e6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 21 Jul 2018 09:32:52 +0200 Subject: [PATCH] misc: run hourly jobs at a fixed minute (#25402) --- wcs/publisher.py | 3 +-- wcs/qommon/cron.py | 8 +++++++- wcs/qommon/publisher.py | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/wcs/publisher.py b/wcs/publisher.py index bf4ad665..3880e182 100644 --- a/wcs/publisher.py +++ b/wcs/publisher.py @@ -114,8 +114,7 @@ class WcsPublisher(StubWcsPublisher): def register_cronjobs(cls): super(WcsPublisher, cls).register_cronjobs() # every hour: check for global action timeouts - cls.register_cronjob(CronJob(cls.apply_global_action_timeouts, - minutes=[random.randint(0, 59)])) + cls.register_cronjob(CronJob(cls.apply_global_action_timeouts, hourly=True)) def is_using_postgresql(self): return bool(self.has_site_option('postgresql') and self.cfg.get('postgresql', {})) diff --git a/wcs/qommon/cron.py b/wcs/qommon/cron.py index 6af321ba..5c67cb8c 100644 --- a/wcs/qommon/cron.py +++ b/wcs/qommon/cron.py @@ -16,6 +16,8 @@ import sys +from django.conf import settings + class CronJob(object): hours = None minutes = None @@ -23,12 +25,16 @@ class CronJob(object): days = None function = None - def __init__(self, function, hours = None, minutes = None, weekdays = None, days = None): + def __init__(self, function, hours=None, minutes=None, weekdays=None, days=None, hourly=False): self.function = function self.hours = hours self.minutes = minutes self.weekdays = weekdays self.days = days + if hourly: + # set minutes to an arbitrary value based on installation, this + # prevents waking up all jobs at the same time on a server farm. + self.minutes = [ord(settings.SECRET_KEY[-1]) % 60] def cron_worker(publisher, now): try: diff --git a/wcs/qommon/publisher.py b/wcs/qommon/publisher.py index b6672ab8..77136959 100644 --- a/wcs/qommon/publisher.py +++ b/wcs/qommon/publisher.py @@ -740,8 +740,8 @@ class QommonPublisher(Publisher, object): def register_cronjobs(cls): cls.register_cronjob(CronJob(cls.clean_sessions, minutes=range(0, 60, 5))) cls.register_cronjob(CronJob(cls.clean_nonces, minutes=range(0, 60, 5))) - cls.register_cronjob(CronJob(cls.clean_afterjobs, minutes=[random.randint(0, 59)])) - cls.register_cronjob(CronJob(cls.clean_tempfiles, minutes=[random.randint(0, 59)])) + cls.register_cronjob(CronJob(cls.clean_afterjobs, hourly=True)) + cls.register_cronjob(CronJob(cls.clean_tempfiles, hourly=True)) register_tld_names = False -- 2.18.0