Repository navigation
Pass preload options to commands that do not accept them - #10837
Conversation
Celery adds those options to every sub-command, including celery.commands plugins such as Flower. Consume them and send user_preload_options before the callback runs. AI-assisted (Cursor)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10837 +/- ##
=======================================
Coverage 90.56% 90.56%
=======================================
Files 153 153
Lines 21367 21419 +52
Branches 2608 2621 +13
=======================================
+ Hits 19350 19398 +48
- Misses 1704 1707 +3
- Partials 313 314 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
I checked this out and ran the real celery command group with a preload option (--ini) against main and this branch. One built-in command and one plugin-style command (a click command whose callback has no **kwargs, like Flower's):
| Command | main | this PR |
|---|---|---|
celery -A proj flowerlike --ini f.ini --port 7777 |
TypeError: flowerlike() got an unexpected keyword argument 'ini' |
works, --port still passed, user_preload_options sent |
celery -A proj worker --ini w.ini: user_preload_options |
sent with {'ini': 'w.ini'} |
sent with {'ini': 'w.ini'} |
celery -A proj worker --ini w.ini: ini in app.Worker(**kwargs) |
'w.ini' |
missing |
So the plugin crash in #7894 is fixed. The new tests fail on main and pass here.
Concern: built-in commands no longer receive preload values
handle_preload_options now pops the preload names before calling the command. For worker, beat and events, those values used to flow through **kwargs into app.Worker() / app.Beat(), and from there into custom bootsteps (def __init__(self, worker, ini=None, **kwargs)). The docs describe preload options as "passed to all sub-commands", and the signal is only the documented way to read them. Anyone who reads them in a bootstep would silently get None after this change.
Stripping only the values a callback cannot accept keeps the fix and avoids the break:
def _accepts_keyword(fun, name):
try:
params = inspect.signature(fun).parameters
except (TypeError, ValueError): # pragma: no cover
return False
return name in params or any(
p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values())
def _take_preload_options(app, kwargs, fun=None):
"""Collect preload values, removing those the callback cannot accept."""
taken = {}
for name in _preload_option_names(app):
if name in kwargs:
if fun is not None and _accepts_keyword(fun, name):
taken[name] = kwargs[name]
else:
taken[name] = kwargs.pop(name)
return takenwith _take_preload_options(app, kwargs, f) in handle_preload_options and _take_preload_options(app, kwargs, fun) in consume_preload_options. With that:
workergetsiniagain;- the Flower-like command still works;
- a decorated command without
**kwargsstill works; - the signal is still sent once.
Of the new tests, only test_var_keyword_callback_receives_preload_options_only_via_signal would need to change, since it asserts the behaviour above.
Minor
command.params.extend(preload_options) runs on every invocation of the celery group, so in a long-lived process (or a test session) the same options are appended again each time. That was already the case before this PR. The new wrapping is guarded by _handles_preload_options, so it does not get worse, but a membership check before extend would be cheap to add while touching this code.
worker, beat, and events take those values in kwargs. Only a callback that would raise TypeError loses the name, and the signal still receives every preload value. AI-assisted (Cursor).
|
Preload values are still sent on
Drafted with Cursor. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Re-checked at 41e908e, following up on my earlier review.
I ran the real celery command group with the pyramid_celery_app test app (--ini / --ini-var as preload options) on main and on this branch. flowerlike is a plugin-style command whose callback has no **kwargs, like Flower's, plus its own --port option:
main this PR
flowerlike --ini f.ini --port 7777 TypeError: unexpected keyword 'ini' runs, port=7777, signal {'ini': 'f.ini', ...}
worker --ini w.ini signal sent, Worker(ini='w.ini') signal sent, Worker(ini='w.ini')
purge -f --ini p.ini signal sent once signal sent once
report --ini r.ini no signal signal sent once
second invoke in the same process TypeError signal sent once (wrapper not stacked)
- My earlier concern is resolved.
worker,beatandeventstake**kwargs, so they keep the preload values andapp.Worker()getsiniagain. Only a callback that can't accept a name loses it, and that callback still receives the value throughuser_preload_options. - The crash from #7894 is fixed. The plugin's own options are passed through unchanged, and built-ins already decorated with
handle_preload_optionsaren't wrapped a second time, so the signal is sent once. t/unit/app/test_preload_cli.pyandt/unit/bin/pass locally (61 passed), and CI is green.- The
Changelog.rstchange was removed as requested; the diff now touches onlycelery/bin/base.py,celery/bin/celery.pyand the tests.
Remaining suggestions (non-blocking)
- Built-in commands that never used
handle_preload_options, such asreport, now senduser_preload_optionstoo, becauseconsume_preload_optionswraps every command in the group. That seems like the right behaviour (preload options are documented as applying to all sub-commands), but it is a visible change for receivers, so it belongs in the docs note below. - On the earlier request for documentation: the "Preload options" section in
docs/userguide/extending.rstis the natural place for a short.. versionchanged:: 5.7note. It should say that preload options now work with commands installed through thecelery.commandsentry point (e.g. Flower), that a callback which can't accept an option receives it only throughuser_preload_options, and that the signal is now sent for every sub-command.
@Cherith1222 please recheck this two |
report never used handle_preload_options, but the umbrella command wraps every callback, so the signal is sent there too. The 5.7 note records that this is intentional. AI-assisted (Cursor).
|
Rechecked both points on this branch.
Drafted with Cursor. |
Fixes #7894.
Issue #7894 adds a preload option such as
--inithroughuser_options['preload'].celery -A example worker --ini app.iniworks.celery -A example flower --ini app.iniraisesTypeError: flower() got an unexpected keyword argument 'ini'. Celery adds those options to every subcommand, and Click passes them as keywords. Flower is acelery.commandsentry point, and its callback does not accept them.handle_preload_optionsnow removes the preload names, sendsuser_preload_options, and then calls the original function. A subcommand that already carries preload options, and whose callback is not already wrapped, gets the same handling before Click calls it. A plugin callback that uses@click.pass_contextis not givenctxagain as a positional argument.A command's own options, such as
--force, are still passed through. A callback that already accepts**kwargsdoes not receiveini. The preload signal is sent once if that wrapper runs again.On this commit (
dd8f43a53a840bf78f83eb43e1f5b7e1f5852dff), Windows, Python 3.13.3:pytest t/unit/app/test_preload_cli.py t/unit/bin/test_upgrade.py -k "not test_preload_options"was 13 passed, 2 deselected. Those new tests failed with theTypeErrorbefore the change. Pre-commit on this diff passed, including isort and mypy. Full tox, bandit, apicheck, configcheck, integration tests, and a real Flower install were not run.AI-assisted (Cursor).
Cherith reviewed this diff and confirmed it with the person who helped draft it.