dvadmin3-celery 3.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_celery_results/__init__.py +36 -0
- django_celery_results/admin.py +83 -0
- django_celery_results/apps.py +15 -0
- django_celery_results/backends/__init__.py +4 -0
- django_celery_results/backends/cache.py +39 -0
- django_celery_results/backends/database.py +308 -0
- django_celery_results/managers.py +214 -0
- django_celery_results/migrations/__init__.py +0 -0
- django_celery_results/models.py +240 -0
- django_celery_results/urls.py +86 -0
- django_celery_results/utils.py +17 -0
- django_celery_results/views.py +53 -0
- dvadmin3_celery/__init__.py +0 -0
- dvadmin3_celery/admin.py +1 -0
- dvadmin3_celery/apps.py +7 -0
- dvadmin3_celery/fixtures/__init__.py +0 -0
- dvadmin3_celery/fixtures/init_menu.json +52 -0
- dvadmin3_celery/fixtures/initialize.py +29 -0
- dvadmin3_celery/settings.py +66 -0
- dvadmin3_celery/tasks.py +23 -0
- dvadmin3_celery/urls.py +27 -0
- dvadmin3_celery/views/__init__.py +0 -0
- dvadmin3_celery/views/crontab_schedule.py +24 -0
- dvadmin3_celery/views/interval_schedule.py +35 -0
- dvadmin3_celery/views/periodic_task.py +81 -0
- dvadmin3_celery/views/task.py +140 -0
- dvadmin3_celery/views/task_detail.py +48 -0
- dvadmin3_celery-3.1.3.dist-info/LICENSE +201 -0
- dvadmin3_celery-3.1.3.dist-info/METADATA +139 -0
- dvadmin3_celery-3.1.3.dist-info/RECORD +32 -0
- dvadmin3_celery-3.1.3.dist-info/WHEEL +5 -0
- dvadmin3_celery-3.1.3.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""Model managers."""
|
|
2
|
+
|
|
3
|
+
import warnings
|
|
4
|
+
from functools import wraps
|
|
5
|
+
from itertools import count
|
|
6
|
+
|
|
7
|
+
from celery.utils.time import maybe_timedelta
|
|
8
|
+
from django.conf import settings
|
|
9
|
+
from django.db import connections, models, router, transaction
|
|
10
|
+
|
|
11
|
+
from .utils import now
|
|
12
|
+
|
|
13
|
+
W_ISOLATION_REP = """
|
|
14
|
+
Polling results with transaction isolation level 'repeatable-read'
|
|
15
|
+
within the same transaction may give outdated results.
|
|
16
|
+
|
|
17
|
+
Be sure to commit the transaction for each poll iteration.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TxIsolationWarning(UserWarning):
|
|
22
|
+
"""Warning emitted if the transaction isolation level is suboptimal."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def transaction_retry(max_retries=1):
|
|
26
|
+
"""Decorate a function to retry database operations.
|
|
27
|
+
|
|
28
|
+
For functions doing database operations, adding
|
|
29
|
+
retrying if the operation fails.
|
|
30
|
+
|
|
31
|
+
Keyword Arguments:
|
|
32
|
+
max_retries (int): Maximum number of retries. Default one retry.
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
def _outer(fun):
|
|
36
|
+
|
|
37
|
+
@wraps(fun)
|
|
38
|
+
def _inner(*args, **kwargs):
|
|
39
|
+
_max_retries = kwargs.pop('exception_retry_count', max_retries)
|
|
40
|
+
for retries in count(0):
|
|
41
|
+
try:
|
|
42
|
+
return fun(*args, **kwargs)
|
|
43
|
+
except Exception: # pragma: no cover
|
|
44
|
+
# Depending on the database backend used we can experience
|
|
45
|
+
# various exceptions. E.g. psycopg2 raises an exception
|
|
46
|
+
# if some operation breaks the transaction, so saving
|
|
47
|
+
# the task result won't be possible until we rollback
|
|
48
|
+
# the transaction.
|
|
49
|
+
if retries >= _max_retries:
|
|
50
|
+
raise
|
|
51
|
+
return _inner
|
|
52
|
+
|
|
53
|
+
return _outer
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ResultManager(models.Manager):
|
|
57
|
+
"""Generic manager for celery results."""
|
|
58
|
+
|
|
59
|
+
def warn_if_repeatable_read(self):
|
|
60
|
+
if 'mysql' in self.current_engine().lower():
|
|
61
|
+
cursor = self.connection_for_read().cursor()
|
|
62
|
+
# MariaDB and MySQL since 8.0 have different transaction isolation
|
|
63
|
+
# variables: the former has tx_isolation, while the latter has
|
|
64
|
+
# transaction_isolation
|
|
65
|
+
if cursor.execute("SHOW VARIABLES WHERE variable_name IN "
|
|
66
|
+
"('tx_isolation', 'transaction_isolation');"):
|
|
67
|
+
isolation = cursor.fetchone()[1]
|
|
68
|
+
if isolation == 'REPEATABLE-READ':
|
|
69
|
+
warnings.warn(TxIsolationWarning(W_ISOLATION_REP.strip()))
|
|
70
|
+
|
|
71
|
+
def connection_for_write(self):
|
|
72
|
+
return connections[router.db_for_write(self.model)]
|
|
73
|
+
|
|
74
|
+
def connection_for_read(self):
|
|
75
|
+
return connections[self.db]
|
|
76
|
+
|
|
77
|
+
def current_engine(self):
|
|
78
|
+
try:
|
|
79
|
+
return settings.DATABASES[self.db]['ENGINE']
|
|
80
|
+
except AttributeError:
|
|
81
|
+
return settings.DATABASE_ENGINE
|
|
82
|
+
|
|
83
|
+
def get_all_expired(self, expires):
|
|
84
|
+
"""Get all expired results."""
|
|
85
|
+
return self.filter(date_done__lt=now() - maybe_timedelta(expires))
|
|
86
|
+
|
|
87
|
+
def delete_expired(self, expires):
|
|
88
|
+
"""Delete all expired results."""
|
|
89
|
+
with transaction.atomic(using=self.db):
|
|
90
|
+
self.get_all_expired(expires).delete()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class TaskResultManager(ResultManager):
|
|
94
|
+
"""Manager for :class:`~.models.TaskResult` models."""
|
|
95
|
+
|
|
96
|
+
_last_id = None
|
|
97
|
+
|
|
98
|
+
def get_task(self, task_id):
|
|
99
|
+
"""Get result for task by ``task_id``.
|
|
100
|
+
|
|
101
|
+
Keyword Arguments:
|
|
102
|
+
exception_retry_count (int): How many times to retry by
|
|
103
|
+
transaction rollback on exception. This could
|
|
104
|
+
happen in a race condition if another worker is trying to
|
|
105
|
+
create the same task. The default is to retry once.
|
|
106
|
+
|
|
107
|
+
"""
|
|
108
|
+
try:
|
|
109
|
+
return self.get(task_id=task_id)
|
|
110
|
+
except self.model.DoesNotExist:
|
|
111
|
+
if self._last_id == task_id:
|
|
112
|
+
self.warn_if_repeatable_read()
|
|
113
|
+
self._last_id = task_id
|
|
114
|
+
return self.model(task_id=task_id)
|
|
115
|
+
|
|
116
|
+
@transaction_retry(max_retries=2)
|
|
117
|
+
def store_result(self, content_type, content_encoding,
|
|
118
|
+
task_id, result, status,
|
|
119
|
+
traceback=None, meta=None,
|
|
120
|
+
periodic_task_name=None,
|
|
121
|
+
task_name=None, task_args=None, task_kwargs=None,
|
|
122
|
+
worker=None, using=None):
|
|
123
|
+
"""Store the result and status of a task.
|
|
124
|
+
|
|
125
|
+
Arguments:
|
|
126
|
+
content_type (str): Mime-type of result and meta content.
|
|
127
|
+
content_encoding (str): Type of encoding (e.g. binary/utf-8).
|
|
128
|
+
task_id (str): Id of task.
|
|
129
|
+
periodic_task_name (str): Celery Periodic task name.
|
|
130
|
+
task_name (str): Celery task name.
|
|
131
|
+
task_args (str): Task arguments.
|
|
132
|
+
task_kwargs (str): Task kwargs.
|
|
133
|
+
result (str): The serialized return value of the task,
|
|
134
|
+
or an exception instance raised by the task.
|
|
135
|
+
status (str): Task status. See :mod:`celery.states` for a list of
|
|
136
|
+
possible status values.
|
|
137
|
+
worker (str): Worker that executes the task.
|
|
138
|
+
using (str): Django database connection to use.
|
|
139
|
+
traceback (str): The traceback string taken at the point of
|
|
140
|
+
exception (only passed if the task failed).
|
|
141
|
+
meta (str): Serialized result meta data (this contains e.g.
|
|
142
|
+
children).
|
|
143
|
+
|
|
144
|
+
Keyword Arguments:
|
|
145
|
+
exception_retry_count (int): How many times to retry by
|
|
146
|
+
transaction rollback on exception. This could
|
|
147
|
+
happen in a race condition if another worker is trying to
|
|
148
|
+
create the same task. The default is to retry twice.
|
|
149
|
+
|
|
150
|
+
"""
|
|
151
|
+
fields = {
|
|
152
|
+
'status': status,
|
|
153
|
+
'result': result,
|
|
154
|
+
'traceback': traceback,
|
|
155
|
+
'meta': meta,
|
|
156
|
+
'content_encoding': content_encoding,
|
|
157
|
+
'content_type': content_type,
|
|
158
|
+
'periodic_task_name': periodic_task_name,
|
|
159
|
+
'task_name': task_name,
|
|
160
|
+
'task_args': task_args,
|
|
161
|
+
'task_kwargs': task_kwargs,
|
|
162
|
+
'worker': worker
|
|
163
|
+
}
|
|
164
|
+
obj, created = self.using(using).get_or_create(task_id=task_id,
|
|
165
|
+
defaults=fields)
|
|
166
|
+
if not created:
|
|
167
|
+
for k, v in fields.items():
|
|
168
|
+
setattr(obj, k, v)
|
|
169
|
+
obj.save(using=using)
|
|
170
|
+
return obj
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class GroupResultManager(ResultManager):
|
|
174
|
+
"""Manager for :class:`~.models.GroupResult` models."""
|
|
175
|
+
|
|
176
|
+
_last_id = None
|
|
177
|
+
|
|
178
|
+
def get_group(self, group_id):
|
|
179
|
+
"""Get result for group by ``group_id``.
|
|
180
|
+
|
|
181
|
+
Keyword Arguments:
|
|
182
|
+
exception_retry_count (int): How many times to retry by
|
|
183
|
+
transaction rollback on exception. This could
|
|
184
|
+
happen in a race condition if another worker is trying to
|
|
185
|
+
create the same task. The default is to retry once.
|
|
186
|
+
|
|
187
|
+
"""
|
|
188
|
+
try:
|
|
189
|
+
return self.get(group_id=group_id)
|
|
190
|
+
except self.model.DoesNotExist:
|
|
191
|
+
if self._last_id == group_id:
|
|
192
|
+
self.warn_if_repeatable_read()
|
|
193
|
+
self._last_id = group_id
|
|
194
|
+
return self.model(group_id=group_id)
|
|
195
|
+
|
|
196
|
+
@transaction_retry(max_retries=2)
|
|
197
|
+
def store_group_result(self, content_type, content_encoding,
|
|
198
|
+
group_id, result, using=None):
|
|
199
|
+
fields = {
|
|
200
|
+
'result': result,
|
|
201
|
+
'content_encoding': content_encoding,
|
|
202
|
+
'content_type': content_type,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if not using:
|
|
206
|
+
using = self.db
|
|
207
|
+
|
|
208
|
+
obj, created = self.using(using).get_or_create(group_id=group_id,
|
|
209
|
+
defaults=fields)
|
|
210
|
+
if not created:
|
|
211
|
+
for k, v in fields.items():
|
|
212
|
+
setattr(obj, k, v)
|
|
213
|
+
obj.save(using=self.db)
|
|
214
|
+
return obj
|
|
File without changes
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"""Database models."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from celery import states
|
|
6
|
+
from celery.result import GroupResult as CeleryGroupResult
|
|
7
|
+
from celery.result import result_from_tuple
|
|
8
|
+
from django.conf import settings
|
|
9
|
+
from django.db import models
|
|
10
|
+
from django.utils.translation import gettext_lazy as _
|
|
11
|
+
|
|
12
|
+
from . import managers
|
|
13
|
+
|
|
14
|
+
ALL_STATES = sorted(states.ALL_STATES)
|
|
15
|
+
TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TaskResult(models.Model):
|
|
19
|
+
"""Task result/status."""
|
|
20
|
+
|
|
21
|
+
task_id = models.CharField(
|
|
22
|
+
max_length=getattr(
|
|
23
|
+
settings,
|
|
24
|
+
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
|
|
25
|
+
255
|
|
26
|
+
),
|
|
27
|
+
unique=True,
|
|
28
|
+
verbose_name=_('Task ID'),
|
|
29
|
+
help_text=_('Celery ID for the Task that was run'))
|
|
30
|
+
periodic_task_name = models.CharField(
|
|
31
|
+
null=True, max_length=255,
|
|
32
|
+
verbose_name=_('Periodic Task Name'),
|
|
33
|
+
help_text=_('Name of the Periodic Task which was run'))
|
|
34
|
+
task_name = models.CharField(
|
|
35
|
+
null=True, max_length=getattr(
|
|
36
|
+
settings,
|
|
37
|
+
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
|
|
38
|
+
255
|
|
39
|
+
),
|
|
40
|
+
verbose_name=_('Task Name'),
|
|
41
|
+
help_text=_('Name of the Task which was run'))
|
|
42
|
+
task_args = models.TextField(
|
|
43
|
+
null=True,
|
|
44
|
+
verbose_name=_('Task Positional Arguments'),
|
|
45
|
+
help_text=_('JSON representation of the positional arguments '
|
|
46
|
+
'used with the task'))
|
|
47
|
+
task_kwargs = models.TextField(
|
|
48
|
+
null=True,
|
|
49
|
+
verbose_name=_('Task Named Arguments'),
|
|
50
|
+
help_text=_('JSON representation of the named arguments '
|
|
51
|
+
'used with the task'))
|
|
52
|
+
status = models.CharField(
|
|
53
|
+
max_length=50, default=states.PENDING,
|
|
54
|
+
choices=TASK_STATE_CHOICES,
|
|
55
|
+
verbose_name=_('Task State'),
|
|
56
|
+
help_text=_('Current state of the task being run'))
|
|
57
|
+
worker = models.CharField(
|
|
58
|
+
max_length=100, default=None, null=True,
|
|
59
|
+
verbose_name=_('Worker'), help_text=_('Worker that executes the task')
|
|
60
|
+
)
|
|
61
|
+
content_type = models.CharField(
|
|
62
|
+
max_length=128,
|
|
63
|
+
verbose_name=_('Result Content Type'),
|
|
64
|
+
help_text=_('Content type of the result data'))
|
|
65
|
+
content_encoding = models.CharField(
|
|
66
|
+
max_length=64,
|
|
67
|
+
verbose_name=_('Result Encoding'),
|
|
68
|
+
help_text=_('The encoding used to save the task result data'))
|
|
69
|
+
result = models.TextField(
|
|
70
|
+
null=True, default=None, editable=False,
|
|
71
|
+
verbose_name=_('Result Data'),
|
|
72
|
+
help_text=_('The data returned by the task. '
|
|
73
|
+
'Use content_encoding and content_type fields to read.'))
|
|
74
|
+
date_created = models.DateTimeField(
|
|
75
|
+
auto_now_add=True,
|
|
76
|
+
verbose_name=_('Created DateTime'),
|
|
77
|
+
help_text=_('Datetime field when the task result was created in UTC'))
|
|
78
|
+
date_done = models.DateTimeField(
|
|
79
|
+
auto_now=True,
|
|
80
|
+
verbose_name=_('Completed DateTime'),
|
|
81
|
+
help_text=_('Datetime field when the task was completed in UTC'))
|
|
82
|
+
traceback = models.TextField(
|
|
83
|
+
blank=True, null=True,
|
|
84
|
+
verbose_name=_('Traceback'),
|
|
85
|
+
help_text=_('Text of the traceback if the task generated one'))
|
|
86
|
+
meta = models.TextField(
|
|
87
|
+
null=True, default=None, editable=False,
|
|
88
|
+
verbose_name=_('Task Meta Information'),
|
|
89
|
+
help_text=_('JSON meta information about the task, '
|
|
90
|
+
'such as information on child tasks'))
|
|
91
|
+
|
|
92
|
+
objects = managers.TaskResultManager()
|
|
93
|
+
|
|
94
|
+
class Meta:
|
|
95
|
+
"""Table information."""
|
|
96
|
+
|
|
97
|
+
ordering = ['-date_done']
|
|
98
|
+
|
|
99
|
+
verbose_name = _('task result')
|
|
100
|
+
verbose_name_plural = _('task results')
|
|
101
|
+
|
|
102
|
+
# Explicit names to solve https://code.djangoproject.com/ticket/33483
|
|
103
|
+
indexes = [
|
|
104
|
+
models.Index(fields=['task_name'],
|
|
105
|
+
name='django_cele_task_na_08aec9_idx'),
|
|
106
|
+
models.Index(fields=['status'],
|
|
107
|
+
name='django_cele_status_9b6201_idx'),
|
|
108
|
+
models.Index(fields=['worker'],
|
|
109
|
+
name='django_cele_worker_d54dd8_idx'),
|
|
110
|
+
models.Index(fields=['date_created'],
|
|
111
|
+
name='django_cele_date_cr_f04a50_idx'),
|
|
112
|
+
models.Index(fields=['date_done'],
|
|
113
|
+
name='django_cele_date_do_f59aad_idx'),
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
def as_dict(self):
|
|
117
|
+
return {
|
|
118
|
+
'task_id': self.task_id,
|
|
119
|
+
'task_name': self.task_name,
|
|
120
|
+
'task_args': self.task_args,
|
|
121
|
+
'task_kwargs': self.task_kwargs,
|
|
122
|
+
'status': self.status,
|
|
123
|
+
'result': self.result,
|
|
124
|
+
'date_done': self.date_done,
|
|
125
|
+
'traceback': self.traceback,
|
|
126
|
+
'meta': self.meta,
|
|
127
|
+
'worker': self.worker
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
def __str__(self):
|
|
131
|
+
return '<Task: {0.task_id} ({0.status})>'.format(self)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class ChordCounter(models.Model):
|
|
135
|
+
"""Chord synchronisation."""
|
|
136
|
+
|
|
137
|
+
group_id = models.CharField(
|
|
138
|
+
max_length=getattr(
|
|
139
|
+
settings,
|
|
140
|
+
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
|
|
141
|
+
255),
|
|
142
|
+
unique=True,
|
|
143
|
+
verbose_name=_("Group ID"),
|
|
144
|
+
help_text=_("Celery ID for the Chord header group"),
|
|
145
|
+
)
|
|
146
|
+
sub_tasks = models.TextField(
|
|
147
|
+
help_text=_(
|
|
148
|
+
"JSON serialized list of task result tuples. "
|
|
149
|
+
"use .group_result() to decode"
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
count = models.PositiveIntegerField(
|
|
153
|
+
help_text=_(
|
|
154
|
+
"Starts at len(chord header) and decrements after each task is "
|
|
155
|
+
"finished"
|
|
156
|
+
)
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def group_result(self, app=None):
|
|
160
|
+
"""Return the :class:`celery.result.GroupResult` of self.
|
|
161
|
+
|
|
162
|
+
Arguments:
|
|
163
|
+
app (celery.app.base.Celery): app instance to create the
|
|
164
|
+
:class:`celery.result.GroupResult` with.
|
|
165
|
+
|
|
166
|
+
"""
|
|
167
|
+
return CeleryGroupResult(
|
|
168
|
+
self.group_id,
|
|
169
|
+
[result_from_tuple(r, app=app)
|
|
170
|
+
for r in json.loads(self.sub_tasks)],
|
|
171
|
+
app=app
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
class GroupResult(models.Model):
|
|
176
|
+
"""Task Group result/status."""
|
|
177
|
+
|
|
178
|
+
group_id = models.CharField(
|
|
179
|
+
max_length=getattr(
|
|
180
|
+
settings,
|
|
181
|
+
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
|
|
182
|
+
255
|
|
183
|
+
),
|
|
184
|
+
unique=True,
|
|
185
|
+
verbose_name=_("Group ID"),
|
|
186
|
+
help_text=_("Celery ID for the Group that was run"),
|
|
187
|
+
)
|
|
188
|
+
date_created = models.DateTimeField(
|
|
189
|
+
auto_now_add=True,
|
|
190
|
+
verbose_name=_("Created DateTime"),
|
|
191
|
+
help_text=_("Datetime field when the group result was created in UTC"),
|
|
192
|
+
)
|
|
193
|
+
date_done = models.DateTimeField(
|
|
194
|
+
auto_now=True,
|
|
195
|
+
verbose_name=_("Completed DateTime"),
|
|
196
|
+
help_text=_("Datetime field when the group was completed in UTC"),
|
|
197
|
+
)
|
|
198
|
+
content_type = models.CharField(
|
|
199
|
+
max_length=128,
|
|
200
|
+
verbose_name=_("Result Content Type"),
|
|
201
|
+
help_text=_("Content type of the result data"),
|
|
202
|
+
)
|
|
203
|
+
content_encoding = models.CharField(
|
|
204
|
+
max_length=64,
|
|
205
|
+
verbose_name=_("Result Encoding"),
|
|
206
|
+
help_text=_("The encoding used to save the task result data"),
|
|
207
|
+
)
|
|
208
|
+
result = models.TextField(
|
|
209
|
+
null=True, default=None, editable=False,
|
|
210
|
+
verbose_name=_('Result Data'),
|
|
211
|
+
help_text=_('The data returned by the task. '
|
|
212
|
+
'Use content_encoding and content_type fields to read.'))
|
|
213
|
+
|
|
214
|
+
def as_dict(self):
|
|
215
|
+
return {
|
|
216
|
+
'group_id': self.group_id,
|
|
217
|
+
'result': self.result,
|
|
218
|
+
'date_done': self.date_done,
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
def __str__(self):
|
|
222
|
+
return f'<Group: {self.group_id}>'
|
|
223
|
+
|
|
224
|
+
objects = managers.GroupResultManager()
|
|
225
|
+
|
|
226
|
+
class Meta:
|
|
227
|
+
"""Table information."""
|
|
228
|
+
|
|
229
|
+
ordering = ['-date_done']
|
|
230
|
+
|
|
231
|
+
verbose_name = _('group result')
|
|
232
|
+
verbose_name_plural = _('group results')
|
|
233
|
+
|
|
234
|
+
# Explicit names to solve https://code.djangoproject.com/ticket/33483
|
|
235
|
+
indexes = [
|
|
236
|
+
models.Index(fields=['date_created'],
|
|
237
|
+
name='django_cele_date_cr_bd6c1d_idx'),
|
|
238
|
+
models.Index(fields=['date_done'],
|
|
239
|
+
name='django_cele_date_do_caae0e_idx'),
|
|
240
|
+
]
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""URLs defined for celery.
|
|
2
|
+
|
|
3
|
+
* ``/$task_id/done/``
|
|
4
|
+
URL to :func:`~celery.views.is_successful`.
|
|
5
|
+
* ``/$task_id/status/``
|
|
6
|
+
URL to :func:`~celery.views.task_status`.
|
|
7
|
+
"""
|
|
8
|
+
import warnings
|
|
9
|
+
|
|
10
|
+
from django.conf import settings
|
|
11
|
+
from django.urls import path, register_converter
|
|
12
|
+
|
|
13
|
+
from . import views
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TaskPatternConverter:
|
|
17
|
+
"""Custom path converter for task & group id's.
|
|
18
|
+
|
|
19
|
+
They are slightly different from the built `uuid`
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
regex = r'[\w\d\-\.]+'
|
|
23
|
+
|
|
24
|
+
def to_python(self, value):
|
|
25
|
+
"""Convert url to python value."""
|
|
26
|
+
return str(value)
|
|
27
|
+
|
|
28
|
+
def to_url(self, value):
|
|
29
|
+
"""Convert python value into url, just a string."""
|
|
30
|
+
return value
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
register_converter(TaskPatternConverter, 'task_pattern')
|
|
34
|
+
|
|
35
|
+
urlpatterns = [
|
|
36
|
+
path(
|
|
37
|
+
'task/done/<task_pattern:task_id>/',
|
|
38
|
+
views.is_task_successful,
|
|
39
|
+
name='celery-is_task_successful'
|
|
40
|
+
),
|
|
41
|
+
path(
|
|
42
|
+
'task/status/<task_pattern:task_id>/',
|
|
43
|
+
views.task_status,
|
|
44
|
+
name='celery-task_status'
|
|
45
|
+
),
|
|
46
|
+
path(
|
|
47
|
+
'group/done/<task_pattern:group_id>/',
|
|
48
|
+
views.is_group_successful,
|
|
49
|
+
name='celery-is_group_successful'
|
|
50
|
+
),
|
|
51
|
+
path(
|
|
52
|
+
'group/status/<task_pattern:group_id>/',
|
|
53
|
+
views.group_status,
|
|
54
|
+
name='celery-group_status'
|
|
55
|
+
),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
if getattr(settings, 'DJANGO_CELERY_RESULTS_ID_FIRST_URLS', True):
|
|
59
|
+
warnings.warn(
|
|
60
|
+
"ID first urls depricated, use noun first urls instead."
|
|
61
|
+
"Will be removed in 2022.",
|
|
62
|
+
DeprecationWarning
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
urlpatterns += [
|
|
66
|
+
path(
|
|
67
|
+
'<task_pattern:task_id>/done/',
|
|
68
|
+
views.is_task_successful,
|
|
69
|
+
name='celery-is_task_successful'
|
|
70
|
+
),
|
|
71
|
+
path(
|
|
72
|
+
'<task_pattern:task_id>/status/',
|
|
73
|
+
views.task_status,
|
|
74
|
+
name='celery-task_status'
|
|
75
|
+
),
|
|
76
|
+
path(
|
|
77
|
+
'<task_pattern:group_id>/group/done/',
|
|
78
|
+
views.is_group_successful,
|
|
79
|
+
name='celery-is_group_successful'
|
|
80
|
+
),
|
|
81
|
+
path(
|
|
82
|
+
'<task_pattern:group_id>/group/status/',
|
|
83
|
+
views.group_status,
|
|
84
|
+
name='celery-group_status'
|
|
85
|
+
),
|
|
86
|
+
]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Utilities."""
|
|
2
|
+
# -- XXX This module must not use translation as that causes
|
|
3
|
+
# -- a recursive loader import!
|
|
4
|
+
|
|
5
|
+
from django.conf import settings
|
|
6
|
+
from django.utils import timezone
|
|
7
|
+
|
|
8
|
+
# see Issue celery/django-celery#222
|
|
9
|
+
now_localtime = getattr(timezone, 'template_localtime', timezone.localtime)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def now():
|
|
13
|
+
"""Return the current date and time."""
|
|
14
|
+
if getattr(settings, 'USE_TZ', False):
|
|
15
|
+
return now_localtime(timezone.now())
|
|
16
|
+
else:
|
|
17
|
+
return timezone.now()
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Views."""
|
|
2
|
+
from celery import states
|
|
3
|
+
from celery.result import AsyncResult, GroupResult
|
|
4
|
+
from celery.utils import get_full_cls_name
|
|
5
|
+
from django.http import JsonResponse
|
|
6
|
+
from kombu.utils.encoding import safe_repr
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def is_task_successful(request, task_id):
|
|
10
|
+
"""Return task execution status in JSON format."""
|
|
11
|
+
return JsonResponse({'task': {
|
|
12
|
+
'id': task_id,
|
|
13
|
+
'executed': AsyncResult(task_id).successful(),
|
|
14
|
+
}})
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def task_status(request, task_id):
|
|
18
|
+
"""Return task status and result in JSON format."""
|
|
19
|
+
result = AsyncResult(task_id)
|
|
20
|
+
state, retval = result.state, result.result
|
|
21
|
+
response_data = {'id': task_id, 'status': state, 'result': retval}
|
|
22
|
+
if state in states.EXCEPTION_STATES:
|
|
23
|
+
traceback = result.traceback
|
|
24
|
+
response_data.update({'result': safe_repr(retval),
|
|
25
|
+
'exc': get_full_cls_name(retval.__class__),
|
|
26
|
+
'traceback': traceback})
|
|
27
|
+
return JsonResponse({'task': response_data})
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def is_group_successful(request, group_id):
|
|
31
|
+
"""Return if group was successfull as boolean."""
|
|
32
|
+
results = GroupResult.restore(group_id)
|
|
33
|
+
|
|
34
|
+
return JsonResponse({
|
|
35
|
+
'group': {
|
|
36
|
+
'id': group_id,
|
|
37
|
+
'results': [
|
|
38
|
+
{'id': task.id, 'executed': task.successful()}
|
|
39
|
+
for task in results
|
|
40
|
+
] if results else []
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def group_status(request, group_id):
|
|
46
|
+
"""Return group id and its async results status & result in JSON format."""
|
|
47
|
+
result = GroupResult.restore(group_id)
|
|
48
|
+
retval = [
|
|
49
|
+
{"result": async_result.result, "status": async_result.status}
|
|
50
|
+
for async_result in result.results
|
|
51
|
+
]
|
|
52
|
+
response_data = {'id': group_id, 'results': retval}
|
|
53
|
+
return JsonResponse({'group': response_data})
|
|
File without changes
|
dvadmin3_celery/admin.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from django.contrib import admin
|
dvadmin3_celery/apps.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"name": "定时任务",
|
|
4
|
+
"icon": "iconfont icon-caijian",
|
|
5
|
+
"sort": 6,
|
|
6
|
+
"is_link": false,
|
|
7
|
+
"is_catalog": true,
|
|
8
|
+
"web_path": "/celeryManage",
|
|
9
|
+
"component": "",
|
|
10
|
+
"component_name": "",
|
|
11
|
+
"status": true,
|
|
12
|
+
"cache": true,
|
|
13
|
+
"visible": true,
|
|
14
|
+
"parent": null,
|
|
15
|
+
"children": [
|
|
16
|
+
{
|
|
17
|
+
"name": "任务管理",
|
|
18
|
+
"icon": "iconfont icon-caijian",
|
|
19
|
+
"sort": 1,
|
|
20
|
+
"is_link": false,
|
|
21
|
+
"is_catalog": false,
|
|
22
|
+
"web_path": "/taskManage",
|
|
23
|
+
"component": "plugins/dvadmin3-celery-web/src/taskManage/index",
|
|
24
|
+
"component_name": "taskManage",
|
|
25
|
+
"status": true,
|
|
26
|
+
"cache": true,
|
|
27
|
+
"visible": true,
|
|
28
|
+
"children": [],
|
|
29
|
+
"menu_button": [],
|
|
30
|
+
"menu_field": []
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"name": "任务日志",
|
|
34
|
+
"icon": "iconfont icon--chaifenhang",
|
|
35
|
+
"sort": 2,
|
|
36
|
+
"is_link": false,
|
|
37
|
+
"is_catalog": false,
|
|
38
|
+
"web_path": "/taskLog",
|
|
39
|
+
"component": "plugins/dvadmin3-celery-web/src/taskManage/component/taskLog/index",
|
|
40
|
+
"component_name": "taskLog",
|
|
41
|
+
"status": true,
|
|
42
|
+
"cache": true,
|
|
43
|
+
"visible": true,
|
|
44
|
+
"children": [],
|
|
45
|
+
"menu_button": [],
|
|
46
|
+
"menu_field": []
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"menu_button": [],
|
|
50
|
+
"menu_field": []
|
|
51
|
+
}
|
|
52
|
+
]
|