Jobs / Jobs

Jobs

The Jobs engine runs work later, on a schedule, or now but outside the request that asked for it: an email in the background, a reminder at six, a cleanup every night. A queue keeps its jobs in jobs.db, or in an application's database so that a job commits with its rows. A worker runs each job, retries it when it fails, and keeps the steps it finished.

const emails = store.queue<Email>('emails', { attempts: 5, concurrency: 4 })

await emails.add({ to: 'ada@example.com', subject: 'Welcome' }, { delay: '1m' })
emails.work(async (email, run) => {
	const body = await run.step('render', () => render(email))
	await smtp.send(email.to, body)
})
emails = store.queue("emails", Email, attempts=5, concurrency=4)

await emails.add(Email(to="ada@example.com", subject="Welcome"), delay="1m")


async def send(email: Email, run: tinystore.Run) -> None:
    body = await run.step("render", lambda: render(email))
    await smtp.send(email.to, body)


emails.work(send)
emails, err := jobs.OpenQueue[Email](store, "emails", jobs.Attempts(5), jobs.Concurrency{Total: 4})

_, err = emails.Add(ctx, Email{To: "ada@example.com", Subject: "Welcome"}, jobs.Delay(time.Minute))
err = emails.Work(ctx, func(ctx context.Context, email Email, run *jobs.Run) error {
	body, err := jobs.Step(ctx, run, "render", func(ctx context.Context) (string, error) { return render(email) })
	if err != nil {
		return err
	}
	return smtp.Send(ctx, email.To, body)
})
let emails = store.queue::<Email>("emails").attempts(5).concurrency(Concurrency::total(4)).open()?;

emails.delay(Duration::from_mins(1)).add(&Email { to: "ada@example.com".into(), subject: "Welcome".into() })?;
let worker = emails.work(|email: Email, run| -> Result<(), AppError> {
    let body: String = run.step("render", || render(&email))?;
    smtp.send(&email.to, &body)
})?;

What it guarantees

  • A job is on the disk before add returns, and survives any crash after (contract DUR-01).
  • A job never runs before its time, and due jobs start in the order of their time (contract JOB-01).
  • A job added in a transaction commits with its rows, or not at all (contract JOB-02).
  • A job runs at least once: a run that ends before it is settled runs again, and its stale lease settles nothing, so an update or a set made once the lease ended runs (contract JOB-05).
  • A step runs once per job, across its attempts (contract JOB-06).
  • concurrency counts every worker of every process, and a rate counts within each process (contract JOB-09).

In these guides

PageCovers
Idsid, upsert, dedupe, keep, cancel, get, pages and counts
Gather and quiet hoursgather, items under one id collected into one job, and quiet hours
Schedules and cronevery, cron with a time zone, ttl and expiresAt, store.schedule, runNow
Concurrency and ratelimits in all and per group, starts per second
Watching a jobget, watch, ahead, progress
Stepsanswers kept across attempts
Jobs in your databasedb.queue and transactions
TestingrunDue and a clock you move

New to Jobs? Replies in the background in the Handbook writes a chat's replies with a queue, steps and a watch, and How a job lives explains leases and crashes.

Async Rust

Rust only: with the async feature, a queue opened from tinystore::aio::Store has the same calls, awaited. Rust with async explains where each call runs and what a dropped future does.

rust
use tinystore::aio::Store;
use tinystore::aio::jobs::Run;

async fn send(email: Email, run: Run) -> Result<(), AppError> {
    let body: String = run.step("render", || render(&email)).await?;
    smtp.send(&email.to, &body).await
}

let store = Store::open("data", Options::default())?;
let emails = store.queue::<Email>("emails").attempts(5).open()?; // a plain call, as in blocking Rust

emails.delay(Duration::from_mins(1)).add(&email).await?;
let worker = tokio::spawn(emails.work(send).until(tokio::signal::ctrl_c()));
worker.await??; // after ctrl-c: the handlers under way finished, and their jobs are settled

Each async call has the name of its blocking twin:

BlockingAsync
emails.add(&email)?emails.add(&email).await?
emails.id(&id).delay(span).add(&email)?emails.id(&id).delay(span).add(&email).await?
emails.get(&id)?, emails.list(&filter)?emails.get(&id).await?, emails.list(&filter).await?
for job in emails.all(filter)while let Some(job) = all.next().await
for job in emails.watch(&id)while let Some(job) = watch.next().await
let worker = emails.work(send)?let worker = tokio::spawn(emails.work(send))
worker.stop()emails.work(send).until(signal)
emails.run_due(send)?emails.run_due(send).await?
run.step("render", work)?run.step("render", work).await?
imports.claim()?, claimed.settle(())?imports.claim().await?, claimed.settle(()).await?

all and watch return a Stream. A handler is an async function that takes the job's value and its Run by value, so that a multi-threaded runtime can move its future between threads.

A worker is a future

work returns a worker at once and starts nothing. Spawn it on your runtime, and its handlers run inside its future, as many at once as the worker's concurrency. The loop that claims and settles jobs runs on one of the store's own threads, so a hundred finished jobs still share one commit.

  • until(signal) stops the worker gracefully. Once the signal is ready, it takes no more jobs, waits for the handlers under way, and returns after their jobs are settled. Jobs that it claimed and had not started go back to the queue, and their attempt is not counted (contract JOB-11).
  • Dropping the worker, for example with JoinHandle::abort, stops it at once. Jobs that it had not started go back uncounted. A job whose handler was running runs again after its backoff, and its attempt is counted.
  • When the store closes, every worker stops as until stops it, and its future returns Ok(()). Close the store with store.close().await. A blocking close on your executor's only thread can't poll the handlers, so it waits for the queue's timeout and ten seconds more.

A cancel of a running job makes run.stopped() return true, as in the blocking API. The worker doesn't drop the handler's future, and what the handler returns changes nothing.

Where the calls run

A watch reads its job again each time the queue commits. Unlike the blocking watch, it doesn't look again every minute, so a scheduled job that becomes due shows up when a worker claims it.

Inside a transaction's closure, a queue's calls are the blocking ones, as in blocking Rust:

rust
let queue = emails.clone(); // the closure runs on the store's threads and owns what it takes
store.tx(move |tx| tx.with(&queue).id("welcome:42").add(&email)).await?;

Reference

Bun and Node · Python · Go · Rust

Edit this page on GitHub Updated 27 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results