124 lines
4.4 KiB
Python
124 lines
4.4 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2010 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 os
|
|
import time
|
|
from contextlib import contextmanager
|
|
|
|
from django.conf import settings
|
|
from django.utils.timezone import localtime
|
|
from quixote import get_publisher
|
|
|
|
|
|
class CronJob:
|
|
name = None
|
|
hours = None
|
|
minutes = None
|
|
weekdays = None
|
|
days = None
|
|
function = None
|
|
|
|
LONG_JOB_DURATION = 2 * 60 # 2 minutes
|
|
|
|
def __init__(self, function, name=None, hours=None, minutes=None, weekdays=None, days=None):
|
|
self.function = function
|
|
self.name = name
|
|
self.hours = hours
|
|
self.minutes = minutes
|
|
self.weekdays = weekdays
|
|
self.days = days
|
|
|
|
@contextmanager
|
|
def log_long_job(self, obj_description=None):
|
|
start = time.perf_counter()
|
|
yield
|
|
duration = time.perf_counter() - start
|
|
if duration > self.LONG_JOB_DURATION:
|
|
minutes = int(duration / 60)
|
|
if obj_description:
|
|
self.log('%s: running on "%s" took %d minutes' % (self.name, obj_description, minutes))
|
|
else:
|
|
self.log('long job: %s (took %s minutes)' % (self.name, minutes))
|
|
|
|
@staticmethod
|
|
def log(message, with_tenant=True):
|
|
tenant_prefix = ''
|
|
now = localtime()
|
|
if with_tenant:
|
|
tenant_prefix = '[tenant %s] ' % get_publisher().tenant.hostname
|
|
with open(os.path.join(get_publisher().APP_DIR, 'cron.log-%s' % now.strftime('%Y%m%d')), 'a+') as fd:
|
|
fd.write('%s %s%s\n' % (now.isoformat(), tenant_prefix, message))
|
|
|
|
def is_time(self, timetuple):
|
|
minutes = self.minutes
|
|
if minutes:
|
|
# will set minutes to an arbitrary value based on installation, this
|
|
# prevents waking up all jobs at the same time on a container farm.
|
|
minutes = [(x + ord(settings.SECRET_KEY[-1])) % 60 for x in minutes]
|
|
if self.days and timetuple[2] not in self.days:
|
|
return False
|
|
if self.weekdays and timetuple[6] not in self.weekdays:
|
|
return False
|
|
if self.hours and timetuple[3] not in self.hours:
|
|
return False
|
|
if minutes and timetuple[4] not in minutes:
|
|
return False
|
|
return True
|
|
|
|
|
|
def cron_worker(publisher, now, job_name=None, delayed_jobs=None):
|
|
if delayed_jobs:
|
|
jobs = delayed_jobs
|
|
else:
|
|
# reindex user and formdata if needed (should only be run once)
|
|
publisher.reindex_sql()
|
|
|
|
jobs = []
|
|
|
|
for job in publisher.cronjobs:
|
|
if job_name:
|
|
# a specific job name is asked, run it whatever
|
|
# the current time is.
|
|
if job.name != job_name:
|
|
continue
|
|
elif not job.is_time(now):
|
|
continue
|
|
jobs.append(job)
|
|
|
|
for job in jobs:
|
|
publisher.install_lang()
|
|
publisher.substitutions.reset()
|
|
publisher.substitutions.feed(publisher)
|
|
for extra_source in publisher.extra_sources:
|
|
publisher.substitutions.feed(extra_source(publisher, None))
|
|
try:
|
|
with job.log_long_job():
|
|
job.function(publisher, job=job)
|
|
except Exception as e:
|
|
publisher.record_error(exception=e, context='[CRON]', notify=True)
|
|
|
|
if not (job_name or delayed_jobs):
|
|
# assemble jobs that would have been triggered since start
|
|
delayed_jobs = set()
|
|
old_now_timestamp = time.mktime(now)
|
|
for timestamp in range(int(old_now_timestamp + 60), int(time.mktime(time.localtime())), 60):
|
|
timetuple = time.localtime(timestamp)
|
|
for job in publisher.cronjobs:
|
|
if job not in jobs and job.is_time(timetuple):
|
|
delayed_jobs.add(job)
|
|
if delayed_jobs:
|
|
cron_worker(publisher, now, delayed_jobs=delayed_jobs)
|