2014-12-29 15:55:57 +01:00
|
|
|
"""
|
|
|
|
This model defines the database models to handle Celery AsyncResults.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from django.db import models
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from model_utils.models import TimeStampedModel
|
2014-12-29 15:55:57 +01:00
|
|
|
|
|
|
|
from gnuviechadmin.celery import app
|
|
|
|
|
|
|
|
|
|
|
|
class TaskResultManager(models.Manager):
|
2023-02-18 22:46:48 +01:00
|
|
|
def create_task_result(self, creator, signature, notes=""):
|
2015-10-12 00:23:31 +02:00
|
|
|
sigstr = str(signature)
|
|
|
|
result = signature.apply_async()
|
|
|
|
taskresult = self.create(
|
2023-02-18 22:46:48 +01:00
|
|
|
task_id=result.task_id, creator=creator, signature=sigstr, notes=notes
|
|
|
|
)
|
2014-12-29 15:55:57 +01:00
|
|
|
return taskresult
|
|
|
|
|
|
|
|
|
2016-01-09 15:26:52 +01:00
|
|
|
class TaskResult(TimeStampedModel):
|
2023-02-18 22:46:48 +01:00
|
|
|
task_id = models.CharField(_("Task id"), max_length=36)
|
|
|
|
signature = models.TextField(_("Task signature"))
|
|
|
|
creator = models.TextField(_("Task creator"))
|
|
|
|
notes = models.TextField(_("Task notes"))
|
|
|
|
result = models.TextField(_("Task result"))
|
2014-12-29 15:55:57 +01:00
|
|
|
finished = models.BooleanField(default=False)
|
2023-02-18 22:46:48 +01:00
|
|
|
state = models.CharField(_("Task state"), max_length=16)
|
2014-12-29 15:55:57 +01:00
|
|
|
|
|
|
|
objects = TaskResultManager()
|
|
|
|
|
|
|
|
class Meta:
|
2023-02-18 22:46:48 +01:00
|
|
|
verbose_name = _("Task result")
|
|
|
|
verbose_name_plural = _("Task results")
|
|
|
|
ordering = ["created"]
|
2014-12-29 15:55:57 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-10-12 00:23:31 +02:00
|
|
|
return "{creator} ({task_id}): {finished}".format(
|
|
|
|
creator=self.creator,
|
2014-12-29 15:55:57 +01:00
|
|
|
task_id=self.task_id,
|
2023-02-18 22:46:48 +01:00
|
|
|
finished=_("yes") if self.finished else _("no"),
|
2014-12-29 15:55:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def fetch_result(self):
|
|
|
|
if not self.finished:
|
|
|
|
ar = app.AsyncResult(self.task_id)
|
|
|
|
self.state = ar.state
|
2015-10-12 00:23:31 +02:00
|
|
|
if ar.ready():
|
|
|
|
res = ar.get()
|
|
|
|
self.result = str(res)
|
|
|
|
self.finished = True
|