celery / django-celery-results

Celery result back end with django
Other
668 stars 206 forks source link

date_created and date_done are almost exactly the same #368

Closed hsenot closed 1 year ago

hsenot commented 1 year ago

Hi there - am having the issue described in the title (results date_created almost the same as date_done) despite tasks taking several seconds or minutes).

I was hoping the date_created to be the creation date of the task, but I now realise it's the creation date of the task result itself (due to the auto_now_add flag in the model definition).

@bigott-a seems to have the same issue: https://github.com/celery/django-celery-results/issues/87#issuecomment-1041524877

In the link above, @steresi seems to imply that the TaskResult record is created first (setting the date_created correctly), then updated when the task is completed (setting the date_done correctly) - which is not what I observe. In my setup, the TaskResult record is only committed to the database after the task has completed.

Is there something I am missing here? Could difference in settings explain this? Thanks!

steresi commented 1 year ago

@hsenot I'm not working on that project anymore and unfortunately can't verify one way or the other from my side!

peterfarrell commented 1 year ago

@hsenot are you using track_started=True ? If not, then the Task Result is created at nearly the same time of done.

@shared_task(bind=True, name="default_task", track_started=True)
def default_task(self, files=None):
    start = str(timezone.now())
    sleep(10)
    end = str(timezone.now())
    return f"{start=}\n{end=}"

https://docs.celeryq.dev/en/stable/userguide/tasks.html#Task.track_started

hsenot commented 1 year ago

Hey PJ - that's what I needed. Wonderful answer, thank you so much!

Will attempt a PR on this project for the documentation to mention the celery settings track_started in relation to date_created.

Thanks again!

auvipy commented 1 year ago

I will be happy to accept any PR to improve the docs. thanks in advance

jaydeepmarvaniya commented 1 month ago

wonderful resolution, btw does adding this argument in shared task will make it slower?