Catch Celery beat jobs that point at unregistered tasks
PythonIf a beat job names a task the worker never registered, the worker silently drops it and the job never runs. These two pytest checks fail in CI instead of in production.
Celery
Django
pytest
Python
Python · 47 lines
copied 0 times
# Catch Celery beat jobs that point at tasks your worker never registered.
#
# Beat sends a task by its name. If that name isn't registered on the worker
# (a missing @shared_task, a renamed function, a moved module), the worker logs
# "Received unregistered task" and throws the message away. Nothing fails
# loudly, and the job just never runs. Put this test in your suite and CI
# catches it before production does.
import inspect
import pytest
from django.conf import settings
def registered_task_names(app) -> set[str]:
"""Every task name the worker would know about after autodiscovery."""
app.loader.import_default_modules()
return set(app.tasks.keys())
@pytest.fixture(scope="module")
def celery_app():
from config.celery import app # change to wherever your Celery app lives
return app
def test_every_beat_entry_names_a_registered_task(celery_app):
known = registered_task_names(celery_app)
missing = {
name: entry["task"]
for name, entry in settings.CELERY_BEAT_SCHEDULE.items()
if entry["task"] not in known
}
assert not missing, f"Beat entries the worker doesn't know: {missing}"
def test_every_beat_task_accepts_the_arguments_beat_sends(celery_app):
# A task that needs an argument beat doesn't send (or a datetime, which
# a JSON message can't carry) will fail every time it fires.
for name, entry in settings.CELERY_BEAT_SCHEDULE.items():
task = celery_app.tasks[entry["task"]]
signature = inspect.signature(task.run)
try:
signature.bind(*entry.get("args", ()), **entry.get("kwargs", {}))
except TypeError as exc:
pytest.fail(f"{name}: {exc}")
0
Fire
0
Learned something
0
Saved me time
0
Mind-blown
Sign in to react and star