Enqueue, claim, settle
Insert three sends, lease the due ones in claim order, then complete one and retry the other in the same program.
# Three sends. The third is due in an hour.
request_id: c7-enqueue-1
issued_at: 2026-11-16T09:30:00Z
statements:
- insert:
into: sends
rows:
- {key: c7-ana, priority: 1, campaign: c7, address: ana@example.com, body: {template: welcome}}
- {key: c7-bo, priority: 2, campaign: c7, address: bo@example.com, body: {template: welcome}}
- {key: c7-cy, campaign: c7, address: cy@example.com, after: 1h, body: {template: reminder}}
The result has one entry per row: id, key and status inserted. Sending the same program again returns the recorded response with replayed: true, and inserts nothing.
# Lease up to 10 due sends for 30 seconds.
request_id: w1-claim-1
issued_at: 2026-11-16T09:30:01Z
statements:
- claim: {from: sends, limit: 10, lease: 30s, worker: w1}
Returns c7-ana, then c7-bo. Both became due at the same instant, so priority 1 comes first; c7-cy is not due for an hour. Each item comes back with its lease token, attempts (1), version, body and declared fields.
# Ana's send went out; Bo's provider timed out, so retry in 5 minutes.
# The ids and tokens are the ones the claim returned.
request_id: w1-settle-1
issued_at: 2026-11-16T09:30:02Z
params:
ana: 1
ana_token: "<token from the claim>"
bo: 2
bo_token: "<token from the claim>"
statements:
- settle:
queue: sends
outcome: complete
items: [{id: {param: ana}, token: {param: ana_token}}]
set: {step: 1}
body:
- {set: /sent_at, value: "2026-11-16T09:30:02Z"}
- settle:
queue: sends
outcome: retry
after: 5m
items: [{id: {param: bo}, token: {param: bo_token}}]
Each settle returns status: ok per item. A wrong token or a lapsed lease reports stale and leaves the item alone; require_all: true would reject the program instead. The retry keeps Bo's attempt charged: with max_attempts: 5, once five attempts are used a retry leaves the item failed.
Programs that write carry a fixed issued_at here. Set it
to the time you create the request id, and resend it unchanged on
every retry. Ids and tokens come from earlier responses and are passed
as params.