forked from CGM_Public/pretix_original
Allow to reduce the interval of some cronjobs (#1753)
This commit is contained in:
@@ -6,6 +6,7 @@ from django_scopes import scopes_disabled
|
||||
|
||||
from pretix.api.models import ApiCall, WebHookCall
|
||||
from pretix.base.signals import periodic_task
|
||||
from pretix.helpers.periodic import minimum_interval
|
||||
|
||||
register_webhook_events = Signal(
|
||||
providing_args=[]
|
||||
@@ -19,11 +20,13 @@ instances.
|
||||
|
||||
@receiver(periodic_task)
|
||||
@scopes_disabled()
|
||||
@minimum_interval(minutes_after_success=12 * 60)
|
||||
def cleanup_webhook_logs(sender, **kwargs):
|
||||
WebHookCall.objects.filter(datetime__lte=now() - timedelta(days=30)).delete()
|
||||
|
||||
|
||||
@receiver(periodic_task)
|
||||
@scopes_disabled()
|
||||
@minimum_interval(minutes_after_success=12 * 60)
|
||||
def cleanup_api_logs(sender, **kwargs):
|
||||
ApiCall.objects.filter(created__lte=now() - timedelta(hours=24)).delete()
|
||||
|
||||
@@ -53,6 +53,7 @@ from pretix.base.signals import (
|
||||
)
|
||||
from pretix.celery_app import app
|
||||
from pretix.helpers.models import modelcopy
|
||||
from pretix.helpers.periodic import minimum_interval
|
||||
|
||||
error_messages = {
|
||||
'unavailable': _('Some of the products you selected were no longer available. '
|
||||
@@ -988,6 +989,7 @@ def expire_orders(sender, **kwargs):
|
||||
|
||||
@receiver(signal=periodic_task)
|
||||
@scopes_disabled()
|
||||
@minimum_interval(minutes_after_success=60)
|
||||
def send_expiry_warnings(sender, **kwargs):
|
||||
today = now().replace(hour=0, minute=0, second=0)
|
||||
days = None
|
||||
|
||||
@@ -18,6 +18,7 @@ from pretix.base.models import (
|
||||
)
|
||||
from pretix.celery_app import app
|
||||
|
||||
from ...helpers.periodic import minimum_interval
|
||||
from ..signals import periodic_task, quota_availability
|
||||
|
||||
|
||||
@@ -403,6 +404,7 @@ class QuotaAvailability:
|
||||
|
||||
|
||||
@receiver(signal=periodic_task)
|
||||
@minimum_interval(minutes_after_success=60)
|
||||
def build_all_quota_caches(sender, **kwargs):
|
||||
refresh_quota_caches.apply_async()
|
||||
|
||||
|
||||
61
src/pretix/helpers/periodic.py
Normal file
61
src/pretix/helpers/periodic.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import logging
|
||||
import uuid
|
||||
from functools import wraps
|
||||
|
||||
from django.core.cache import cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def minimum_interval(minutes_after_success, minutes_after_error=0, minutes_running_timeout=30):
|
||||
"""
|
||||
This is intended to be used as a decorator on receivers of the ``periodic_task`` signal.
|
||||
It stores the result in the task in the cache (usually redis) to ensure the receiver function
|
||||
isn't executed less than ``minutes_after_success`` after the last successful run and no less
|
||||
than ``minutes_after_error`` after the last failed run. There's also a simple locking mechanism
|
||||
implemented making sure the function is not called a second time while it is running, unless
|
||||
``minutes_running_timeout`` have passed. This locking mechanism is naive and not safe of
|
||||
race-conditions, it should not be relied upon.
|
||||
"""
|
||||
def deco(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
key_running = f'pretix_periodic_{f.__module__}.{f.__name__}_running'
|
||||
key_result = f'pretix_periodic_{f.__module__}.{f.__name__}_result'
|
||||
|
||||
running_val = cache.get(key_running)
|
||||
if running_val:
|
||||
# Currently running
|
||||
return
|
||||
|
||||
result_val = cache.get(key_result)
|
||||
if result_val:
|
||||
# Has run recently
|
||||
return
|
||||
|
||||
uniqid = str(uuid.uuid4())
|
||||
cache.set(key_running, uniqid, timeout=minutes_running_timeout * 60)
|
||||
try:
|
||||
retval = f(*args, **kwargs)
|
||||
except Exception as e:
|
||||
try:
|
||||
cache.set(key_result, 'error', timeout=minutes_after_error * 60)
|
||||
except:
|
||||
logger.exception('Could not store result')
|
||||
raise e
|
||||
else:
|
||||
try:
|
||||
cache.set(key_result, 'success', timeout=minutes_after_success * 60)
|
||||
except:
|
||||
logger.exception('Could not store result')
|
||||
return retval
|
||||
finally:
|
||||
try:
|
||||
if cache.get(key_running) == uniqid:
|
||||
cache.delete(key_running)
|
||||
except:
|
||||
logger.exception('Could not release lock')
|
||||
|
||||
return wrapper
|
||||
|
||||
return deco
|
||||
Reference in New Issue
Block a user