mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-04 19:08:12 +08:00
cached result backend
first version of a result backend using django's cache framework
This commit is contained in:
+26
-2
@@ -325,7 +325,11 @@ def monitor(result_queue, broker=None):
|
||||
if ack_id:
|
||||
broker.acknowledge(ack_id)
|
||||
# save the result
|
||||
save_task(task)
|
||||
if task.get('cached', False):
|
||||
save_cache(task, broker)
|
||||
else:
|
||||
save_task(task)
|
||||
# log the result
|
||||
if task['success']:
|
||||
logger.info(_("Processed [{}]").format(task['name']))
|
||||
else:
|
||||
@@ -384,7 +388,7 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
|
||||
|
||||
def save_task(task):
|
||||
"""
|
||||
Saves the task package to Django
|
||||
Saves the task package to Django or the cache
|
||||
"""
|
||||
# SAVE LIMIT < 0 : Don't save success
|
||||
if not task.get('save', Conf.SAVE_LIMIT > 0) and task['success']:
|
||||
@@ -409,6 +413,26 @@ def save_task(task):
|
||||
logger.error(e)
|
||||
|
||||
|
||||
def save_cache(task, broker):
|
||||
key = 'django_q:{}:results'.format(broker.list_key)
|
||||
timeout = task['cached']
|
||||
if timeout is True:
|
||||
timeout = None
|
||||
try:
|
||||
task_package = signing.SignedPackage.dumps(task)
|
||||
group = task.get('group', False)
|
||||
if group:
|
||||
group_list = broker.cache.get('{}:{}'.format(key, group)) or []
|
||||
group_list.append(task_package)
|
||||
broker.cache.set('{}:{}'.format(key, group), group_list, timeout)
|
||||
else:
|
||||
broker.cache.set('{}:{}'.format(key, task['id']),
|
||||
task_package,
|
||||
timeout)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
|
||||
def scheduler(broker=None):
|
||||
"""
|
||||
Creates a task from a schedule at the scheduled time and schedules next run
|
||||
|
||||
Reference in New Issue
Block a user