wcs/wcs/qommon/management/commands/cron.py

118 lines
4.7 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2014 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import datetime
import sys
import setproctitle
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.utils.timezone import now
from wcs import sql
from wcs.qommon.cron import CronJob, cron_worker
from wcs.qommon.publisher import get_publisher_class
class Command(BaseCommand):
help = 'Execute cronjobs'
def add_arguments(self, parser):
parser.set_defaults(verbosity=0)
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN')
parser.add_argument(
'--force-job',
dest='force_job',
action='store_true',
help='Run even if DISABLE_CRON_JOBS is set in settings',
)
parser.add_argument('--job', dest='job_name', metavar='NAME')
def handle(self, verbosity, domain=None, job_name=None, **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
if domain:
domains = [domain]
else:
domains = [x.hostname for x in get_publisher_class().get_tenants()]
if not job_name and verbosity > 2:
print('cron start')
publisher_class = get_publisher_class()
publisher_class.register_cronjobs()
publisher = publisher_class.create_publisher()
offset = ord(settings.SECRET_KEY[-1]) % 60
if not job_name:
CronJob.log('starting cron (minutes offset is %s)' % offset, in_tenant=False)
if not domain and not options['force_job']:
# exit early if maximum number of workers has been reached
running = 0
stalled_tenants = []
for hostname in domains:
publisher.set_tenant_by_hostname(hostname)
if not publisher.has_postgresql_config():
continue
status, timestamp = sql.get_cron_status()
if status == 'running':
running += 1
if now() - timestamp > datetime.timedelta(hours=6):
stalled_tenants.append(hostname)
CronJob.log('stalled tenant: %s' % hostname)
sql.mark_cron_status('done')
if stalled_tenants:
raise CommandError('aborting, stalled tenants: %s' % ', '.join(stalled_tenants))
if running >= settings.CRON_WORKERS:
CronJob.log('skipped, too many workers')
return
for hostname in domains:
publisher.set_tenant_by_hostname(hostname)
if publisher.get_site_option('disable_cron_jobs', 'variables'):
if verbosity > 1:
print('cron ignored on %s because DISABLE_CRON_JOBS is set' % hostname)
continue
if not publisher.has_postgresql_config():
if verbosity > 1:
print('cron ignored on %s because it has no PostgreSQL configuration' % hostname)
continue
if domain:
cron_status, timestamp = 'ignored', now()
else:
cron_status, timestamp = sql.get_and_update_cron_status()
if not options['force_job']:
if cron_status == 'running':
if verbosity > 1:
print(hostname, 'skip running, already handled')
continue
setproctitle.setproctitle(sys.argv[0] + ' cron [%s]' % hostname)
if verbosity > 1:
print('cron work on %s' % hostname)
CronJob.log('start')
try:
cron_worker(publisher, timestamp, job_name=job_name)
except Exception as e:
CronJob.log('aborted (%r)' % e)
raise e
finally:
if not domain:
sql.mark_cron_status('done')
if verbosity > 1:
print('cron end')