|
1 |
| -from celery.result import AsyncResult |
| 1 | +from celery.result import AsyncResult, EagerResult |
| 2 | +from django.conf import settings |
2 | 3 | from django.conf.urls import url
|
3 | 4 | from tastypie import resources, http, utils
|
4 | 5 |
|
| 6 | +# use for dev/test only! |
| 7 | +EAGER_RESULTS={} |
5 | 8 |
|
6 | 9 | class AsyncResourceMixin(object):
|
7 | 10 | def async_get_detail(self, request, **kwargs):
|
@@ -49,7 +52,10 @@ def async_state(self, request, task_id, **kwargs):
|
49 | 52 |
|
50 | 53 | Other methods are forbidden.
|
51 | 54 | """
|
52 |
| - task = AsyncResult(task_id) |
| 55 | + if not getattr(settings, 'CELERY_ALWAYS_EAGER'): |
| 56 | + task = AsyncResult(task_id) |
| 57 | + else: |
| 58 | + task = EAGER_RESULTS[task_id] |
53 | 59 | if request.method == 'GET':
|
54 | 60 | data = {
|
55 | 61 | 'state': task.state, 'id': task.id,
|
@@ -82,7 +88,11 @@ def async_result(self, request, task_id, **kwargs):
|
82 | 88 | If request is not ready (or doesn't exist - we can't tell this),
|
83 | 89 | return Http 404 Not Found.
|
84 | 90 | """
|
85 |
| - task = AsyncResult(task_id) |
| 91 | + # hack to allow local testing |
| 92 | + if not getattr(settings, 'CELERY_ALWAYS_EAGER'): |
| 93 | + task = AsyncResult(task_id) |
| 94 | + else: |
| 95 | + task = EAGER_RESULTS[task_id] |
86 | 96 | if task.ready():
|
87 | 97 | try:
|
88 | 98 | result = task.get()
|
@@ -163,6 +173,8 @@ def inner(self, request, **kwargs):
|
163 | 173 | return http.HttpNotImplemented()
|
164 | 174 |
|
165 | 175 | if isinstance(result, AsyncResult):
|
| 176 | + if isinstance(result, EagerResult): |
| 177 | + EAGER_RESULTS[result.id] = result |
166 | 178 | response = http.HttpAccepted()
|
167 | 179 | response['Location'] = self._build_reverse_url(
|
168 | 180 | 'api_async_state',
|
|
0 commit comments