Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ you broke it. Cite an entry by its bold phrase.
machine's. Symptom: a pause or a limit read from `Runner.create`'s
arguments; the suite pauses a queue and finds no runner claims from it,
one made after the pause among them.
- **A handler's write is made with its job's completion, under its
lease.** `handle_committing`'s write runs in the transaction that
completes the job, after the statement guarded by the lease, and a write
that fails rolls the completion back, so a write is made once, by the
runner that finished the job. Symptom: a write made by a runner whose
lease was taken, or one left by a completion rolled back; `test_spool`
takes a lease from under a write, and fails and raises in one.
- **A limit holds across runners, counted under the queue's lock.** A
limited queue's claim takes the queue's advisory lock and counts what
runs and what began in the rate's period before it claims, since a limit
Expand Down
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

## Unreleased

- `Spool.handle_committing`: a handler answers a write to the
application's own tables, made in the transaction that completes its job
and only under its lease, so the job is completed and the write made
together, or neither; a write that fails is the attempt's failure.

## 0.7.2 (2026-12-03)

- Every package installs the documentation for agents, `llms.txt` and every
Expand Down
25 changes: 23 additions & 2 deletions agents/pages/guide/handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,29 @@ carries it.
`Spool.job` is the attempt a handler is running: its `id`, its `attempt`,
counting from one, its `max_attempts` and its `inserted_at`.

## A write with the completion

Where an attempt's work ends in a write to the application's own tables,
`Spool.handle_committing` makes it in the transaction that completes the
job. The handler does its work outside every transaction, as any does, and
answers the write:

```ocaml
Spool.handle_committing invoice (fun _job order ->
let url = Pdf.render order in
Ok (fun conn -> Invoices.set_pdf conn order url))
```

The job is completed and the write made together, or neither. The write
runs after the statement guarded by the lease, so a runner whose lease was
taken makes none. A write that answers a failure rolls both back and is
that failure -- retried, or discarded -- and one that raises is a retry
named by its constructor. Its connection is the runner's own, on the
database the runner was given, so the tables it writes are there.

## At least once

