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

TypeScript
// 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 };
}
shell
forkbase deploy ./welcome --env RESEND_KEY=re_xxx --max-attempts 5
forkbase invoke welcome --payload '{"email": "ada@example.com"}' --wait 30

Python works the same way:

Python
# nightly.py
from forkbase import step, sql

def main(payload, ctx):
    n = step("count", lambda: 42)
    return {"rows": n}
shell
forkbase deploy ./nightly.py --cron "0 3 * * *"

How a job runs

  • TypeScript and JavaScript (.ts .mts .js .mjs .cjs) run on Node: node on PATH or jobs.node_bin. .ts needs Node 22.6 or later (types are stripped, not checked). The entry's default export is called, or its handler, with (payload, ctx).
  • Python (.py) runs on python3 on PATH or jobs.python_bin. The entry's main is called, or its handler, with (payload, ctx) (or fewer arguments if it takes fewer). An async def works.
  • 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_URLthis server
FORKBASE_JOB_ID, FORKBASE_ATTEMPT, FORKBASE_FUNCTIONwhich job, which attempt
FORKBASE_JOB_TOKENthe job's fbj_ token, for its steps and sleeps
DATABASE_URL, FORKBASE_RUN_ID, FORKBASE_RUN_TOKENagent functions only: the job's world
  • The SDK (step, sleep, sql, context, createClient) is shipped with every job: import it from @chronosdb/forkbase (or forkbase) in TypeScript, forkbase in 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 and fetch, 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.

FieldDefault
nameletters, digits, _, -
files{"index.ts": "...", "lib/util.ts": "..."}
sourceone file's text instead of files (with runtime: node or python)
entryindex.ts / main.pythe file that's run
env{"NAME": "value"}; sealed at rest; null removes one
timeout_sjobs.timeout_s (300)up to jobs.max_timeout_s (3600)
memory_mbjobs.memory_mb (512)64 to jobs.max_memory_mb (4096)
max_attemptsjobs.max_attempts (3)1 to 100
concurrency0 (no limit)most jobs of it at once
agentrun each job as a run of this agent (below)
proposetrueagent functions: propose the run when the job ends well
disabledfalsestop 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

shell
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
payloadany JSON
delay_s or run_atstart later (run_at in Unix ms)
idempotency_keya 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}/cancelstop it (a running attempt is killed)
POST /jobs/{id}/retryrun 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) runs fn once and records what it returned. When the job retries, the step returns the recorded value without running fn again. 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

shell
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:

JSON
{"function": "on_signup", "table": "public.profiles", "ops": ["insert"]}

The job's payload is shaped like Supabase's database webhooks:

JSON
{"type": "INSERT", "schema": "public", "table": "profiles", "at": 1733140000000,
 "record": {...}, "old_record": null, "too_big": false}

On an event:

JSON
{"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_URL is the job's own world, and sql(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.
TypeScript
import { sql } from "@chronosdb/forkbase";
export default async function () {
  await sql("update carts set closed = true where updated_at < now() - interval '30 days'");
}
shell
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.

Jobs · Forkbase