Scheduler and Workers
How scheduled and delayed jobs reach a worker in django-cfg (RQ 3 native cron,
python manage.py rqcron), how to tell a dead rqcron from a busy worker, and
how to lay out queues so a short, latency-bound job is never stuck behind a long
one.
One rqcron per project
python manage.py rqcron # run the schedules
python manage.py rqcron --list # print the registered schedules as JSON and exit
python manage.py rqcron --poll 2 -l DEBUG| Option | Default | Meaning |
|---|---|---|
--list | off | Print schedules, count and not_registered as JSON, then exit |
--poll SECONDS | 1 | Seconds between loops: the most a delayed job is late |
-l, --logging-level | INFO | DEBUG, INFO, WARNING, ERROR, CRITICAL |
rqcron registers every RQScheduleConfig of DjangoRQConfig.get_all_schedules()
on the queue the entry names. There is no --queue and no config file argument.
Run exactly one per project. A second rqcron does no harm: it fails to take the
leader lock djangocfg:rq:cron:leader, logs rq cron: standby, leader is <name>,
enqueues nothing, and takes over when the leader stops.
An entry whose queue is not in RQ_QUEUES, or whose function cannot be imported,
is logged and listed under not_registered by rqcron --list; nothing is pushed
for it.
rqcron replaces rqscheduler. rq-scheduler is no longer a dependency, and
python manage.py rqscheduler now reaches django-rq’s own command, which fails
with “rq_scheduler not installed”. At first start rqcron removes the
registrations the project left under rq-scheduler in Redis, so the old schedules
stop.
Cron, interval, one-off
| Entry | When it runs |
|---|---|
cron="0 9 * * *" | At the cron times, evaluated in UTC |
interval=300 | Every 300 s after the previous run |
scheduled_time="2026-12-01T09:00:00" | Once at that time (a naive time is UTC) |
schedules=[
RQScheduleConfig(func="apps.reports.tasks.daily", cron="0 9 * * *", queue="default"),
RQScheduleConfig(func="apps.prices.tasks.refresh", interval=300, queue="high", timeout=120),
RQScheduleConfig(func="apps.ops.tasks.migrate_once", scheduled_time="2026-12-01T09:00:00"),
]- Every run is a fresh job with its own id. The schedule lives in the
rqcronprocess, not in a job hash, so a run’sresult_ttlis honoured and cannot delete the schedule. - Queue defaults apply. An entry without
timeout/result_ttlgets the queue’sdefault_timeout/default_result_ttl.timeoutis rq’sjob_timeout. - The schedule name is
job_idwhen set, elseschedule_<func>_<hash>. It keys the run historyrq:cron_job:<name>:jobs, which the django-rq 4.2 admin shows on the cron job page. For a one-off, it is also the job id. repeat=Nstops the entry after N runs.- A one-off runs once.
rqcronbooks it withenqueue_atwhen it starts leading; a marker key keeps a restart from booking it again.
Resume after a restart
On start, or when a standby takes over, cron and interval entries compute
their next run from the last run in their history. A deploy does not fire every
interval job at once; a daily interval job that ran an hour ago runs again in 23
hours.
A run is skipped while the previous copy is pending
Behind a busy worker, a scheduler that pushes on every due time fills the queue
with copies of the same job, which all run back to back once the worker is free.
rqcron checks the newest id in the entry’s history first:
| Previous copy | What the due run does |
|---|---|
| Still waiting in its queue | Skipped; the next run is booked |
| Running, heartbeat younger than 120 s | Skipped; the next run is booked |
| Running with a stale heartbeat (abandoned) | Pushes a new copy |
| Gone (finished or failed) | Pushes a new copy |
A skipped run logs one INFO line:
rq cron: skipped apps.sources.tasks.hot_sync (schedule_hot_sync_…): previous copy 3f2a… still queued in 'low'Nothing to configure.
Delayed jobs
queue.enqueue_in(...), queue.enqueue_at(...), Retry(interval=...) and the
one-off entries park jobs in rq:scheduled:<queue>. The same rqcron process
runs RQ’s RQScheduler for every queue in RQ_QUEUES and moves them into their
queues when due (within --poll seconds). Workers do not need --with-scheduler
for this. A worker that still runs with it does no harm: the per-queue locks let
both coexist.
from datetime import timedelta
import django_rq
django_rq.get_queue("default").enqueue_in(timedelta(minutes=10), "apps.mail.tasks.reminder", user_id=1)Cron dead or worker starved: rq_cron_status
A late scheduled job has two different causes, and restarting rqcron only
fixes one of them.
python manage.py rq_cron_status
python manage.py rq_cron_status --queue high --max-wait 60
python manage.py rq_cron_status --jsonstarved: cron_alive=True heartbeat=4s leader=…
queue high: depth=9 wait=312s
queue low: depth=41 wait=780s
busy 342863c3…: apps.catalog.titles.describe.tasks.describe_batch 790s (high,default,low)
reason: high waits 312s (9 queued); workers busy: apps.catalog.titles.describe.tasks.describe_batch 790s| Verdict | Meaning | Action |
|---|---|---|
ok | rqcron alive, no queue waits longer than --max-wait | None |
cron_dead | No heartbeat in rq:cron_schedulers, or the newest is older than --max-heartbeat-age (default 120 s) | Restart rqcron |
starved | rqcron alive, a queue waits longer than --max-wait (default 120 s); busy names the jobs holding the workers | Move the long job to its own worker (below) |
--queue is repeatable (--queue high --queue default); without it every queue is
judged. A --queue that is not in RQ_QUEUES fails with one line,
unknown RQ queue 'high'; declared: default, ..., and exit 1. Pass only the queues
the project declares.
--json prints verdict, cron_alive, heartbeat_age_sec, leader, queues,
busy, reason. The command exits 1 when the verdict is not ok. The same data
in Python:
from django_cfg.modules.django_rq.health.status import cron_status
st = cron_status(queue_names=["high"], max_wait=60)
st.verdict, st.reason, st.queues, st.busyThe queue-health monitor’s scheduler_lag is the most overdue cron run of the
leading rqcron, or the most overdue delayed job in rq:scheduled:<queue>.
A long job gets its own queue and worker
One RQ worker runs one job at a time across every queue it serves. Queue order
(rqworker high default low) only decides which job it takes next, never
interrupts the one it runs. So a 13-minute LLM batch on low holds the worker,
and a 30-second job on high waits 13 minutes.
Rules:
- A long job (LLM batch, import, crawl, anything that can pass a minute) gets its own queue and its own worker container.
- A latency-bound queue (heads, reapers, probes) gets a worker that serves only that queue. The shared worker may serve it too.
rqcronstays one service. Never scale it.
# api/settings/builders/background.py
DjangoRQConfig(
queues=[
RQQueueConfig(queue="high", default_timeout=180),
RQQueueConfig(queue="default"),
RQQueueConfig(queue="low"),
RQQueueConfig(queue="describe", default_timeout=3600),
],
schedules=[
RQScheduleConfig(func="apps.legal.tasks.issue_head", interval=30, queue="high"),
RQScheduleConfig(func="apps.titles.tasks.describe_batch", interval=600, queue="describe", timeout=3600),
],
)# docker-compose: one process per kind of work
rq-worker: # shared: everything short
command: ["python", "manage.py", "rqworker", "high", "default", "low"]
rq-high: # latency-bound only
command: ["python", "manage.py", "rqworker", "high"]
rq-describe: # the long job only
command: ["python", "manage.py", "rqworker", "describe"]
rq-cron: # exactly one per project
command: ["python", "manage.py", "rqcron"]
stop_grace_period: 20s
healthcheck:
test: ["CMD", "python", "-c", "import os,sys,time; sys.exit(time.time() - os.stat(os.environ.get('RQCRON_ALIVE_FILE', '/tmp/rqcron.alive')).st_mtime > 90)"]
interval: 60s
timeout: 10s
retries: 3
start_period: 120srqcron loops every second (--poll), so an interval below a minute and a
delayed job are on time.
Every loop, leader and standby alike, rqcron touches an alive file once Redis
has answered: /tmp/rqcron.alive, or env RQCRON_ALIVE_FILE, or --alive-file.
The healthcheck above fails when that file is older than 90 s. It is plain Python
with no Django boot and takes well under a second; the same string is
django_cfg.modules.django_rq.cron.HEALTHCHECK_CMD. Do not use rq_cron_status
as the healthcheck: it boots Django, which took 10-30 s on the production host,
longer than the healthcheck timeout, so the container went unhealthy while the
scheduler worked. Keep it for the diagnosis above.
Deploy overlap
During a deploy the old and the new rqcron may run together for a moment. Only
the one holding djangocfg:rq:cron:leader enqueues; the lock expires 30 s after
its holder stops refreshing it, and a standby that takes over resumes every entry
from its history, so nothing runs twice.
If a scheduled job is late after a deploy, check rq_cron_status first: in
the gptkino incident of 2026-11-12 the scheduler was healthy the whole time and the
single worker was busy.