Jobs
TypeScript and Python functions with retries, durable steps, sleeps, schedules and table triggers.
/jobs/v1 runs your code on the server. Deploy a TypeScript, JavaScript or Python function, then start jobs
of it: by a call, after a delay, on a schedule, when a table changes, or when an event happens. Failed jobs retry.
Long jobs keep their progress in steps and can sleep for days without holding a worker.
Every /jobs/v1 route except /self takes the service key.
A first function
// welcome/index.ts
import { step, sleep } from "@chronosdb/forkbase";
export default async function (payload: { email: string }, ctx) {
await step("welcome", () => sendEmail(payload.email, "Welcome!")); // runs once, even if a later step fails
await sleep("a-day", 86_400); // the job leaves its worker until tomorrow
await step("tips", () => sendEmail(payload.email, "Three tips"));
return { sent: 2 };
}forkbase deploy ./welcome --env RESEND_KEY=re_xxx --max-attempts 5
forkbase invoke welcome --payload '{"email": "ada@example.com"}' --wait 30Python works the same way:
# nightly.py
from forkbase import step, sql
def main(payload, ctx):
n = step("count", lambda: 42)
return {"rows": n}forkbase deploy ./nightly.py --cron "0 3 * * *"How a job runs
- TypeScript and JavaScript (
.ts .mts .js .mjs .cjs) run on Node:nodeonPATHorjobs.node_bin..tsneeds Node 22.6 or later (types are stripped, not checked). The entry's default export is called, or itshandler, with(payload, ctx). - Python (
.py) runs onpython3onPATHorjobs.python_bin. The entry'smainis called, or itshandler, with(payload, ctx)(or fewer arguments if it takes fewer). Anasync defworks. - What the function returns (JSON) is the job's
result. A throw, a non-zero exit or a timeout fails the attempt. - Each attempt is a fresh process in a fresh folder, in its own process group, with a time limit and a memory limit
(Node's heap; Python's address space on Linux). Only these variables reach it: the function's own
env, and:
| Variable | |
|---|---|
FORKBASE_URL | this server |
FORKBASE_JOB_ID, FORKBASE_ATTEMPT, FORKBASE_FUNCTION | which job, which attempt |
FORKBASE_JOB_TOKEN | the job's fbj_ token, for its steps and sleeps |
DATABASE_URL, FORKBASE_RUN_ID, FORKBASE_RUN_TOKEN | agent functions only: the job's world |
- The SDK (
step,sleep,sql,context,createClient) is shipped with every job: import it from@chronosdb/forkbase(orforkbase) in TypeScript,forkbasein Python, without installing it. - Dependencies are not installed. Deploy sends your text files;
node_modules, virtualenvs and binaries are left out. Use the standard library andfetch, or bundle your function into one file first (esbuild index.ts --bundle --platform=node --format=esm --outfile=dist/index.mjs). - Not a sandbox. A function runs as Forkbase's own user, like any code you deploy yourself. Don't run code you don't trust.
Functions
POST /jobs/v1/functions deploys. Deploying a name again makes a new version; jobs keep the version they started on.
| Field | Default | |
|---|---|---|
name | letters, digits, _, - | |
files | {"index.ts": "...", "lib/util.ts": "..."} | |
source | one file's text instead of files (with runtime: node or python) | |
entry | index.ts / main.py | the file that's run |
env | {"NAME": "value"}; sealed at rest; null removes one | |
timeout_s | jobs.timeout_s (300) | up to jobs.max_timeout_s (3600) |
memory_mb | jobs.memory_mb (512) | 64 to jobs.max_memory_mb (4096) |
max_attempts | jobs.max_attempts (3) | 1 to 100 |
concurrency | 0 (no limit) | most jobs of it at once |
agent | run each job as a run of this agent (below) | |
propose | true | agent functions: propose the run when the job ends well |
disabled | false | stop starting jobs of it |
GET /functions lists; GET /functions/{name} shows one (variable names, never values); PATCH changes settings
without new files; DELETE removes it. GET /jobs/v1/runtimes says which runtimes this server found.
Jobs
curl -X POST "$URL/jobs/v1/functions/welcome/invoke?wait=30" -H "authorization: Bearer $SERVICE" \
-H "content-type: application/json" -d '{"payload": {"email": "ada@example.com"}}'| Field | |
|---|---|
payload | any JSON |
delay_s or run_at | start later (run_at in Unix ms) |
idempotency_key | a second invoke with the same key returns the first job |
?wait= | wait up to 60 s for it to end |
| Route | |
|---|---|
GET /jobs?function=&status=&before=&limit= | jobs, newest first |
GET /jobs/{id} | one job: status, result, every attempt with the end of its output, its steps |
POST /jobs/{id}/cancel | stop it (a running attempt is killed) |
POST /jobs/{id}/retry | run a failed or canceled job again |
Statuses: queued (waiting to start, including a sleeping job), running, succeeded, failed, canceled.
Retries: a failed attempt is tried again after 10 s, 20 s, 40 s and so on (at most an hour apart) until
max_attempts. Leases: a job whose server died goes back in line when its lease runs out, so another server
picks it up. Up to jobs.log_bytes (64 KiB) of each attempt's output is kept, the end of it. Finished jobs are
deleted after jobs.keep_days (30).
Steps and sleep
step(name, fn)runsfnonce and records what it returned. When the job retries, the step returns the recorded value without runningfnagain. A step is found by its name, so give each step in a function its own name.sleep(name, seconds)ends the attempt and starts the job again after the time, steps kept. A sleep does not use up an attempt and holds no worker.
Both call /jobs/v1/self with the job's own token, so the function never needs the database password.
Schedules
curl -X POST $URL/jobs/v1/schedules -H "authorization: Bearer $SERVICE" -H "content-type: application/json" \
-d '{"function": "nightly", "cron": "0 3 * * *", "payload": {"full": true}}'cron is in UTC: five fields (*/5 * * * *), @hourly, @daily, @weekly, @monthly, @yearly, or @every 30m. Each time
starts once, however many servers share the database. PATCH /schedules/{id} with {"enabled": false} pauses,
POST /schedules/{id}/run runs it now, DELETE removes it.
Triggers
On a table's changes:
{"function": "on_signup", "table": "public.profiles", "ops": ["insert"]}The job's payload is shaped like Supabase's database webhooks:
{"type": "INSERT", "schema": "public", "table": "profiles", "at": 1733140000000,
"record": {...}, "old_record": null, "too_big": false}On an event:
{"function": "page_oncall", "event": "run.held"}event: run.held, run.merged, any agent event, run.*, job.failed,
job.succeeded, effect.<channel>, effect.* or *. The payload is {"id", "type", "run", "data", "created_at"}.
PATCH /triggers/{id} with {"enabled": false} pauses one; DELETE removes it. Jobs started by events stop three
triggers deep, so a trigger can't start jobs forever.
In chronos mode a table trigger doesn't see rows an agent's merge wrote (the engine merges without running
triggers); use a run.merged event trigger for those.
Agent functions
Deploy with agent set and every job runs as a run of that agent:
DATABASE_URLis the job's own world, andsql(query, params)from the SDK runs there.- When the job ends well, the run is proposed, so the agent's merge policy decides. A held run still counts as a succeeded job; a conflict fails the job.
- A failed or canceled job discards its world.
- A world lives
agents.run_ttl_s. A job whose world expires while it sleeps, after steps ran in it, fails rather than go on in a new world.
import { sql } from "@chronosdb/forkbase";
export default async function () {
await sql("update carts set closed = true where updated_at < now() - interval '30 days'");
}forkbase deploy ./close-carts.ts --agent cleanup --cron "@daily"Servers and workers
[jobs] in forkbase.toml: enabled (does this server run jobs), workers (how many at once, default 4),
node_bin, python_bin, the limits above, log_bytes, keep_days. A server without a runtime leaves those jobs to
one that has it; a server with enabled = false takes functions and jobs over the API but runs none.
Something wrong or missing? Edit this page on GitHub or open an issue.