Know when Celery beat stops scheduling or your workers stop taking tasks

Updated 4 min read

On this page
  1. The problem
  2. What goes wrong silently
  3. Setup
  4. Recommended settings
  5. When the alert fires, check this first
  6. Related
  7. FAQ

Stallbell gets a ping from a Celery worker every 5 minutes, and emails you if the pings stop because beat died or the workers stopped consuming. On a fixed interval, Free checks as often as every 15 minutes, so on Free set the schedule to 900 seconds. Pro, Scale and beta accounts can use 5 minutes.

The problem

Celery beat and the workers fail quietly. Beat crashes after a deploy or isn't restarted with the workers, and nothing gets scheduled. Or the workers stay up but every slot is stuck on a long task, so new work sits in the queue. ps shows the processes running and no errors are logged, so you only find out when the nightly emails or invoices don't go out.

This page is a ready-made setup for one scenario.

What goes wrong silently

What Stallbell catches
What happensWhat Stallbell seesAlert
Beat crashes or never started after a deployNo heartbeat by the expected time plus graceMissed run
Workers are down, or lost their broker connectionBeat queues the heartbeat, but nothing runs itMissed run
Every worker slot is busy with long tasks for more than a few minutesThe heartbeat waits, expires and is droppedMissed run
The heartbeat task itself raises an error (for example, a bad URL in the environment)No ping arrivesMissed run
Pings start againA check-in after the missed runRecovered
Apply this template

Creates a monitor called celery-beat-and-workers with the settings below. You can change everything later.

Setup

Add a tiny task and a beat entry. The task runs on a worker, so a ping proves two things at once: beat queued it, and a worker picked it up.

Python · tasks.py
# tasks.py (or wherever your Celery app lives)
import os
import urllib.parse
import urllib.request

from celery import Celery

app = Celery("proj", broker=os.environ.get("CELERY_BROKER_URL", "redis://localhost:6379/0"))

STALLBELL_URL = os.environ["STALLBELL_HEARTBEAT_URL"]  # https://stallbell.com/p/TOKEN

@app.task(name="stallbell.heartbeat", bind=True, ignore_result=True)
def heartbeat(self):
    # Runs on a worker, so a ping proves beat queued it and a worker picked it up.
    msg = urllib.parse.urlencode({"msg": f"heartbeat from {self.request.hostname}"})
    urllib.request.urlopen(f"{STALLBELL_URL}?{msg}", timeout=10).read()

app.conf.beat_schedule = {
    "stallbell-heartbeat": {
        "task": "stallbell.heartbeat",
        "schedule": 300.0,  # every 5 minutes; on Free use 900.0
        # Drop heartbeats that waited in the queue while workers were down,
        # so a recovering worker doesn't send a burst of stale pings.
        "options": {"expires": 240},  # on Free use 840
    },
}

Set STALLBELL_HEARTBEAT_URL to the ping URL from the monitor page, then restart beat and the workers. In Django projects that read Celery settings with the CELERY_ prefix, the same entry goes in CELERY_BEAT_SCHEDULE.

Each ping carries the worker's name in msg, so Recent runs on the monitor page shows which worker ran the last heartbeat. Click a run to open the Run drawer with its time and message.

If you use several queues, route the heartbeat to the queue you care about by adding "queue": "emails" next to expires. A heartbeat on the default queue says nothing about a worker that only consumes emails. Use one monitor per queue.

Recommended settings
FieldValueWhy
KindScheduled jobA label only. Beat is a scheduler, so this sits with the other scheduled jobs.
How it runsOn a fixed intervalBeat sends the heartbeat task on a fixed interval, not at clock times.
Every5 minutesEvery 5 minutes, matching the beat entry. It's also the fastest the Free plan allows.
Grace period5 minutes5 minutes covers a busy queue delaying the heartbeat task. Silence for 10 minutes means beat or the workers are down.
Failures in a row1The heartbeat task does nothing, so a single failure is worth a look.
Stuck after10 minutesNot used by the snippet, which sends no start ping. 10 minutes keeps it tight if you add one.

When the alert fires, check this first

The heartbeat can't tell you which half broke, so check in this order:

  1. Are the workers answering? Run celery -A proj inspect ping (Redis and RabbitMQ brokers). No reply means the workers are down or can't reach the broker.
  2. Are they all busy? If they answer, run celery -A proj inspect active. If every slot holds a long-running task, the heartbeat waited past its 240-second expiry and was dropped. Your real tasks are waiting too.
  3. Is beat sending? If the workers are free, look at beat's log for Scheduler: Sending due task stallbell-heartbeat. If that line stopped, beat is the problem.

FAQ

Can Stallbell tell whether beat or the workers stopped?

No. Both stop the heartbeat, so both look the same: a Missed run. Run celery -A proj inspect ping first. If the workers answer, beat is the likely cause.

Does this work with django-celery-beat?

Yes. With the database scheduler, add the same task as a periodic task in the Django admin: task stallbell.heartbeat, every 5 minutes with an expiry of 240 seconds. On Free, use every 15 minutes with an expiry of 840 seconds.

Why does the beat entry set expires?

Without it, heartbeats that queue up while workers are down all run when a worker comes back. An hour of downtime is 12 queued pings in a few seconds, which also trips Stallbell's Runaway loop warning (more than 6 check-ins in 10 minutes for this monitor). In our test, a restarted worker sent 9 stale pings within 5 seconds without expires, and 2 with it.

Will a Stallbell outage break my Celery tasks?

No. Only the heartbeat task calls Stallbell. If the call fails, that one task fails and your other tasks carry on.