Know when Celery beat stops scheduling or your workers stop taking tasks
On this page
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 happens | What Stallbell sees | Alert |
|---|---|---|
| Beat crashes or never started after a deploy | No heartbeat by the expected time plus grace | Missed run |
| Workers are down, or lost their broker connection | Beat queues the heartbeat, but nothing runs it | Missed run |
| Every worker slot is busy with long tasks for more than a few minutes | The heartbeat waits, expires and is dropped | Missed run |
| The heartbeat task itself raises an error (for example, a bad URL in the environment) | No ping arrives | Missed run |
| Pings start again | A check-in after the missed run | Recovered |
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.
# 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
| Field | Value | Why |
|---|---|---|
| Kind | Scheduled job | A label only. Beat is a scheduler, so this sits with the other scheduled jobs. |
| How it runs | On a fixed interval | Beat sends the heartbeat task on a fixed interval, not at clock times. |
| Every | 5 minutes | Every 5 minutes, matching the beat entry. It's also the fastest the Free plan allows. |
| Grace period | 5 minutes | 5 minutes covers a busy queue delaying the heartbeat task. Silence for 10 minutes means beat or the workers are down. |
| Failures in a row | 1 | The heartbeat task does nothing, so a single failure is worth a look. |
| Stuck after | 10 minutes | Not 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:
- 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. - 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. - 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.
Related
- Know when Laravel's schedule:run stops running
- Know when a Raspberry Pi script stops after a reboot
- Docs: check-in fields
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.