Repository navigation
Fix premature task completion timestamps - #522
sipa-echo-zaoa wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
I read through 3edcb23. Setting date_done only for ready states matches Celery itself: BaseBackend._get_result_meta() sets date_done for READY_STATES and None otherwise. So the meta from this backend would now agree with the other backends, and the migration tests cover both directions. A few things worth looking at before merging:
1. Long-running tasks that report progress can now expire while they run
Before this change, auto_now=True updated date_done on every save, including update_state() progress saves. A task that keeps reporting progress was therefore never older than result_expires. With the new fallback (date_done IS NULL AND date_created < cutoff), such a row is deleted by celery.backend_cleanup one result_expires after it was created, even while it's still running and updating. With the default of one day, any task that runs over a day is affected. When it finishes, store_result() goes through get_or_create() and creates a new row. That loses the original date_created and date_started, and anything that read the progress meta sees the row disappear in the meantime.
The docs note describes this, but I think it's a regression for a fairly common pattern. A few options:
- Keep a separate "last updated" timestamp (
auto_now=True) and expire unfinished rows by it. That keeps the current expiry behaviour and makesdate_donecorrect. - At least fall back to
date_startedbeforedate_created(Coalesce('date_started', 'date_created')). That helps tasks that wait in the queue a long time, but not tasks that run for a long time. - Don't expire unfinished rows here at all. That leaves abandoned rows, which is the reason for the fallback, so it's probably the weakest of the three.
2. ordering = ['-date_done'] now puts NULLs in a different place on each database
TaskResult.Meta.ordering and the admin both sort by -date_done. For a descending sort, PostgreSQL and Oracle put NULLs first, while MySQL and SQLite put them last. So on one database every unfinished task is now at the top of the admin list, and on another at the bottom. Their order among themselves isn't defined either. Something like ordering = [F('date_done').desc(nulls_first=True), '-date_created'], in this same migration, would make it the same everywhere. date_hierarchy = 'date_done' and the date_done list filter now also hide every unfinished task once a date is chosen. That can surprise people who use the admin to watch running tasks.
3. Worth a Changelog entry as a backward-incompatible change
date_done used to be always set. Code that does result.date_done - result.date_started, or sorts and filters on date_done expecting every row, will now get None or miss rows. The note in getting_started.rst is good, but the Changelog isn't updated. I think it should be listed under the next release as a behaviour change, together with the expiry change from point 1.
4. Migration cost on large tables
On MySQL/InnoDB, making a column nullable rebuilds the table. The RunPython step also runs an UPDATE ... WHERE NOT (status IN (...)) over the whole table, inside the same migration. Result tables are often large, so a line in the upgrade notes saying the migration can take a while on big tables would help.
Task results currently receive a
date_donetimestamp as soon as they are saved, including when Celery recordsSTARTEDor a progress update. A running task therefore appears to have a completion time, and later metadata saves change that time.Make
TaskResult.date_donenullable and set it explicitly when storingSUCCESS,FAILURE, orREVOKED. Pending, running, retrying, and custom progress states keep it unset; returning to an unfinished state clears it. Ordinary model saves preserve the completion timestamp.The migration clears misleading timestamps on existing unfinished results while preserving completed results. Its reverse fills null timestamps from
date_createdbefore restoring the non-null schema.Cleanup continues to expire completed results by
date_doneand falls back todate_createdfor unfinished results, so abandoned rows can still expire. This means an unfinished result older thanresult_expirescan be deleted even while its task is running. The documentation describes this behavior.Validation:
git diff --checkpass.Fixes #415.