Skip to content

Workflows

A job may wait for others. Spool.enqueue_after writes it Blocked, and it runs, as any job does, once every job it waits for has completed:

let* rows = Spool.enqueue_many conn ~now import_row lines in
let* _ = Spool.enqueue_after conn ~now ~waits_for:rows summary file in

That is a batch with a summary, a workflow of one level; a job that waits for the summary makes it two. There is nothing else to learn: a workflow is its jobs and what each waits for.

A job that fails – its attempts exhausted, discarded, expired or cancelled – cancels every job that waits for it to complete, however far down, each with a reason naming the job that failed and how. Each cancelled one is failing in turn, so its own kind’s ~failed runs for it. What waits on a job learns how it ended in the transaction that records it, so no job is left blocked on one that has ended.

A batch’s last step – telling the user the import finished, removing the upload – must run whether every row completed or not. It waits with ~until:All_ended:

let* rows = Spool.enqueue_many conn ~now import_row lines in
let* _ =
Spool.enqueue_after conn ~now ~waits_for:rows ~until:All_ended
finish_import file
in

It runs once every job it waits for has ended, however: completed, or failed and settled, so each failed one’s ~failed has committed first and what the last step reads is final. A row’s failure never cancels it. Its handler reads how each ended by Spool.workflow.

A job waited for is kept while anything waits for it: pruning passes it over, delete refuses it, and a kind that keeps none keeps its row with its payload emptied. So how it ended is always its own row’s state, and an id the table does not hold – never given, or gone before the wait – is refused with `Unknown_jobs, nothing written. Waiting for completion, one already failed when the job is enqueued cancels it at once, and one ending as it is enqueued is seen to, whichever commits first: the wait reads it under a lock its ending waits for.

A retried job that waits for others waits again: retry what it waited for first.

enqueue_after takes no unique key. A blocked job is outside the fold of waiting jobs by key, and once released it would meet the job its key already names.

Spool.workflow conn id answers every job connected to one by waiting, either way, with each one’s info, and the edges between them as (job, waits_for) pairs.