Skip to content
Snippets Groups Projects
Verified Commit 5cfcefd1 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Add wrapper to mark an optional Celery task

This allows code that does not need Celery to also be run synchronously, e.g.
if the administrator does not want or cannot run Celery.
parent 47e24bd2
No related branches found
No related tags found
1 merge request!123Resolve "Background Tasks / Task Broker"
Pipeline #662 failed
......@@ -102,3 +102,22 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool:
return False
return getattr(obj, "person", None) is not None
def celery_optional(orig: Callable) -> Callable:
""" Decorator that makes Celery optional for a function.
If Celery is configured and available, it wraps the function in a Task
and calls its delay method when invoked; if not, it leaves it untouched
and it is executed synchronously.
"""
if hasattr(settings, "CELERY_RESULT_BACKEND"):
from ..celery import app # noqa
task = app.task(orig)
def wrapped(*args, **kwargs):
task.delay(*args, **kwargs)
return wrapped
else:
return orig
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment