From 0098c8f730490419e755a76802f0a6198bc0bac2 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Fri, 8 Sep 2017 00:09:31 +0200 Subject: [PATCH] don't start a new cron if locked (#18519) --- wcs/qommon/management/commands/cron.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/wcs/qommon/management/commands/cron.py b/wcs/qommon/management/commands/cron.py index a95dba66..045481e9 100644 --- a/wcs/qommon/management/commands/cron.py +++ b/wcs/qommon/management/commands/cron.py @@ -21,6 +21,7 @@ import os from django.core.management.base import BaseCommand from qommon.publisher import get_publisher_class +from qommon import get_logger from qommon.vendor import locket from qommon.cron import cron_worker @@ -29,12 +30,16 @@ class Command(BaseCommand): help = 'Execute cronjobs' def handle(self, verbosity, **options): - with locket.lock_file(os.path.join(tempfile.gettempdir(), 'wcs-cron')): - now = time.localtime() - publisher_class = get_publisher_class() - publisher_class.register_cron = True - publisher = publisher_class.create_publisher() - app_dir = publisher.app_dir - for hostname in publisher.get_tenants(): - publisher.app_dir = os.path.join(app_dir, hostname) - cron_worker(publisher, now) + lockfile = os.path.join(tempfile.gettempdir(), 'wcs-cron-in-progress.lock') + try: + with locket.lock_file(lockfile, timeout=0): + now = time.localtime() + publisher_class = get_publisher_class() + publisher_class.register_cron = True + publisher = publisher_class.create_publisher() + app_dir = publisher.app_dir + for hostname in publisher.get_tenants(): + publisher.app_dir = os.path.join(app_dir, hostname) + cron_worker(publisher, now) + except locket.LockError: + get_logger().warn('can not start cron job - locked by %s' % lockfile) -- 2.14.1