Skip to content

Recurring tasks

Register a task to run repeatedly with @app.cron. Give it either a standard 5-field cron expression (evaluated in UTC, or in the time zone you pass) or a fixed every= interval:

from datetime import timedelta
from ardiq import Ardiq
app = Ardiq(queue_name="jobs")
@app.cron("0 3 * * *") # every day at 03:00 UTC
async def nightly_report():
...
@app.cron(every=30) # every 30 seconds
async def heartbeat():
...
@app.cron(every=timedelta(minutes=5)) # every 5 minutes
async def poll_inbox():
...

@app.cron registers an ordinary task (it shows up in app.tasks and can still be enqueued by hand) plus a schedule. Recurring tasks fire while a worker is running.

The cron string has the standard five fields — minute hour day-of-month month day-of-week — evaluated in UTC unless you pass a time zone:

┌───────────── minute (0–59)
│ ┌─────────── hour (0–23)
│ │ ┌───────── day of month (1–31)
│ │ │ ┌─────── month (1–12)
│ │ │ │ ┌───── day of week (0–6, Sunday = 0; 7 also = Sunday)
│ │ │ │ │
* * * * *

Each field supports *, single values, lists (1,15), ranges (9-17), and steps (*/5, 0-30/10). As in classic cron, when both day-of-month and day-of-week are restricted, a task fires when either matches.

@app.cron("*/15 9-17 * * 1-5") # every 15 min, 09:00–17:00 UTC, Mon–Fri
async def business_hours_sync():
...

A schedule people read, such as a digest at 10:00 and 22:00 Madrid time, drifts by an hour twice a year if it is written in UTC. Pass tz and the expression is read on that zone’s wall clock:

@app.cron("0 10,22 * * *", tz="Europe/Madrid")
async def digest():
...

tz takes an IANA name or any tzinfo. Fire times are still stored in UTC, and ardiq status shows them in UTC.

The two daylight saving changes of the year need a rule, and ArdiQ follows cron’s:

  • A rate follows real time. An expression with * in the minute or hour field, such as */10 * * * * or 0 * * * *, keeps its interval through both changes. When the clocks go back it fires through the repeated hour as usual.
  • A time of day fires once a day. An expression with a fixed minute and hour, such as 30 2 * * *, fires at the first 02:30 when the clocks go back, not at the second. When the clocks go forward and 02:30 never happens, it fires as the gap ends, at 03:00, so a daily task is never skipped.

every= intervals take no time zone: they count real time.

every= takes a number of seconds (int or float) or a timedelta. Occurrences are aligned to the epoch, so every=30 fires at :00 and :30 of each minute. Use it for the sub-minute cadences cron can’t express:

@app.cron(every=0.5) # twice a second
async def sample():
...

@app.cron accepts the same per-task options as @app.task — name, max_retries, backoff_ms, timeout, priority — applied to every occurrence:

@app.cron("0 * * * *", priority="low", timeout=60)
async def hourly_cleanup():
...

A worker that is up but busy still enqueues every occurrence when it comes due. If its slots stay taken longer than one period, the occurrences queue up behind the running work and then run back to back once a slot frees.

For a task that only cares about the latest state, such as polling for new data, a health check or an analysis of the last hour, the older occurrences are wasted work. Pass coalesce=True and an occurrence is skipped while the previous one is still waiting to start:

@app.cron(every=timedelta(minutes=10), coalesce=True)
async def discover() -> int:
...

The occurrence that is waiting runs once a slot frees, and the schedule picks up again from the next fire time after that. At most one occurrence waits, and the periods skipped in between never run, as when the worker is down. An occurrence waiting for a retry counts as waiting too. A running one does not, so the next occurrence can queue behind it.

Each due occurrence is enqueued as an ordinary job with a deterministic id (cron:<name>:<fire-ms>), reusing the same delayed queue as delay_ms / schedule_ms. That means:

  • every occurrence has its own result, status, retries and timeout, like any task;
  • the same occurrence is never enqueued twice, even with several workers running (Redis SET NX dedup);
  • a worker that was down simply skips missed periods — it always schedules the next occurrence after now, never a backlog.

A worker re-checks each cron’s next fire time every cron_poll_s (default 1s, see Configuration). Recurring tasks don’t run under burst mode, which exits as soon as the queue drains.

ardiq status lists every cron with its next fire time and the outcome of its last run. The last run is recorded apart from its result, so it is still there after result_ttl_ms has expired the result. See the CLI reference.

Made bytay.dev