Sample logic
logic.py
@shared_task
def run_create_or_update_google_creative():
return create_or_update_google_creative()
def create_or_update_google_creative() :
# do some logic
def run_db_sinc():
result = run_create_or_update_google_creative.delay()
job = CeleryJobResult(job_id=result.task_id, status=result.status)
job.save()
return 201, job.id
There is such a structure to the celery task call logic. First I call run_db_sinc
, a new celery task is generated and I immediately get the task_id
value which I save in the database and send as a response to the frontend. As long as the status is PENDING the frontend will go through the endpoint to the database and find the task_id status.
My question is how do I know that the task has completed and the status has changed to SUCCESS? At what point and how to do it ? I know that it is possible to use similar function
from celery.result import AsyncResult
def get_task_status(task_id):
task = AsyncResult(task_id)
if task.status = 'SUCCESS': # or task ended already
job = CeleryJobResult.objects.get(job_id=task_id)
job.status=task.status
job.save()
return task.status
But I can't understand at what point in time and where in my code to call it.