|
1 | | -"""Database models.""" |
2 | | - |
3 | | -import json |
| 1 | +"""Abstract models.""" |
4 | 2 |
|
5 | 3 | from celery import states |
6 | | -from celery.result import GroupResult as CeleryGroupResult |
7 | | -from celery.result import result_from_tuple |
8 | 4 | from django.conf import settings |
9 | 5 | from django.db import models |
10 | 6 | from django.utils.translation import gettext_lazy as _ |
|
15 | 11 | TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES)) |
16 | 12 |
|
17 | 13 |
|
18 | | -class TaskResult(models.Model): |
19 | | - """Task result/status.""" |
20 | | - |
| 14 | +class AbstractTaskResult(models.Model): |
21 | 15 | task_id = models.CharField( |
22 | 16 | max_length=getattr( |
23 | 17 | settings, |
@@ -94,8 +88,8 @@ class TaskResult(models.Model): |
94 | 88 | class Meta: |
95 | 89 | """Table information.""" |
96 | 90 |
|
| 91 | + abstract = True |
97 | 92 | ordering = ['-date_done'] |
98 | | - |
99 | 93 | verbose_name = _('task result') |
100 | 94 | verbose_name_plural = _('task results') |
101 | 95 |
|
@@ -131,48 +125,74 @@ def __str__(self): |
131 | 125 | return '<Task: {0.task_id} ({0.status})>'.format(self) |
132 | 126 |
|
133 | 127 |
|
134 | | -class ChordCounter(models.Model): |
135 | | - """Chord synchronisation.""" |
| 128 | + |
| 129 | +class AbstractGroupResult(models.Model): |
| 130 | + """Task Group result/status.""" |
136 | 131 |
|
137 | 132 | group_id = models.CharField( |
138 | 133 | max_length=getattr( |
139 | 134 | settings, |
140 | 135 | "DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH", |
141 | | - 255), |
| 136 | + 255 |
| 137 | + ), |
142 | 138 | unique=True, |
143 | 139 | verbose_name=_("Group ID"), |
144 | | - help_text=_("Celery ID for the Chord header group"), |
| 140 | + help_text=_("Celery ID for the Group that was run"), |
145 | 141 | ) |
146 | | - sub_tasks = models.TextField( |
147 | | - help_text=_( |
148 | | - "JSON serialized list of task result tuples. " |
149 | | - "use .group_result() to decode" |
150 | | - ) |
| 142 | + date_created = models.DateTimeField( |
| 143 | + auto_now_add=True, |
| 144 | + verbose_name=_("Created DateTime"), |
| 145 | + help_text=_("Datetime field when the group result was created in UTC"), |
151 | 146 | ) |
152 | | - count = models.PositiveIntegerField( |
153 | | - help_text=_( |
154 | | - "Starts at len(chord header) and decrements after each task is " |
155 | | - "finished" |
156 | | - ) |
| 147 | + date_done = models.DateTimeField( |
| 148 | + auto_now=True, |
| 149 | + verbose_name=_("Completed DateTime"), |
| 150 | + help_text=_("Datetime field when the group was completed in UTC"), |
| 151 | + ) |
| 152 | + content_type = models.CharField( |
| 153 | + max_length=128, |
| 154 | + verbose_name=_("Result Content Type"), |
| 155 | + help_text=_("Content type of the result data"), |
| 156 | + ) |
| 157 | + content_encoding = models.CharField( |
| 158 | + max_length=64, |
| 159 | + verbose_name=_("Result Encoding"), |
| 160 | + help_text=_("The encoding used to save the task result data"), |
157 | 161 | ) |
| 162 | + result = models.TextField( |
| 163 | + null=True, default=None, editable=False, |
| 164 | + verbose_name=_('Result Data'), |
| 165 | + help_text=_('The data returned by the task. ' |
| 166 | + 'Use content_encoding and content_type fields to read.')) |
| 167 | + |
| 168 | + def as_dict(self): |
| 169 | + return { |
| 170 | + 'group_id': self.group_id, |
| 171 | + 'result': self.result, |
| 172 | + 'date_done': self.date_done, |
| 173 | + } |
| 174 | + |
| 175 | + def __str__(self): |
| 176 | + return f'<Group: {self.group_id}>' |
| 177 | + |
| 178 | + objects = managers.GroupResultManager() |
158 | 179 |
|
159 | | - def group_result(self, app=None): |
160 | | - """Return the GroupResult of self. |
| 180 | + class Meta: |
| 181 | + """Table information.""" |
| 182 | + |
| 183 | + ordering = ['-date_done'] |
161 | 184 |
|
162 | | - Arguments: |
163 | | - --------- |
164 | | - app (Celery): app instance to create the GroupResult with. |
165 | 185 |
|
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 | | - ) |
| 186 | + # Explicit names to solve https://code.djangoproject.com/ticket/33483 |
| 187 | + indexes = [ |
| 188 | + models.Index(fields=['date_created'], |
| 189 | + name='django_cele_date_cr_bd6c1d_idx'), |
| 190 | + models.Index(fields=['date_done'], |
| 191 | + name='django_cele_date_do_caae0e_idx'), |
| 192 | + ] |
173 | 193 |
|
174 | 194 |
|
175 | | -class GroupResult(models.Model): |
| 195 | +class AbstractGroupResult(models.Model): |
176 | 196 | """Task Group result/status.""" |
177 | 197 |
|
178 | 198 | group_id = models.CharField( |
@@ -226,10 +246,10 @@ def __str__(self): |
226 | 246 | class Meta: |
227 | 247 | """Table information.""" |
228 | 248 |
|
229 | | - ordering = ['-date_done'] |
230 | | - |
| 249 | + abstract = True |
231 | 250 | verbose_name = _('group result') |
232 | 251 | verbose_name_plural = _('group results') |
| 252 | + ordering = ['-date_done'] |
233 | 253 |
|
234 | 254 | # Explicit names to solve https://code.djangoproject.com/ticket/33483 |
235 | 255 | indexes = [ |
|
0 commit comments