From 0c923295f49bb027ff7bd2e7fbe797164ee7a1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 5 Apr 2017 16:26:01 +0200 Subject: [PATCH] general: add possibility to skip all cron jobs (#15470) This is useful for load balancing as jobs should only be run on one host. Original was amended with a condition on "--all-tenants" and help messages. Signed-off-by: Christophe Siraut --- debian/debian_config_common.py | 6 ++++++ .../management/commands/tenant_command.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index a78f263..4aa4e7b 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -24,6 +24,12 @@ ADMINS = ( EMAIL_SUBJECT_PREFIX = '[%s] ' % PROJECT_NAME +# For high availability installations with multiple instances of Publik +# components, one should disable cron jobs execution on secondary servers; +# set the following variable True disables all tenant_commands launched with +# option "--all-tenants". +DISABLE_CRON_JOBS = False + LOGGING = { 'version': 1, 'disable_existing_loggers': True, diff --git a/hobo/multitenant/management/commands/tenant_command.py b/hobo/multitenant/management/commands/tenant_command.py index e80c9be..43f5f9c 100644 --- a/hobo/multitenant/management/commands/tenant_command.py +++ b/hobo/multitenant/management/commands/tenant_command.py @@ -1,9 +1,13 @@ +# -*- coding: utf-8 -*- # this file derive from django-tenant-schemas # Author: Bernardo Pires Carneiro # Email: carneiro.be@gmail.com # License: MIT license # Home-page: http://github.com/bcarneiro/django-tenant-schemas + import argparse + +from django.conf import settings from django.core.management.base import BaseCommand, CommandError from django.core.management import call_command, get_commands, load_command_class from django.db import connection @@ -40,8 +44,24 @@ class Command(InteractiveTenantOption, BaseCommand): args_parser.add_argument("--all-tenants", help="apply command to all tenants", action='store_true') args_parser.add_argument("-d", "--domain", dest="domain_name", help="specify tenant domain name") + args_parser.add_argument( + '--force-job', dest='force_job', action='store_true', + help='Run command even if DISABLE_CRON_JOBS is set') + args_parser.add_argument( + '-v', '--verbosity', action='store', dest='verbosity', default=1, + type=int, choices=[0, 1, 2, 3], + help='Verbosity level; 0=minimal output, 1=normal output, ' + '2=verbose output, 3=very verbose output', + ) args_namespace, args = args_parser.parse_known_args(argv) + if (args_namespace.all_tenants and not args_namespace.force_job and + getattr(settings, 'DISABLE_CRON_JOBS', False)): + if args_namespace.verbosity > 1: + print('Command %s is ignored because DISABLE_CRON_JOBS is set' + % app_name) + return + if args_namespace.all_tenants: for tenant in TenantMiddleware.get_tenants(): connection.set_tenant(tenant) -- 2.11.0