Skip to content

Spool

Durable jobs for modern OCaml, backed by Postgres.

Source on GitHubDocumentation

About

Jobs live in the database you already run. A job is a row written with the work that made it owed, so a rollback leaves no job, and several processes share one queue with claims that never wait on each other, under leases a heartbeat renews.

Retried with backoff or after what an upstream said, scheduled, prioritised and recurring, and a second request for the same job folds into the one already waiting. A job may wait for others, and a queue's limits hold across every runner. Tests never sleep: one call runs every job due at the test's own clock.

A live dashboard comes as routes of your own Spindle server, open to whom you say and for what: watch the queue, retry, cancel or delete a job, pause a queue and set its limits.

Messaging and jobs

Install

From open-opam, on OCaml 5.5 or later:

opam repo add open-opam https://github.com/open-opam/opam-repository.git
opam install spool rowtype-migrate

Then in your dune file:

(libraries spool rowtype-postgres eio_main)

In code

  1. A kind, enqueued with the order it is owed for: a rollback leaves neither.

    let receipt = Spool.Kind.make ~queue:"mail" "receipt.send" order
    
    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
  2. A runner, two workers on the mail queue.

    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

Build with Hypha.

  • Talk to us

    Bring an idea, a product or a system. Questions and feedback are just as welcome.

  • Services

    A new product built to last, or the system your business runs on rebuilt. Hypha's expertise and its foundation, end to end.

  • The Factory

    Make the decisions and let Hypha Autonomous Factory do the rest, turning them into a tested release, ready to ship.