spool
Durable jobs for modern OCaml, backed by Postgres
A newer version is out: spool 0.7.1
spool
Durable jobs for modern OCaml, backed by Postgres. Transactional, reliable and testable. Eio-native and multicore-ready.
Documentation: hyphatech.github.io/spool
Features
- In your transaction: a job is a row written with the work that made it owed, so a rollback leaves no job.
- Several processes, one queue: claims that never wait on each other, leases a heartbeat renews, and a lapsed one rescued.
- Retries with backoff: a handler retries, retries after what an upstream said, or discards.
- Asked twice, one job: a unique key folds a second request into the job already waiting.
- Scheduled and prioritised: at an instant, after a span, until an expiry, at a priority from 0 to 9.
- Tests that never sleep:
Runner.drainruns every job due at the test's own clock. - A live dashboard:
spool-dashboard, routes of your own Spindle server, open to whom you say and for what, to watch the queue, cancel, retry or delete a job or a selection, pause a queue and set its limits.
Install
opam repo add open-opam https://github.com/open-opam/opam-repository.git
opam install spool rowtype-migrate
(libraries spool rowtype-postgres eio_main)
spool's table is made by its own migrations, applied beside yours before your application starts:
rowtype-migrate up --source spool # spool's migrations, then yours
A whole program: the quick start.
Example
open Result.Syntax
module Pg = Rowtype_postgres
let send_receipt order =
Printf.printf "Receipt for order %d sent.\n%!" order;
Ok ()
(* {"order": 42} *)
let order =
Wiretype.Object.map Fun.id
|> Wiretype.Object.mem "order" Wiretype.int ~enc:Fun.id
|> Wiretype.Object.finish
let receipt = Spool.Kind.make ~queue:"mail" "receipt.send" order
let insert_order =
Rowtype.find ~params:Rowtype.text ~row:Rowtype.int
"insert into orders (email) values ($1) returning id"
(* The order and its receipt, in one transaction: a rollback leaves neither. *)
let take_order conn ~now email =
Pg.Transaction.within conn @@ fun conn ->
let* order = Pg.run conn insert_order email in
Spool.enqueue conn ~now ~unique:(string_of_int order) receipt order
let runner ~sw env conninfo =
Spool.Runner.create ~sw ~net:(Eio.Stdenv.net env)
~mono_clock:(Eio.Stdenv.mono_clock env)
~now:Ptime_clock.now
~queues:[ ("mail", 2) ]
[ Spool.handle receipt (fun _job order -> send_receipt order) ]
conninfo
The runner claims the job once the transaction commits, under a lease, and
runs its handler outside every transaction; Spool.Runner.start starts it
and Spool.Runner.stop stops it.
With an agent
Each release installs its documentation for an agent with it, at
$(opam var spool:doc): llms.txt, and every page in Markdown. An agent
reads it when your project's AGENTS.md tells it to:
Before writing code with spool, read `$(opam var spool:doc)/llms.txt` and
the pages it lists for the parts you need, and use those parts rather than
writing your own.
Contributing
See AGENTS.md.
Licence
MIT, copyright Hypha Technologies Ltd. See LICENSE.
This is the README of 0.7.1, the latest version. This version's README
| Package | Constraint |
|---|---|
| dune | >= "3.18" |
| ocaml | >= "5.5" |
| rowtype | >= "0.10.0" & < "0.11.0" |
| rowtype-postgres | >= "0.10.0" & < "0.11.0" |
| ptime | >= "1.2.0" |
| wiretype | >= "0.7.0" & < "0.8.0" |
| eio | >= "1.5" |
| logs | >= "0.10.0" |
| mtime | >= "2.1.0" |
| timedesc | >= "3.1.2" |
| eio_main | with-test & >= "1.5" |
| alcotest | with-test & >= "1.9.1" |
| hypha-litany | with-dev-setup & >= "1.2.2" & < "2.0.0" |
| merlin | with-dev-setup & >= "5.6-504" |
| ocaml-lsp-server | with-dev-setup & >= "1.25.0" |
| ocamlformat | with-dev-setup & = "0.29.0" |
| odoc | with-dev-setup & >= "3.2.1" |
| odoc | with-doc |
| Package | Constraint |
|---|---|
| spool-dashboard | = version |