Reference · Jobs and services

schedule

signature
schedule(str | int, fn, map | null=) -> null

What it does

Runs a function on the server on a timetable: every so often, given as a duration (seconds, or "30s", "5m", "1h", "1d", "2w", "1y", counted from 1970 in UTC so that "5m" runs at :00, :05, ...), or at the times of a cron line (minute hour day month weekday, in UTC, with *, 1-5, */15 and lists, or @hourly, @daily, @weekly, @monthly, @yearly). Written at the top of a program (or in a function the top level calls as it starts), which quillo run then serves. A job is named after its function, or by { name }; no two alike. It gives null. See Scheduled jobs.

Examples

in a program
fn clean_sessions() = db.exec("delete from sessions where expires < ?", now())fn nightly_report() { for t in db.tenants() { with_tenant(t, report) } }schedule("5m", clean_sessions)                  // every 5 minutes, on the clockschedule("0 3 * * *", nightly_report)           // cron: 03:00 UTCschedule("@daily", () => db.exec("delete from tokens where expires < ?", now()), { name: "expire-tokens" })schedule(60, ping, { name: "ping-minute" })      // seconds

Edge cases

Errors

A first argument that is neither a duration nor a cron line (a wrong unit, 0, a cron line without 5 fields, a field out of range, an unknown @name), a second that is not a function, a bad or repeated name, and options that are not a map are runtime errors when the program loads, not error values; so is a call once the program has started.

this one fails
schedule("soon", () => 1)
output
line 1: `schedule` takes a duration such as "5m" or a cron line such as "0 3 * * *", got "soon"
this one fails
schedule("0 9 * * mon-fri", () => 1)
output
line 1: `schedule`: the weekday field "mon-fri" is not a number from 0 to 7, a range, a list or a step
this one fails
fn clean() = 1schedule("5m", clean)schedule("1h", clean)
output
line 3: `schedule`: there is already a job named "clean"; give this one another with { name: ... }
in a program
fn clean() = 1route POST /admin/jobs = { schedule("5m", clean); "added" }// `schedule` adds a job as the program starts, at its top level: called here, once it// has started (in a route, a server fn, a job or a tick), the job would never run

Chaining

A job's function takes no arguments; give it what it needs by closing over it, and keep the work in a named function so the log names it:

in a program
fn remind(days: int) = () => db.query("select * from invoices where due < ?", now() + days * 86400000) | map(send_reminder)schedule("@daily", remind(3), { name: "remind-3d" })schedule("@daily", remind(7), { name: "remind-7d" })

See also

every, with_tenant, db.tenants, sleep, ready

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On schedule(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo