Projet

Général

Profil

0001-disable-cron-command-if-settings.DISABLE_CRON_JOBS-i.patch

Thomas Noël, 30 octobre 2018 18:01

Télécharger (3,2 ko)

Voir les différences:

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(-)
tests/test_publisher.py
10 10
import mock
11 11
import pytest
12 12

  
13
from django.conf import settings
13 14
from django.core.management import call_command
14 15
from django.core.management.base import CommandError
16
from django.test import override_settings
15 17
from quixote import cleanup
16 18
from wcs.qommon.http_request import HTTPRequest
17 19

  
......
205 207
    with mock.patch('wcs.qommon.management.commands.cron.cron_worker') as cron_worker:
206 208
        call_command('cron')
207 209
        assert cron_worker.call_count == 1
210

  
211
    # disable cron system
212
    with override_settings(DISABLE_CRON_JOBS=True):
213
        with mock.patch('wcs.qommon.management.commands.cron.cron_worker') as cron_worker:
214
            call_command('cron')
215
            assert cron_worker.call_count == 0
wcs/qommon/management/commands/cron.py
18 18
import time
19 19
import os
20 20

  
21
from django.conf import settings
21 22
from django.core.management.base import BaseCommand, CommandError
22 23
from qommon.publisher import get_publisher_class
23 24

  
......
31 32

  
32 33
    def add_arguments(self, parser):
33 34
        parser.set_defaults(verbosity=0)
35
        parser.add_argument('--force-job', dest='force_job', action='store_true',
36
            help='Run even if DISABLE_CRON_JOBS is set in settings')
34 37

  
35
    def handle(self, verbosity, **kwargs):
38
    def handle(self, verbosity, **options):
39
        if getattr(settings, 'DISABLE_CRON_JOBS', False) and not options['force_job']:
40
            if verbosity > 1:
41
                print('Command is ignored because DISABLE_CRON_JOBS is set in settings')
42
            return
36 43
        lockfile = os.path.join(tempfile.gettempdir(), 'wcs-cron-in-progress.lock')
37 44
        try:
38 45
            with locket.lock_file(lockfile, timeout=0):
wcs/settings.py
159 159
# we use 28s by default: timeout just before web server, which is usually 30s
160 160
REQUESTS_TIMEOUT = 28
161 161

  
162
# For high availability installations with multiple instances of w.c.s.
163
# components, one should disable cron jobs execution on secondary servers;
164
# set the following variable True disables "cron" management command
165
DISABLE_CRON_JOBS = False
166

  
162 167
local_settings_file = os.environ.get('WCS_SETTINGS_FILE',
163 168
        os.path.join(os.path.dirname(__file__), 'local_settings.py'))
164 169
if os.path.exists(local_settings_file):
165
-