[Django]-Celery task with a time_start attribute in 1970

34đź‘Ť

âś…

I found the answer to my own question by digging in the Celery and Kombu code: the time_start attribute of a task is computed by the kombu.five.monotonic function. (Ironically, the kombu code also refers to another StackOverflow question for reference) The timestamp returned by that function refers to a “monotonic” time computed by the clock_gettime system call.

As explained in the clock_gettime documentation, this monotonic time represents the time elapsed “since some unspecified starting point”. The purpose of this function is to make sure that time increases monotonically, despite changes of other clock values.

Thus, in order to obtain the real datetime at which the task was started, we just need to compare the time_start attribute to the current value of the monotonic clock:

>> from datetime import datetime
>> from time import time
>> import kombu.five
>> datetime.fromtimestamp(time() - (kombu.five.monotonic() - 9636801.218162088))
datetime.datetime(2013, 11, 20, 9, 55, 56, 193768)

EDIT: the time_start attribute reported by inspection is no longer monotonic : https://github.com/celery/celery/pull/3684 And it only took me four years to write a proper pull request 0:-)

👤Régis B.

Leave a comment