Support for CELERY_ALWAYS_EAGER
parent
24e9d92af1
commit
0ff888df53
|
@ -18,3 +18,5 @@ from .development import *
|
|||
|
||||
SKIP_SOUTH_TESTS = True
|
||||
SOUTH_TESTS_MIGRATE = False
|
||||
|
||||
CELERY_ALWAYS_EAGER = True
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
from django.conf import settings
|
||||
|
||||
from .celery import app
|
||||
|
||||
|
||||
def _send_task(task, args, kwargs, **options):
|
||||
if settings.CELERY_ALWAYS_EAGER:
|
||||
return app.tasks[task].apply(args, kwargs, **options)
|
||||
return app.send_task(task, args, kwargs, **options)
|
||||
|
||||
|
||||
def defer(task: str, *args, **kwargs):
|
||||
"""Defer the execution of a task.
|
||||
|
||||
|
@ -17,7 +25,7 @@ def defer(task: str, *args, **kwargs):
|
|||
|
||||
:return: A future object.
|
||||
"""
|
||||
return app.send_task(task, args, kwargs, routing_key="transient.deferred")
|
||||
return _send_task(task, args, kwargs, routing_key="transient.deferred")
|
||||
|
||||
|
||||
def call_async(task: str, *args, **kwargs):
|
||||
|
@ -40,4 +48,4 @@ def apply_async(task: str, args=None, kwargs=None, **options):
|
|||
:param kwargs: Dict of keyword arguments for the task.
|
||||
:param options: Celery-specific options when running the task. See Celery docs on `apply_async`
|
||||
"""
|
||||
app.send_task(task, args, kwargs, **options)
|
||||
_send_task(task, args, kwargs, **options)
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
from unittest import mock
|
||||
|
||||
from taiga import celery
|
||||
from taiga.deferred import defer, call_async, apply_async
|
||||
|
||||
from ..utils import set_settings
|
||||
|
||||
|
||||
@set_settings(CELERY_ALWAYS_EAGER=False)
|
||||
@mock.patch("taiga.deferred.app")
|
||||
def test_defer(app):
|
||||
name = "task name"
|
||||
|
@ -13,6 +18,7 @@ def test_defer(app):
|
|||
app.send_task.assert_called_once_with(name, args, kwargs, routing_key="transient.deferred")
|
||||
|
||||
|
||||
@set_settings(CELERY_ALWAYS_EAGER=False)
|
||||
@mock.patch("taiga.deferred.app")
|
||||
def test_apply_async(app):
|
||||
name = "task name"
|
||||
|
@ -24,6 +30,7 @@ def test_apply_async(app):
|
|||
app.send_task.assert_called_once_with(name, args, kwargs)
|
||||
|
||||
|
||||
@set_settings(CELERY_ALWAYS_EAGER=False)
|
||||
@mock.patch("taiga.deferred.app")
|
||||
def test_call_async(app):
|
||||
name = "task name"
|
||||
|
@ -33,3 +40,10 @@ def test_call_async(app):
|
|||
call_async(name, *args, **kwargs)
|
||||
|
||||
app.send_task.assert_called_once_with(name, args, kwargs)
|
||||
|
||||
|
||||
@set_settings(CELERY_ALWAYS_EAGER=True)
|
||||
def test_task_invocation():
|
||||
celery.app.task(name="_test_task")(lambda: 1)
|
||||
|
||||
assert defer("_test_task").get() == 1
|
||||
|
|
Loading…
Reference in New Issue