Monitor Celery beat periodic tasks

On this page

When Celery beat dies, no task is scheduled, so no task fails and your error tracker sees nothing. A heartbeat from the task itself notices the silence. Use Celery's task signals to ping when a periodic task starts, succeeds or fails.

How can a scheduled job fail silently on Celery beat?

  • Celery documents that you must make sure only a single scheduler is running for a schedule at a time; two beats double every task, and none means none.
  • Beat only sends tasks to the broker. A busy or stopped worker pool leaves tasks queued, and the schedule looks healthy on the beat side.
  • The periodic task schedule uses UTC by default. Changing the time zone setting changes when crontab entries fire.
  • With a database scheduler, time zone related settings do not reset the stored schedule when you change them, so an old schedule keeps running at the old times.

What are the limits of Celery beat?

LimitWhat the documentation says
Schedule sourceThe beat_schedule setting, or a database scheduler.
SchedulersOnly one scheduler instance may run for a given schedule.
Time zoneUTC by default; set timezone in the app configuration to change it.
crontab()Takes minute, hour, day_of_week and the other fields, for example crontab(hour=7, minute=30, day_of_week=1).

How do you add a CronMissed ping to Celery beat?

Connect to Celery's task signals so every periodic task reports without edits to the task code. The handler below pings /start before the task, the plain URL on success, and /fail with the exception on failure. Map task names to ping URLs, one check per periodic task.

celery_pings.py
import urllib.request
from celery.signals import task_prerun, task_success, task_failure

PINGS = {
    "app.tasks.nightly_report": "https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10",
}

def _ping(task_name, suffix="", body=None):
    base = PINGS.get(task_name)
    if base is None:
        return
    request = urllib.request.Request(base + suffix, data=body, method="POST" if body else "GET")
    try:
        urllib.request.urlopen(request, timeout=10).read()
    except OSError:
        pass  # a lost ping must never break the task

@task_prerun.connect
def started(sender=None, **kwargs):
    _ping(sender.name, "/start")

@task_success.connect
def succeeded(sender=None, **kwargs):
    _ping(sender.name)

@task_failure.connect
def failed(sender=None, exception=None, **kwargs):
    _ping(sender.name, "/fail", str(exception).encode()[-100000:])

Which check settings fit Celery beat?

Create one cron check per periodic task with the same crontab and time zone as beat_schedule. Set the grace to the task's normal queue wait plus its run time; five minutes is a common start. When the broker is busy the start ping tells you whether the task was late to start or late to finish.

How do you confirm the check works?

  1. Call the task by hand with delay() on a test worker and confirm the start and success pings.
  2. Stop the beat process and wait past the grace; the check goes down even though no task failed.
  3. Raise an exception in the task and confirm the /fail ping carries the message.

Sources

Questions and answers

Why not rely on Flower or the worker logs?
They report what workers did. When beat is down nothing reaches the workers, so there is nothing to report. A heartbeat expects the work and complains when it is absent.
Do I need a check for every periodic task?
Create one for each task whose absence would hurt. Tasks that run every few seconds are better watched by a queue-length metric than by a heartbeat.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring