Mailbox Automations
A durable job queue on every Sprite. Send work while it sleeps, and wake it automatically when messages pile up.
On this page
What it is
Every Sprite has a mailbox: an asynchronous, durable, first-in-first-out (FIFO) queue. Messages are processed in order, one at a time, and each one is either acknowledged or rejected. Another Sprite or an API caller can send messages while the receiving Sprite sleeps; the messages are kept, even across cold stops, until it processes them.
Pair it with a mailbox automation and the Sprite wakes up automatically once the queue reaches a depth you choose, which can be as low as one message. It processes its queue and can go back to sleep afterwards.
Why it matters: hand work off asynchronously without keeping the receiver running, and cut idle compute costs.
Key ideas
- Deliver: put a message (any JSON, up to 16 KiB) in a Sprite’s mailbox. Delivery does not wake the Sprite.
- Receive: the worker takes messages and gets a receipt for each, a temporary token that says “I’m working on this.”
- Ack: after successfully processing a message, acknowledge it with the receipt, which removes it.
- Nack: if processing fails in a retryable way, reject it, and it becomes available again after a delay.
- Message IDs deduplicate: delivering the same
message_idagain within 24 hours is ignored (statusduplicate). - Groups: only one message per
group_idis handed out at a time, which keeps related jobs in order.
Step by step: a job queue that wakes its worker
We’ll set up a Sprite named worker that processes jobs, and have it wake whenever at least one job is waiting.
1. Write the worker script
Inside worker, create /home/sprite/process-inbox. It drains the mailbox, then exits:
#!/bin/sh
# Process every waiting message, then exit.
while true; do
batch="$(sprite-env mailbox receive 1 5000)" || exit 1
receipt="$(printf '%s' "$batch" | jq -r '.messages[0].receipt // empty')"
[ -z "$receipt" ] && exit 0 # mailbox empty: we're done
if printf '%s' "$batch" | /home/sprite/handle-job; then
sprite-env mailbox ack "$receipt"
else
sprite-env mailbox nack "$receipt" 1000 # retry in 1s
fi
done
chmod +x /home/sprite/process-inbox
mailbox receive 1 5000 takes up to 1 message, waiting up to 5000 ms for one to arrive. handle-job stands in for your own processing code.
The output looks like this (an empty mailbox gives {"messages":[]}):
{"messages":[{"message_id":"job-43","group_id":"jobs","seq":1,
"payload":{"task":"index","repository":"acme/widgets","ref":"main"},
"receipt":"...","attempts":1,"max_attempts":10,"lease_until_ms":1793300000000,
"attributes":{}}]}
A received message is leased for 30 seconds. If it isn’t acked or nacked by then, it’s handed out again.
By default a message is attempted up to 10 times before it’s dead-lettered. With this script, a job that keeps failing is retried (1 second apart) within the same run until it reaches that limit. Set max_attempts when delivering to change it.
Ack only after the work is durably done. If the worker crashes before acking, the message will be handed out again. Never reuse a receipt after ack, nack, or once it expires.
2. Create the mailbox automation
Run the script whenever the mailbox has at least one message. Start it disabled so you can test first:
input='{"when":{"type":"mailbox","min_size":1},"action":{"type":"exec","argv":["/home/sprite/process-inbox"],"after":"sleep_if_woken"},"enabled":false}'
ssh my-org@sprites.dev "automation create worker inbox --input-json '$input'"
when.type: mailboxwithmin_size: 1: trigger when 1 or more messages are waiting. Raisemin_sizeto process in batches.after: sleep_if_woken: go back to sleep afterwards if this woke the Sprite.
3. Deliver a test message
From your own machine (needs a full-access token, no grant):
job='{"task":"index","repository":"acme/widgets","ref":"main"}'
ssh my-org@sprites.dev "mailbox deliver worker '$job' --message-id job-43 --group-id jobs"
# HTTP API
curl -sS -X POST "https://api.sprites.dev/v1/sprites/worker/mailbox/deliver" \
-H "Authorization: Bearer $SPRITES_TOKEN" \
-H "Content-Type: application/json" \
-d '{"message_id":"job-43","group_id":"jobs",
"payload":{"task":"index","repository":"acme/widgets","ref":"main"}}'
The response looks like {"message_id":"job-43","seq":...,"status":"delivered","available_at_ms":...}.
4. Check the queue without waking the Sprite
ssh my-org@sprites.dev mailbox stats worker
5. Test the automation, then enable it
ssh my-org@sprites.dev automation run worker inbox
ssh my-org@sprites.dev automation get worker inbox # check last_exit_code
input='{"enabled":true}'
ssh my-org@sprites.dev "automation update worker inbox --input-json '$input'"
Now every delivery that brings the mailbox to min_size wakes worker, runs process-inbox, and lets it sleep again.
Sending from one Sprite to another
A Sprite needs a grant to deliver into another Sprite’s mailbox. Authorize producer to send to worker:
ssh my-org@sprites.dev "service-grant create producer-to-worker-mailbox \
--scope cluster \
--cluster default \
--source producer \
--target worker/sprite.mailbox \
--description 'Producer submits durable jobs to worker'"
# HTTP API
curl -sS -X POST "https://api.sprites.dev/v1/clusters/default/service-grants" \
-H "Authorization: Bearer $SPRITES_TOKEN" \
-H "Content-Type: application/json" \
-d '{"id":"producer-to-worker-mailbox","source_ref":"producer",
"target_ref":"worker/sprite.mailbox",
"description":"Producer submits durable jobs to worker"}'
Then, inside producer, deliver with: target Sprite, message ID, payload, group:
sprite-env mailbox deliver-to \
worker \
job-42 \
'{"task":"index","repository":"acme/widgets","ref":"main"}' \
jobs
Without a matching sprite.mailbox grant, cross-Sprite delivery is denied.
A Sprite can also put messages in its own mailbox (no grant needed):
sprite-env mailbox deliver event-1 '{"event":"rebuild"}' maintenance
Working with messages inside the receiver
sprite-env mailbox stats # counts
sprite-env mailbox receive 10 5000 # up to 10 messages, wait up to 5s
sprite-env mailbox ack "$receipt" # done
sprite-env mailbox nack "$receipt" 1000 # retry after 1s
A long-running worker
If the worker runs continuously (for example on an always-on Sprite) instead of being woken, long-poll in a loop:
while true; do
batch="$(sprite-env mailbox receive 1 30000)" || continue
receipt="$(printf '%s' "$batch" | jq -r '.messages[0].receipt // empty')"
[ -z "$receipt" ] && continue
if process_one_job "$batch"; then
sprite-env mailbox ack "$receipt"
else
sprite-env mailbox nack "$receipt" 1000
fi
done
Delivery options
| Field | Meaning |
|---|---|
payload | Required. Any JSON value, up to 16 KiB. |
message_id | Dedup key; a repeat within 24 hours is the same message. Generated if absent. |
group_id | Default default. One message per group is out at a time. |
attributes | An object of extra metadata. |
delay_ms | Hold the message back for this long. |
max_attempts | How many times it may be delivered before it’s dead-lettered. |
Dead letters
Messages that keep failing land in a dead-letter list. ssh my-org@sprites.dev mailbox stats worker prints the mailbox_key these commands take (its counts are nested under stats; inside the Sprite, sprite-env mailbox stats prints them flat and calls the key mailbox_id):
ssh my-org@sprites.dev mailbox dead list MAILBOX_KEY
ssh my-org@sprites.dev mailbox dead retry MAILBOX_KEY MESSAGE_ID
ssh my-org@sprites.dev mailbox dead discard MAILBOX_KEY MESSAGE_ID
Limits
- A payload over 16 KiB fails with
413 payload_too_large; a missing payload with400 missing_payload. - A mailbox holds up to 100,000 messages; beyond that delivery fails with
mailbox_full(429). - By default, 2,000 deliveries per minute into one mailbox and 10,000 across your organization, shared between outside and in-Sprite delivery. Beyond that:
rate_limited(429).