Source code for smarter.lib.celery_conf

"""
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. In the tool worker, ``smarter-worker-tools``, it
registers only the tool worker's own task (:data:`TOOL_WORKER_TASKS`).
"""

import logging

from celery import Celery
from celery.signals import worker_init, worker_process_init

APP = Celery("smarter")
"""The Celery app that the Smarter workers and Celery Beat run."""
APP.conf.task_protocol = 1
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
APP.config_from_object("django.conf:settings", namespace="CELERY")


[docs] def set_redbeat_conf(app: Celery) -> None: """ Set RedBeat's settings on the app under their own, unprefixed names. RedBeat decides whether ``redbeat_redis_url`` and ``redbeat_redis_options`` are set by looking for those exact keys in ``app.conf.keys()``. Under the ``CELERY`` namespace the keys are still ``CELERY_REDBEAT_REDIS_URL`` and ``CELERY_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_timeout`` is set the same way (issue #964). :param app: The Celery app that Celery Beat runs. :returns: None """ # pylint: disable=import-outside-toplevel from django.conf import settings app.conf.redbeat_redis_url = settings.CELERY_REDBEAT_REDIS_URL app.conf.redbeat_redis_options = settings.CELERY_REDBEAT_REDIS_OPTIONS app.conf.redbeat_lock_timeout = settings.CELERY_REDBEAT_LOCK_TIMEOUT
set_redbeat_conf(APP)
[docs] def set_result_backend(app: Celery) -> None: """ 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 of ``run_tool_call``, which the tool worker returns. The broker's url, as the deployment sets it, e.g. with ``DJANGO_CELERY_BROKER_URL``, is read here, after the settings' overrides, so that the results go to the same Redis. ``CELERY_TASK_IGNORE_RESULT`` keeps every other task's result out of it. :param app: The Celery app. :returns: None """ if not app.conf.result_backend: app.conf.update(result_backend=app.conf.broker_url)
set_result_backend(APP)
[docs] class HideTaskDataFilter(logging.Filter): """ Keep 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."""
[docs] def filter(self, record: logging.LogRecord) -> bool: """ Hide the task data in a record of Celery's, or drop the record. :param record: The record. :returns: False for the pool's record of a task's message; otherwise True. """ if record.name == "celery.pool": return not str(record.msg).startswith("TaskPool: Apply") context = record.args if isinstance(record.args, dict) else None for values in (context, getattr(record, "data", None)): if isinstance(values, dict): for key in ("return_value", "args", "kwargs"): if key in values: values[key] = self.HIDDEN return True
TASK_DATA_LOGGERS = ("celery.app.trace", "celery.pool") """The loggers whose records can hold a task's arguments or result."""
[docs] def hide_tool_calls_from_logs(**kwargs) -> None: """ In the tool worker, keep every task's arguments and result out of Celery's log. Connected to ``worker_init`` and ``worker_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. :param kwargs: The signal's arguments, unused. :returns: None """ # pylint: disable=import-outside-toplevel from smarter.common.conf import smarter_settings if not smarter_settings.database_unavailable: return for name in TASK_DATA_LOGGERS: task_logger = logging.getLogger(name) if not any(isinstance(f, HideTaskDataFilter) for f in task_logger.filters): task_logger.addFilter(HideTaskDataFilter())
worker_init.connect(hide_tool_calls_from_logs, weak=False) worker_process_init.connect(hide_tool_calls_from_logs, weak=False) 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).""" TOOL_WORKER_TASK_PACKAGES = ("smarter.apps.llmtool",) """The packages whose ``tasks`` modules the tool worker imports: those of :data:`TOOL_WORKER_TASKS`.""" CELERY_BUILTIN_TASK_PREFIX = "celery." """The prefix of Celery's own tasks, e.g. ``celery.chord_unlock``, which every worker keeps."""
[docs] def task_packages() -> list[str]: """ Name the packages whose ``tasks`` modules the Celery app imports. Every installed Django app's, except in the tool worker (``smarter_settings.database_unavailable``), which imports only :data:`TOOL_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. :returns: The packages' dotted names. """ # pylint: disable=import-outside-toplevel from django.apps import apps from smarter.common.conf import smarter_settings if smarter_settings.database_unavailable: return list(TOOL_WORKER_TASK_PACKAGES) packages = [config.name for config in apps.get_app_configs()] return packages
[docs] def keep_only_tool_worker_tasks(app: Celery) -> list[str]: """ Remove every task from the app's registry except :data:`TOOL_WORKER_TASKS` and Celery's own. Django's app loading imports many apps' ``tasks`` modules, through their receivers, whatever :func:`task_packages` says, and so registers tasks that deploy LLMClients, write DNS records or charge accounts. The tool worker must never run them. A worker rejects a message for a task that isn't in its registry ("Received unregistered task"), so after this it runs nothing but tool calls, whatever lands on its queue. :param app: The Celery app. :returns: The names of the tasks removed, sorted. :raises SmarterConfigurationError: If a task of :data:`TOOL_WORKER_TASKS` isn't registered: the worker would run nothing. """ # pylint: disable=import-outside-toplevel from smarter.common.exceptions import SmarterConfigurationError registry = app.tasks for name in TOOL_WORKER_TASKS: if name not in registry: raise SmarterConfigurationError(f"The tool worker's task {name} is not registered.") removed = [] for name in sorted(registry.keys()): if name in TOOL_WORKER_TASKS: continue if name.startswith(CELERY_BUILTIN_TASK_PREFIX): continue registry.unregister(name) removed.append(name) return removed
[docs] def restrict_tool_worker_tasks(**kwargs) -> None: """ In the tool worker, keep only its own tasks in the registry (:func:`keep_only_tool_worker_tasks`). Connected to ``worker_init``, which the worker sends before it prints its banner and before its pool's processes fork, so that they inherit the restricted registry. Only the tool worker (``smarter_settings.database_unavailable``): the other workers keep every task. :param kwargs: The signal's arguments, unused. :returns: None """ # pylint: disable=import-outside-toplevel from smarter.common.conf import smarter_settings if not smarter_settings.database_unavailable: return keep_only_tool_worker_tasks(APP)
worker_init.connect(restrict_tool_worker_tasks, weak=False)
[docs] def run_worker_startup_checks(**kwargs) -> None: """ 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); every other worker its database, cache and Kubernetes cluster. See :mod:`smarter.apps.infrastructure.startup`. :param kwargs: The signal's arguments, unused. :returns: None """ # pylint: disable=import-outside-toplevel from smarter.apps.infrastructure.startup import ( TOOL_WORKER_CHECKS, WORKER_CHECKS, run_startup_checks, ) from smarter.common.conf import smarter_settings checks = WORKER_CHECKS if smarter_settings.database_unavailable: checks = TOOL_WORKER_CHECKS run_startup_checks(checks)
worker_init.connect(run_worker_startup_checks, weak=False) # Load the task modules of the packages that task_packages() names, when the app is finalized. APP.autodiscover_tasks(task_packages) APP.conf.broker_connection_retry_on_startup = True