add a management command to run cron jobs (#6735)
This commit is contained in:
parent
263edfbf2a
commit
bb707d540f
|
@ -0,0 +1,3 @@
|
|||
MAILTO=root
|
||||
|
||||
* * * * * wcs /usr/bin/wcs-manage cron
|
|
@ -7,6 +7,9 @@ import StringIO
|
|||
import os
|
||||
import zipfile
|
||||
|
||||
import mock
|
||||
|
||||
from django.core.management import call_command
|
||||
from quixote import cleanup
|
||||
from wcs.qommon.http_request import HTTPRequest
|
||||
|
||||
|
@ -124,6 +127,8 @@ def test_get_tenants():
|
|||
assert not 'plop.invalid' in tenants
|
||||
|
||||
def test_register_cronjobs():
|
||||
assert not pub.cronjobs
|
||||
pub.register_cronjobs()
|
||||
assert 'apply_global_action_timeouts' in [x.function.func_name for x in pub.cronjobs]
|
||||
assert 'clean_sessions' in [x.function.func_name for x in pub.cronjobs]
|
||||
|
||||
|
@ -161,3 +166,9 @@ def test_import_config_zip():
|
|||
assert pub.cfg['sp'] == {'what': 'ever'}
|
||||
assert not isinstance(pub.cfg['language'], unicode)
|
||||
assert not isinstance(pub.cfg['whatever2'][-1]['c'], unicode)
|
||||
|
||||
def test_cron_command():
|
||||
pub = create_temporary_pub()
|
||||
with mock.patch('wcs.qommon.management.commands.cron.cron_worker') as cron_worker:
|
||||
call_command('cron')
|
||||
assert cron_worker.call_count == 1
|
||||
|
|
|
@ -14,21 +14,8 @@
|
|||
# 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 os
|
||||
import signal
|
||||
import time
|
||||
try:
|
||||
import prctl
|
||||
except ImportError:
|
||||
prctl = None
|
||||
import sys
|
||||
|
||||
parent_killed = False
|
||||
|
||||
def stop_cron_process(*args):
|
||||
global parent_killed
|
||||
parent_killed = True
|
||||
|
||||
class CronJob(object):
|
||||
hours = None
|
||||
minutes = None
|
||||
|
@ -57,7 +44,11 @@ def cron_worker(publisher, now):
|
|||
continue
|
||||
if job.minutes and not now[4] in job.minutes:
|
||||
continue
|
||||
publisher.install_lang(publisher.get_site_language())
|
||||
|
||||
class FakeRequest(object):
|
||||
language = publisher.get_site_language()
|
||||
|
||||
publisher.install_lang(FakeRequest())
|
||||
publisher.substitutions.reset()
|
||||
publisher.substitutions.feed(publisher)
|
||||
for extra_source in publisher.extra_sources:
|
||||
|
@ -66,48 +57,3 @@ def cron_worker(publisher, now):
|
|||
job.function(publisher)
|
||||
except:
|
||||
publisher.notify_of_exception(sys.exc_info(), context='[CRON]')
|
||||
publisher.cleanup()
|
||||
|
||||
def cron(publisher):
|
||||
global parent_killed
|
||||
app_dir = publisher.app_dir
|
||||
|
||||
last = time.localtime()
|
||||
while not parent_killed:
|
||||
now = time.localtime()
|
||||
if now[:5] != last[:5]:
|
||||
last = now
|
||||
for hostname in publisher.get_tenants():
|
||||
pid = os.fork()
|
||||
if pid == 0:
|
||||
publisher.app_dir = os.path.join(app_dir, hostname)
|
||||
cron_worker(publisher, now)
|
||||
os._exit(0)
|
||||
else:
|
||||
pid, exit_status = os.waitpid(pid, 0)
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
def spawn_cron(create_publisher):
|
||||
global cron_pid
|
||||
cron_pid = os.fork()
|
||||
if cron_pid == 0:
|
||||
# create a fake publisher
|
||||
pub = create_publisher()
|
||||
# set process name and title (this only works on Linux)
|
||||
if prctl:
|
||||
prctl.set_name(os.path.basename(sys.argv[0]) + ' [cron]')
|
||||
prctl.set_proctitle(os.path.basename(sys.argv[0]) + ' [cron]')
|
||||
signal.signal(signal.SIGTERM, stop_cron_process)
|
||||
cron(pub)
|
||||
pub.cleanup()
|
||||
sys.exit(0)
|
||||
else:
|
||||
signal.signal(signal.SIGTERM, kill_cron_process)
|
||||
#os.close(parent_fd)
|
||||
|
||||
def kill_cron_process(signum, frame):
|
||||
global cron_pid
|
||||
os.kill(cron_pid, signal.SIGTERM)
|
||||
sys.exit(0)
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
# 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 tempfile
|
||||
import time
|
||||
import os
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from qommon.publisher import get_publisher_class
|
||||
|
||||
from qommon.vendor import locket
|
||||
from qommon.cron import cron_worker
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Execute cronjobs'
|
||||
|
||||
def handle(self, verbosity, **options):
|
||||
with locket.lock_file(os.path.join(tempfile.gettempdir(), 'wcs-cron')):
|
||||
now = time.localtime()
|
||||
publisher_class = get_publisher_class()
|
||||
publisher_class.register_cron = True
|
||||
publisher = publisher_class.create_publisher()
|
||||
app_dir = publisher.app_dir
|
||||
for hostname in publisher.get_tenants():
|
||||
publisher.app_dir = os.path.join(app_dir, hostname)
|
||||
cron_worker(publisher, now)
|
|
@ -134,6 +134,7 @@ INSTALLED_APPS = (
|
|||
#'django.contrib.staticfiles',
|
||||
#'django.contrib.admin',
|
||||
'gadjo',
|
||||
'wcs.qommon',
|
||||
)
|
||||
|
||||
WCS_LEGACY_CONFIG_FILE = None
|
||||
|
|
Loading…
Reference in New Issue