From 31b2eac56ac7a311e957930a812375628dd096c5 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 ++++++ hobo/multitenant/management/commands/tenant_command.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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..0ecbbbd 100644 --- a/hobo/multitenant/management/commands/tenant_command.py +++ b/hobo/multitenant/management/commands/tenant_command.py @@ -3,7 +3,10 @@ # 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 @@ -15,7 +18,7 @@ class Command(InteractiveTenantOption, BaseCommand): help = "Wrapper around django commands for use with an individual tenant" args = '' - def run_from_argv(self, argv): + def run_from_argv(self, argv, **kwargs): """ Changes the option_list to use the options from the wrapped command. Adds schema parameter to specify which schema will be used when @@ -40,8 +43,18 @@ 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_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 kwargs.get('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