What a job runs is at least once and finished once. A handler whose effect
must happen once is idempotent, or keyed by the job -- as the handler above
What a job runs is at least once and finished once. A write made with the
completion happens once; any other effect a handler has that must happen
once is idempotent, or keyed by the job -- as the handler above
gives the mail service the job's id, so a receipt sent twice is one.
5 changes: 0 additions & 5 deletions agents/pages/guide/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
job to the request that made it owed.
- **A queue's counts are exported nowhere.** `counts` answers them, and
nothing turns them into metrics.
- **A handler's own writes are not part of finishing its job.** A handler
writes to the application's tables in a transaction of its own, before
the runner records the job complete in another, so a runner that dies
between the two runs the job again: a handler's write is idempotent,
keyed by the job or by what it writes about.
- **The dashboard needs a Spindle server.** `spool-dashboard` is routes of
one, and nothing serves it without.
- **A handler that never yields cannot be stopped.** A timeout, a lost lease
Expand Down
3 changes: 2 additions & 1 deletion agents/pages/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ OCaml code names it as a module (`Spool_dashboard`).
- [`Spool`](https://hyphatech.github.io/spool/reference/spool/Spool/index.html) -- the table, kinds,
enqueueing, an operator's hand, and running: `table`, `statements`,
`enqueue`, `enqueue_many`, `enqueue_after`, `find`, `workflow`, `counts`,
`jobs`, `cancel`, `retry`, `run_again`, `delete` and `handle`. Its
`jobs`, `cancel`, `retry`, `run_again`, `delete`, `handle` and
`handle_committing`. Its
modules:
- [`Spool.Kind`](https://hyphatech.github.io/spool/reference/spool/Spool/Kind/index.html) -- a kind
of job: its name, its payload's description, and how it is run.
Expand Down
25 changes: 23 additions & 2 deletions docs/src/content/docs/guide/handlers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,29 @@ carries it.
`Spool.job` is the attempt a handler is running: its `id`, its `attempt`,
counting from one, its `max_attempts` and its `inserted_at`.

## A write with the completion

Where an attempt's work ends in a write to the application's own tables,
`Spool.handle_committing` makes it in the transaction that completes the
job. The handler does its work outside every transaction, as any does, and
answers the write:

```ocaml
Spool.handle_committing invoice (fun _job order ->
let url = Pdf.render order in
Ok (fun conn -> Invoices.set_pdf conn order url))
```

The job is completed and the write made together, or neither. The write
runs after the statement guarded by the lease, so a runner whose lease was
taken makes none. A write that answers a failure rolls both back and is
that failure -- retried, or discarded -- and one that raises is a retry
named by its constructor. Its connection is the runner's own, on the
database the runner was given, so the tables it writes are there.

## At least once

What a job runs is at least once and finished once. A handler whose effect
must happen once is idempotent, or keyed by the job -- as the handler above
What a job runs is at least once and finished once. A write made with the
completion happens once; any other effect a handler has that must happen
once is idempotent, or keyed by the job -- as the handler above
gives the mail service the job's id, so a receipt sent twice is one.
5 changes: 0 additions & 5 deletions docs/src/content/docs/guide/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ title: What it does not do yet
job to the request that made it owed.
- **A queue's counts are exported nowhere.** `counts` answers them, and
nothing turns them into metrics.
- **A handler's own writes are not part of finishing its job.** A handler
writes to the application's tables in a transaction of its own, before
the runner records the job complete in another, so a runner that dies
between the two runs the job again: a handler's write is idempotent,
keyed by the job or by what it writes about.
- **The dashboard needs a Spindle server.** `spool-dashboard` is routes of
one, and nothing serves it without.
- **A handler that never yields cannot be stopped.** A timeout, a lost lease
Expand Down
3 changes: 2 additions & 1 deletion docs/src/content/docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ OCaml code names it as a module (`Spool_dashboard`).
- [`Spool`](/spool/reference/spool/Spool/index.html) -- the table, kinds,
enqueueing, an operator's hand, and running: `table`, `statements`,
`enqueue`, `enqueue_many`, `enqueue_after`, `find`, `workflow`, `counts`,
`jobs`, `cancel`, `retry`, `run_again`, `delete` and `handle`. Its
`jobs`, `cancel`, `retry`, `run_again`, `delete`, `handle` and
`handle_committing`. Its
modules:
- [`Spool.Kind`](/spool/reference/spool/Spool/Kind/index.html) -- a kind
of job: its name, its payload's description, and how it is run.
Expand Down
67 changes: 57 additions & 10 deletions src/spool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1618,10 +1618,20 @@ type failure =
| Retry_in of Mtime.Span.t * string
| Discard of string

(* What a handler's success may carry: a write made in the transaction that
completes its job. *)
type commit = Pg.conn -> (unit, failure) result

type handler =
| Handler : 'a Kind.t * (job -> 'a -> (unit, failure) result) -> handler
| Handler :
'a Kind.t * (job -> 'a -> (commit option, failure) result)
-> handler

let handle kind f =
Handler (kind, fun job a -> Result.map (fun () -> None) (f job a))

let handle kind f = Handler (kind, f)
let handle_committing kind f =
Handler (kind, fun job a -> Result.map Option.some (f job a))

module Runner = struct
type refused =
Expand Down Expand Up @@ -1904,10 +1914,13 @@ module Runner = struct
(* ---------------------------------------------------------------- *)
(* One attempt *)

type outcome = Done | Failed of failure | Interrupted of interrupted
type outcome =
| Done of commit option
| Failed of failure
| Interrupted of interrupted

