DocsMonitor

Celery tasks

Know when a task fails, retries or is revoked, and when a periodic task quietly stops running. One call in your Celery app, no change to your tasks.

3 min readCelery 5+Python SDK

1Install the SDK with the Celery extra

bash
pip install "watchdock-errors[celery]"

Already using the Python SDK? Nothing new to install.

2Initialise in every worker process

Celery's prefork pool forks child processes to run tasks, and the SDK's delivery thread doesn't survive a fork. Initialise once at startup as usual, and again in each forked worker:

celery.py
import watchdock_errors
from celery.signals import worker_process_init
watchdock_errors.init(api_key="wdk_…", environment="production")
@worker_process_init.connect
def init_watchdock(**kwargs):
watchdock_errors.init(api_key="wdk_…", environment="production")

On Django, the init() in settings.py already covers the main process. Keep it, and add only the worker_process_init handler.

3Register the integration

Call register(app) once, after autodiscover_tasks(), so Celery has found every task first:

celery.py
from celery import Celery
from watchdock_errors.integrations.celery import register
app = Celery("myproject")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()
register(app)

Then restart your workers, and beat if you run it. The integration starts on worker startup.

4Verify it's reporting

Your worker log shows a line like this right after startup:

text
watchdock_errors: reported 12 celery task(s) to /api/v1/celery/registry/

What gets reported

  • Every registered task, with its beat schedule if it's periodic, once per worker at startup.
  • Failures, retries and revocations as they happen, with the exception, traceback, queue, worker and retry count.
  • A heartbeat on each run of a periodic task, so Watchdock notices when one stops.

Successful runs aren't reported, so the integration costs the same whether you run ten tasks a day or ten million.

When a periodic task goes quiet

This is the failure most tools miss: beat stops scheduling a task, or a worker stops picking it up, and nothing errors. Watchdock expects each periodic task to run on its schedule. If one stays silent for twice its interval (at least a minute), it opens an incident for that task. The incident closes itself the next time the task runs.

Task runs everyIncident after
30 seconds1 minute of silence
5 minutes10 minutes of silence
1 hour2 hours of silence

Silence detection works for fixed-interval schedules, like schedule=300 or timedelta(minutes=5). Tasks on a crontab() schedule are still registered and their failures reported, but they aren't checked for silence yet.

A task that hasn't run since it was registered isn't flagged until it has run at least once, so new tasks don't page anyone on deploy.

Troubleshooting

No log line after restart

Check that register(app) comes after autodiscover_tasks(), and that the workers were actually restarted.

The log says the integration fired before init()

init() didn't run in that process. Add it at startup, not only in the worker_process_init handler.

Failures are reported but a forked worker's aren't

That worker never called init(). Make sure the worker_process_init handler is in the module Celery imports at startup.