Skip to main content

Module database

Module database 

Source
Expand description

The DatabaseQueue driver — jobs that survive a restart.

Rows in two tables, defined as ordinary Rainier ORM entities, so the queue runs on whatever backend the application already uses (SQLite, D1, MySQL, Postgres) with no extra infrastructure.

§Reserving without losing or duplicating jobs

Two workers polling the same table will see the same row. A plain SELECT … LIMIT 1 followed by UPDATE would hand it to both.

The fix here is an optimistic claim: select a candidate, then update it with WHERE id = ? AND reserved_at IS NULL and check the affected-row count. Exactly one worker’s update touches a row; the losers see zero and try the next candidate. That needs no SELECT … FOR UPDATE, no advisory lock, and no dialect-specific syntax — which matters because D1 has none of them.

A worker that dies mid-job leaves a row reserved forever, so a reservation also carries a deadline: reclaim_expired releases anything held past it.

Structs§

DatabaseQueue
A queue backed by database rows.
FailedJobRow
A job that exhausted its attempts, as a row.
JobRow
A queued job, as a row.