let outcome_words = function
| Done -> "completed"
| Done _ -> "completed"
| Failed (Retry r) -> "failed, to be retried: " ^ r
| Failed (Retry_in (after, r)) ->
Format.asprintf "failed, to be retried in %a: %s" Mtime.Span.pp after r
Expand All @@ -1931,7 +1944,7 @@ module Runner = struct
Eio.Time.Timeout.run (Eio.Time.Timeout.v t.clock kind.timeout)
(fun () ->
match f job a with
| Ok () -> Ok Done
| Ok commit -> Ok (Done commit)
| Error failure -> Ok (Failed failure))
with
| Ok o -> o
Expand All @@ -1955,7 +1968,8 @@ module Runner = struct
(* What the attempt came to, written by one statement guarded by the
claim, and the state it left the job in; [None] where nothing was
written. *)
let finish t (Handler (kind, _)) ~id ~attempt ~max_attempts ~took outcome =
let rec finish t (Handler (kind, _) as h) ~id ~attempt ~max_attempts ~took
outcome =
let now = t.now () in
let s = t.table.s in
let run_us = Int64.unsigned_div (Mtime.Span.to_uint64_ns took) 1000L in
Expand Down Expand Up @@ -1983,13 +1997,42 @@ module Runner = struct
let* n = Pg.run c st args in
after_written c counted n)
in
(* The handler's write, made in the transaction that completes its job
once the statement guarded by the lease has changed the row, so a
runner whose lease was taken makes none; one that fails rolls the
completion back with it, and is the attempt's failure. *)
let committed commit c n =
match commit with
| Some write when n > 0 -> (
match write c with
| Ok () -> Ok n
| Error failure -> Error (`Write failure)
| exception (Eio.Cancel.Cancelled _ as e) -> raise e
| exception e ->
let bt = Printexc.get_raw_backtrace () in
Log.err (fun m ->
m "%s job %d attempt %d's write raised %s\n%s" kind.name id
attempt (Printexc.to_string e)
(Printexc.raw_backtrace_to_string bt));
Error (`Write (Retry (Printexc.exn_slot_name e ^ " raised"))))
| Some _ | None -> Ok n
in
let written =
match outcome with
| Interrupted _ -> None
| Done ->
| Done commit ->
Some
( (if kind.keep then
write Counted_completed s.complete (id, t.runner, attempt, now)
use t (fun c ->
let* () =
Pg.run c s.count_attempt
(id, t.runner, attempt, now, Counted_completed, run_us)
in
let* n =
Pg.run c s.complete (id, t.runner, attempt, now)
in
let* n = after_written c Counted_completed n in
committed commit c n)
else
use t (fun c ->
let* () =
Expand All @@ -2004,7 +2047,8 @@ module Runner = struct
else
Pg.run c s.complete_emptied (id, t.runner, attempt, now)
in
after_written c Counted_completed n)),
let* n = after_written c Counted_completed n in
committed commit c n)),
Completed )
| Failed (Discard reason) ->
Some
Expand Down Expand Up @@ -2053,7 +2097,10 @@ module Runner = struct
m "%s; its lease was taken or the job cancelled, so nothing changed"
(said ()));
None
| Some (Error e, _) ->
| Some (Error (`Write failure), _) ->
(* Rolled back with its write: the attempt is that failure. *)
finish t h ~id ~attempt ~max_attempts ~took (Failed failure)
| Some (Error (#Pg.error as e), _) ->
Log.warn (fun m -> m "%s, and not recorded: %s" (said ()) (words e));
None

Expand Down
21 changes: 21 additions & 0 deletions src/spool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,27 @@ type handler

val handle : 'a Kind.t -> (job -> 'a -> (unit, failure) result) -> handler

val handle_committing :
'a Kind.t ->
(job ->
'a ->
(Rowtype_postgres.conn -> (unit, failure) result, failure) result) ->
handler
(** As {!handle}, for work that ends in a write to the application's own tables:
the handler does its work outside every transaction, as any does, and
answers the write, which the runner makes in the transaction that completes
the job -- after the statement guarded by the lease, so a runner whose lease
was taken makes no write. The job is completed and the write made together,
or neither: a write that answers a {!failure} rolls both back and is that
failure, and one that raises is a {!Retry} named by its constructor. Its
connection is the runner's own, on the database the runner was given.

{[
Spool.handle_committing receipt (fun _job order ->
let url = Pdf.render order in
Ok (fun conn -> Invoices.set_pdf conn order url))
]} *)

module Runner : sig
type t

Expand Down
Loading