I'm planning on using django-celery-results
backend to track status and results of Celery tasks.
Is the django-celery-results
backend suitable to store status of task while it is running, or only after it has finished?
It's not clear when the TaskResult
model is first created (upon task creation, task execution, or completion?)
If it's created upon task creation, will the model status automatically be updated to RUNNING when the task is picked up, if task_track_started
option is set?
Can the TaskResult
instance be accessed within the task function?
Another question here appears to indicate so but doesn't mention task status update to RUNNING
To use the Celery Beat, we need to configure the Redis server in the Django projects settings.py file. As we have installed the Redis server on the local machine, we will point the URL to localhost. The CELERY_TIMEZONE variable must be correctly set to run the tasks at the intended times.
Celery uses a result backend to keep track of the tasks' states. In the previous tutorial, we saw how Celery works and how to integrate it into a Django application. In this tutorial, we are going to use the RPC (RabbitMQ/AMQP) result backend to store and retrieve the states of tasks.
About Taskresult, of course, you can access during execution task, you only need import:
from django_celery_results.models import TaskResult
With this you can access to model taskresult filter by task_id, task_name etc, This is official code, you can filter by any of this fields https://github.com/celery/django-celery-results/blob/master/django_celery_results/models.py
Example:
task = TaskResult.objects.filter(task_name=task_name, status='SUCCESS').first()
In addition, you can add some additional data necessaries for your task in models fields. When task is created and is in execution also is posible modify fields but in case that status is 'SUCCEES', you only can read it, status task is auto save by default.
Example:
task_result.meta = json.dumps({'help_text': {'date': '2020-11-30'}})
I recommend work with is_dict()
function, here you can watch models attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With