From 63541ccb85f73d0361f77328c6ffb185f219e600 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Mon, 1 Oct 2018 15:56:31 +0200 Subject: [PATCH] disable cron command if settings.DISABLE_CRON_JOBS is set (#26863) --- tests/test_publisher.py | 8 ++++++++ wcs/qommon/management/commands/cron.py | 9 ++++++++- wcs/settings.py | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_publisher.py b/tests/test_publisher.py index 3598beee..0c2b310b 100644 --- a/tests/test_publisher.py +++ b/tests/test_publisher.py @@ -10,6 +10,7 @@ import zipfile import mock import pytest +from django.conf import settings from django.core.management import call_command from django.core.management.base import CommandError from quixote import cleanup @@ -205,3 +206,10 @@ def test_cron_command(): with mock.patch('wcs.qommon.management.commands.cron.cron_worker') as cron_worker: call_command('cron') assert cron_worker.call_count == 1 + + # disable cron system + assert settings.DISABLE_CRON_JOBS == False + settings.DISABLE_CRON_JOBS = True + with mock.patch('wcs.qommon.management.commands.cron.cron_worker') as cron_worker: + call_command('cron') + assert cron_worker.call_count == 0 diff --git a/wcs/qommon/management/commands/cron.py b/wcs/qommon/management/commands/cron.py index 48b2a299..3cdcf3cb 100644 --- a/wcs/qommon/management/commands/cron.py +++ b/wcs/qommon/management/commands/cron.py @@ -18,6 +18,7 @@ import tempfile import time import os +from django.conf import settings from django.core.management.base import BaseCommand, CommandError from qommon.publisher import get_publisher_class @@ -31,8 +32,14 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.set_defaults(verbosity=0) + parser.add_argument('--force-job', dest='force_job', action='store_true', + help='Run even if DISABLE_CRON_JOBS is set in settings') - def handle(self, verbosity, **kwargs): + def handle(self, verbosity, **options): + if getattr(settings, 'DISABLE_CRON_JOBS', False) and not options['force_job']: + if verbosity > 1: + print('Command is ignored because DISABLE_CRON_JOBS is set in settings') + return lockfile = os.path.join(tempfile.gettempdir(), 'wcs-cron-in-progress.lock') try: with locket.lock_file(lockfile, timeout=0): diff --git a/wcs/settings.py b/wcs/settings.py index 0382d385..3569502a 100644 --- a/wcs/settings.py +++ b/wcs/settings.py @@ -159,6 +159,11 @@ REQUESTS_PROXIES = None # we use 28s by default: timeout just before web server, which is usually 30s REQUESTS_TIMEOUT = 28 +# For high availability installations with multiple instances of w.c.s. +# components, one should disable cron jobs execution on secondary servers; +# set the following variable True disables "cron" management command +DISABLE_CRON_JOBS = False + local_settings_file = os.environ.get('WCS_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')) if os.path.exists(local_settings_file): -- 2.19.0