I’m sorry if it wasn’t clear, I’ll try to explain the context better.
I’m using a library called “django celery results”, here is the link: GitHub - celery/django-celery-results: Celery result back end with django.
In simple terms, this library allows you to store Celery task results using the Django ORM, with a model called TaskResult
(as you can see, in my TradingSystem
model, the task_id
refers to this TaskResult
model, imported from django_celery_results.models
).
Here is the TaskResult
model:
class TaskResult(models.Model):
"""Task result/status."""
task_id = models.CharField(
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
unique=True,
verbose_name=_('Task ID'),
help_text=_('Celery ID for the Task that was run'))
periodic_task_name = models.CharField(
null=True, max_length=255,
verbose_name=_('Periodic Task Name'),
help_text=_('Name of the Periodic Task which was run'))
task_name = models.CharField(
null=True, max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
verbose_name=_('Task Name'),
help_text=_('Name of the Task which was run'))
task_args = models.TextField(
null=True,
verbose_name=_('Task Positional Arguments'),
help_text=_('JSON representation of the positional arguments '
'used with the task'))
task_kwargs = models.TextField(
null=True,
verbose_name=_('Task Named Arguments'),
help_text=_('JSON representation of the named arguments '
'used with the task'))
status = models.CharField(
max_length=50, default=states.PENDING,
choices=TASK_STATE_CHOICES,
verbose_name=_('Task State'),
help_text=_('Current state of the task being run'))
worker = models.CharField(
max_length=100, default=None, null=True,
verbose_name=_('Worker'), help_text=_('Worker that executes the task')
)
content_type = models.CharField(
max_length=128,
verbose_name=_('Result Content Type'),
help_text=_('Content type of the result data'))
content_encoding = models.CharField(
max_length=64,
verbose_name=_('Result Encoding'),
help_text=_('The encoding used to save the task result data'))
result = models.TextField(
null=True, default=None, editable=False,
verbose_name=_('Result Data'),
help_text=_('The data returned by the task. '
'Use content_encoding and content_type fields to read.'))
date_created = models.DateTimeField(
auto_now_add=True,
verbose_name=_('Created DateTime'),
help_text=_('Datetime field when the task result was created in UTC'))
date_done = models.DateTimeField(
auto_now=True,
verbose_name=_('Completed DateTime'),
help_text=_('Datetime field when the task was completed in UTC'))
traceback = models.TextField(
blank=True, null=True,
verbose_name=_('Traceback'),
help_text=_('Text of the traceback if the task generated one'))
meta = models.TextField(
null=True, default=None, editable=False,
verbose_name=_('Task Meta Information'),
help_text=_('JSON meta information about the task, '
'such as information on child tasks'))
objects = managers.TaskResultManager()
class Meta:
"""Table information."""
ordering = ['-date_done']
verbose_name = _('task result')
verbose_name_plural = _('task results')
# Explicit names to solve https://code.djangoproject.com/ticket/33483
indexes = [
models.Index(fields=['task_name'],
name='django_cele_task_na_08aec9_idx'),
models.Index(fields=['status'],
name='django_cele_status_9b6201_idx'),
models.Index(fields=['worker'],
name='django_cele_worker_d54dd8_idx'),
models.Index(fields=['date_created'],
name='django_cele_date_cr_f04a50_idx'),
models.Index(fields=['date_done'],
name='django_cele_date_do_f59aad_idx'),
]
def as_dict(self):
return {
'task_id': self.task_id,
'task_name': self.task_name,
'task_args': self.task_args,
'task_kwargs': self.task_kwargs,
'status': self.status,
'result': self.result,
'date_done': self.date_done,
'traceback': self.traceback,
'meta': self.meta,
'worker': self.worker
}
def __str__(self):
return '<Task: {0.task_id} ({0.status})>'.format(self)
To show you what’s going on, here is part of the endpoint of my Django REST Framework, where I create the strategy and the Celery task:
user_task = strategyExecutor.delay(data_serializer.validated_data)
[...]
celery_task = TaskResult.objects.get(task_id=user_task.id)
[...]
serializer.save(task_id = celery_task)
So, when I create a strategy, a celery task starts to run and when it finishes, I take the id stored in the TaskResult model, and I save the serializer for the strategy whit that celery task (same with the TradingSystem model).
So, when I create a strategy, a Celery task starts running, and when it finishes, I take the ID stored in the TaskResult
model, and I save the serializer for the strategy with that Celery task (same with the TradingSystem
model).
I hope this helps clarify the problem further.
Are you referring to other tasks in the Celery task queue?
Answer: Nope, I’m refering to the celery task in the TaskResult model associated with the strategy
Or are you talking about a model, and if so, what model are you talking about here?
Answer: Yes, I’m talking about a model of a library I’m using
If there’s a better way to handle this, can you suggest some guidelines or documentation? I was stuck with Celery tasks, and this library helped me a lot.