Smarter Celery Config Library
The Smarter platform’s Celery app.
It is configured from the Django settings whose names start with CELERY_, and finds the
tasks modules of every installed Django app. Each worker then registers only its own tasks
(ADR-030, rule 6): the tool worker, smarter-worker-tools, and the Extension worker only
TOOL_WORKER_TASKS, and every other worker only the tasks of the queues it consumes
(restrict_worker_tasks()).
- smarter.lib.celery_conf.APP = <Celery smarter>
The Celery app that the Smarter workers and Celery Beat run.
- smarter.lib.celery_conf.CELERY_BUILTIN_TASK_PREFIX = 'celery.'
The prefix of Celery’s own tasks, e.g.
celery.chord_unlock, which every worker keeps.
- class smarter.lib.celery_conf.HideTaskDataFilter(name='')[source]
Bases:
FilterKeep a task’s arguments and result out of Celery’s log records.
Celery logs each task’s success with its return value, and passes its arguments, and those of a failed task, in the record’s
data. In the tool worker, the arguments are a tool call, with its credentials, and the result is what the tool answered. Its pool also logs, at DEBUG, each task’s message, which holds the arguments: that record is dropped.- HIDDEN = '(hidden)'
What replaces a hidden value.
- smarter.lib.celery_conf.TASK_DATA_LOGGERS = ('celery.app.trace', 'celery.pool')
The loggers whose records can hold a task’s arguments or result.
('celery.app.trace', 'celery.pool')
- smarter.lib.celery_conf.TOOL_WORKER_TASKS = ('smarter.apps.llmtool.tasks.run_tool_call',)
The names of the only tasks that the tool worker,
smarter-worker-tools, runs (ADR-038).('smarter.apps.llmtool.tasks.run_tool_call', )
- smarter.lib.celery_conf.TOOL_WORKER_TASK_PACKAGES = ('smarter.apps.llmtool',)
those of
TOOL_WORKER_TASKS.('smarter.apps.llmtool', )
- Type:
The packages whose
tasksmodules the tool worker imports
- smarter.lib.celery_conf.configure_tool_worker(app)[source]
In the tool worker, consume its queue without declaring it, and take no remote control.
The tool worker authenticates to Redis as its own ACL user, which may touch only its queue’s keys, its results and a few cache keys (ADR-030, rule 8). Declaring a queue adds it to kombu’s routing set,
_kombu.binding.<queue>, which the app reads to route each task. A tool worker that could write that set could bind its queue’s exchange to another queue, such as the infrastructure queue, whose worker holds the platform’s credentials, and the app would deliver tool calls, with their credentials, there. So the queue isno_declare: the app declares it, when it sends the first tool call. The same goes for the Extension worker (issue #1077), which runs the same code on the Extensions queue: both queues are defined,no_declare, and each worker’s-Qpicks its own.Remote control (
celery inspect,celery control), mingle, gossip and heartbeats all go through fanout exchanges and pidbox queues, which the ACL user can’t reach. The worker turns off remote control here, and the others with--without-mingle --without-gossip --without-heartbeaton its command line. Its liveness probe looks for its process instead of pinging it.Only the tool worker (
smarter_settings.database_unavailable): the other workers are unchanged.- Parameters:
app (
Celery) – The Celery app.- Return type:
- Returns:
None
- smarter.lib.celery_conf.consumed_queue_names(app)[source]
Name the queues that this worker consumes, as its
-Qselected them.The worker selects its queues before it sends
worker_init. Without-Q, it consumes the app’s default queue.
- smarter.lib.celery_conf.hide_tool_calls_from_logs(**kwargs)[source]
In the tool worker, keep every task’s arguments and result out of Celery’s log.
Connected to
worker_initandworker_process_init, so that the pool’s processes have the filter too. Only the tool worker (smarter_settings.database_unavailable), whose tasks are tool calls: the other workers’ logs are unchanged.- Parameters:
kwargs – The signal’s arguments, unused.
- Return type:
- Returns:
None
- smarter.lib.celery_conf.keep_only_queue_tasks(app)[source]
Remove every task from the app’s registry except those of the queues this worker consumes, and Celery’s own.
ADR-030, rule 6. Every worker registers every task, because Django’s app loading imports every app’s
tasksmodules. Without this, the queue a task runs in would be routing, not a permission: a message for an infrastructure task, e.g.delete_default_api, pushed onto the operational queue would run insmarter-worker, and an operational task pushed onto the infrastructure queue would run with the Kubernetes Role and the IAM role. After this,smarter-workerruns only the operational queue’s tasks, andsmarter-worker-infrastructureonly the infrastructure queue’s.The queues are the worker’s own, from its
-Q(consumed_queue_names()), not from a setting, so the registry always matches what the worker takes.
- smarter.lib.celery_conf.keep_only_tasks(app, kept)[source]
Remove every task from the app’s registry except
keptand Celery’s own.A worker rejects a message for a task that isn’t in its registry (“Received unregistered task”), so after this it runs nothing but
kept, whatever lands on its queue.
- smarter.lib.celery_conf.keep_only_tool_worker_tasks(app)[source]
Remove every task from the app’s registry except
TOOL_WORKER_TASKSand Celery’s own.Django’s app loading imports many apps’
tasksmodules, through their receivers, whatevertask_packages()says, and so registers tasks that deploy LLMClients, write DNS records or charge accounts. The tool worker must never run them. The tool worker and the Extension worker keep this list, rather than the tasks of the queue they consume (keep_only_queue_tasks()), because the Extension worker runsrun_tool_callfrom the Extensions queue, which the task doesn’t declare.- Parameters:
app (
Celery) – The Celery app.- Return type:
- Returns:
The names of the tasks removed, sorted.
- Raises:
SmarterConfigurationError – If a task of
TOOL_WORKER_TASKSisn’t registered: the worker would run nothing.
- smarter.lib.celery_conf.queue_task_names(app, queue_names)[source]
Name the platform’s tasks that declare one of the queues, in their
@app.task(queue=...).A task that declares no queue is sent to the app’s default queue. Celery’s own tasks are left out: every worker keeps them.
- smarter.lib.celery_conf.restrict_worker_tasks(**kwargs)[source]
Keep only this worker’s own tasks in its registry (ADR-030, rule 6).
The tool worker and the Extension worker (
smarter_settings.database_unavailable) keep onlyTOOL_WORKER_TASKS(keep_only_tool_worker_tasks()). Every other worker keeps the tasks of the queues it consumes (keep_only_queue_tasks()), and logs how many it runs.Connected to
worker_init, which the worker sends after it selects its queues, and before it prints its banner and its pool’s processes fork, so that they inherit the restricted registry. Only workers send it: the app, beat and the Jobs keep every task, which they send.- Parameters:
kwargs – The signal’s arguments, unused.
- Return type:
- Returns:
None
- smarter.lib.celery_conf.run_worker_startup_checks(**kwargs)[source]
Check, when a worker starts, what it depends on, the way the settings check AWS.
Connected to
worker_init, so that the checks run once, in the worker’s main process, before its pool’s processes fork. The tool worker (smarter_settings.database_unavailable) checks its isolation (ADR-038) and its Redis user (ADR-030), then logs one line that sums them up, with the tasks it runs:restrict_worker_tasks(), connected before this, has already left only its own in the registry. Every other worker checks its database, cache and Kubernetes cluster. Seesmarter.apps.infrastructure.startup.- Parameters:
kwargs – The signal’s arguments, unused.
- Return type:
- Returns:
None
- smarter.lib.celery_conf.set_redbeat_conf(app)[source]
Set RedBeat’s settings on the app under their own, unprefixed names.
RedBeat decides whether
redbeat_redis_urlandredbeat_redis_optionsare set by looking for those exact keys inapp.conf.keys(). Under theCELERYnamespace the keys are stillCELERY_REDBEAT_REDIS_URLandCELERY_REDBEAT_REDIS_OPTIONS, so it found neither, warned that it falls back to the broker’s url and transport options, and RedBeat 2.5.0 removes that fallback (issue #945). Setting them unprefixed is what RedBeat finds.redbeat_lock_timeoutis set the same way (issue #964).- Parameters:
app (
Celery) – The Celery app that Celery Beat runs.- Return type:
- Returns:
None
- smarter.lib.celery_conf.set_result_backend(app)[source]
Store task results in the broker’s Redis, unless a result backend is configured.
The tool runner (
smarter.apps.llmtool.runner) waits for the result ofrun_tool_call, which the tool worker returns. The broker’s url, as the deployment sets it, e.g. withDJANGO_CELERY_BROKER_URL, is read here, after the settings’ overrides, so that the results go to the same Redis.CELERY_TASK_IGNORE_RESULTkeeps every other task’s result out of it.- Parameters:
app (
Celery) – The Celery app.- Return type:
- Returns:
None
- smarter.lib.celery_conf.task_packages()[source]
Name the packages whose
tasksmodules the Celery app imports.Every installed Django app’s, except in the tool worker (
smarter_settings.database_unavailable), which imports onlyTOOL_WORKER_TASK_PACKAGES. The tool worker runs third parties’ code, so it imports no more of the platform than it needs. It is called lazily, when the app is finalized, after Django is set up.