Skip to content

feature: add support for custom task ID generation - #9713

Open
jiisanda wants to merge 23 commits into
celery:mainfrom
jiisanda:custom-task-id-generator
Open

jiisanda wants to merge 23 commits into
celery:mainfrom
jiisanda:custom-task-id-generator

Conversation

@jiisanda

Copy link
Copy Markdown

Fixes Issue and Discussion: #9662

Description

Changes:

  • added 'task_id_generatorproperty to theSettings` class, allows users to configure a custom function for generating task_id
  • updated send_task method to use the custom generator with fallback to UUID when not specified or when error occur
  • added test case

Example file (examples/app/myapp.py) is temporarily updated to test generation of custom task_id.

About TaskID Generation

When implementing a custom task_id using task_id_generator, ensure it return globally unique values across all workers and processes; be aware of clock-related issues like clock drift, system time change if using timestamps; the generator must be fast and avoid blocking operations and consider lexicographic ordering for time-based IDs to help with ordering.

Common alternatives of UUID could be ULIDs, Snowflake IDs, or custom prefixed UUIDs for application-specific identification.

@Nusnus
Nusnus self-requested a review May 18, 2025 20:00
@auvipy
auvipy self-requested a review May 19, 2025 04:24

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make sure that the test passes and the CI REMAIN GREEN

@auvipy auvipy added this to the 5.7.0 milestone May 29, 2025
@codecov

codecov Bot commented May 29, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.34%. Comparing base (4981363) to head (43a62d7).
⚠️ Report is 466 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9713      +/-   ##
==========================================
+ Coverage   88.33%   88.34%   +0.01%     
==========================================
  Files         153      153              
  Lines       19761    19778      +17     
  Branches     2291     2293       +2     
==========================================
+ Hits        17456    17473      +17     
  Misses       2007     2007              
  Partials      298      298              
Flag Coverage Δ
unittests 88.32% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy

auvipy commented May 29, 2025

Copy link
Copy Markdown
Member

can you please also try using ulid to check if it works as well?

@jiisanda
jiisanda requested a review from auvipy May 29, 2025 19:27
@Nusnus
Nusnus marked this pull request as draft May 31, 2025 21:11
@jiisanda
jiisanda marked this pull request as ready for review June 5, 2025 17:20
@jiisanda

Copy link
Copy Markdown
Author

The checks are failing at Celery / Smoke (3.12, test_worker.py) and getting celery/backends/asynchronous.py:293: TimeoutError. This test seems unrelated to the changes made in the PR, which are scoped for custom task ID support. Is this a know flaky test, or could there be a regression I'm missing?? Would appreciate any feedback on this... @auvipy @Nusnus

@Nusnus

Nusnus commented Jun 11, 2025 •

Copy link
Copy Markdown
Member

@jiisanda

The checks are failing at Celery / Smoke (3.12, test_worker.py) and getting celery/backends/asynchronous.py:293: TimeoutError. This test seems unrelated to the changes made in the PR, which are scoped for custom task ID support. Is this a know flaky test, or could there be a regression I'm missing?? Would appreciate any feedback on this... @auvipy @Nusnus

Flaky. Passed after restart.

@Nusnus
Nusnus force-pushed the custom-task-id-generator branch from e3c4ee3 to 27f4de5 Compare July 15, 2025 14:35
@auvipy
auvipy requested a review from Copilot July 16, 2025 04:39

This comment was marked as resolved.

@auvipy
auvipy requested a review from Copilot September 13, 2025 06:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread celery/app/base.py Resolved
Comment thread examples/app/myapp.py Resolved
Comment thread celery/app/base.py Resolved
Comment thread celery/app/base.py Resolved

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example file (examples/app/myapp.py) is temporarily updated to test generation of custom task_id.

I was just about to ask if you want to add a new example showing how to use it, and/or documentation.

Thank you!

@jiisanda

Copy link
Copy Markdown
Author

Yes it will be helpful to add docs... may be in configuration docs... should I add it here in task config or somewhere else let me know...?

@jiisanda
jiisanda requested a review from Nusnus November 13, 2025 09:01
@Nusnus

Nusnus commented Nov 14, 2025

Copy link
Copy Markdown
Member

Yes it will be helpful to add docs... may be in configuration docs... should I add it here in task config or somewhere else let me know...?

Sounds good, yeah - thank you!

Will it be possible to add an example as well? 🙏

Comment thread celery/app/base.py
parent = have_parent = None
amqp = self.amqp
task_id = task_id or uuid()
if task_id is None:

@Nusnus Nusnus Nov 14, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add tests to make sure it works with all canvas primitives.
E.g, setting task_id_generator once and ensuring all primitives are using it, and another test with a canvas workflow, ensuring as well the generator is used as expected.

I’m not sure this (send_task) is enough to cover all cases

@jiisanda jiisanda May 19, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have extended task_id_generator to canvas primitives (group, chain, chord, Signature.freeze), for this i have added _new_task_id helper in canvas.py. Have also added tests covering this, @Nusnus let me know what you think.

I’ve also added the config docs with a ULID example. Is that enough, or is a more detailed explanation needed?

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to push back at this phase, but this is a sensitive change that requires more testing. An example will also be useful to let me QA different scenarios manually, but it’s not mandatory for the PR to be merged.

Thank you 🙏

@auvipy

auvipy commented Nov 16, 2025

Copy link
Copy Markdown
Member

this and uuid7(I have open pr for that) support in kombu should be postponed for a while for 5.7.

@jiisanda
jiisanda requested a review from Nusnus May 19, 2026 21:58
@qfarizon-qftech

qfarizon-qftech commented Nov 17, 2026 •

Copy link
Copy Markdown

We have a concrete use case for this feature: UUIDv7 task IDs in a Django/Celery API with database-backed job tracking and recurring Beat jobs.

I checked PR #9713 at head 43a62d7: normal send_task, apply_async, signature freezing and Beat publication use the configured generator, while direct Task.apply() and task_always_eager still call UUID4. This also affects eager chains and chord bodies.

With AI assistance, I prepared a small follow-up that reuses the existing _new_task_id(app) helper at those two execution paths, plus regression tests. Five new cases fail against the current PR and pass with the patch. The three relevant unit modules pass: 500 tests and 29 subtests, with two existing expected failures. Explicit IDs and eager retry identity are covered.

Would a contribution against #9713’s branch be useful? I can open a focused PR to the author’s fork or share the patch, whichever you prefer.

@auvipy

auvipy commented Nov 18, 2026

Copy link
Copy Markdown
Member

Would a contribution against #9713’s branch be useful? I can open a focused PR to the author’s fork or share the patch, whichever you prefer.

you can open a new PR based on the work from this PRs authors branch, and fix the conflicts, also I have an open PR on kombu which is related to this, I want to merge it early next month before releasing some celery v5.7.x bug fix releases

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants