Recipes

Recipe: Async decision document writer

Generate structured decision docs from async workflows without blocking the main thread.

Overview

This recipe wires a background worker to consume decision events, hydrate templates, and write finalized documents to object storage. The caller receives a job ID immediately and polls or subscribes for completion.

Ingredients

  • Upstash QStash or equivalent durable queue
  • Template engine (React-pdf, docx-templater, or handlebars)
  • Object store (S3 / R2) with pre-signed upload URLs
  • Status endpoint for polling

Flow

  1. 1Client POSTs decision payload to /api/decisions
  2. 2API enqueues job, returns {jobId, statusUrl}
  3. 3Worker hydrates template, renders document, uploads to store
  4. 4Status endpoint returns done with download URL

Edge cases

  • Idempotency key on POST prevents duplicate jobs
  • Worker retries capped at 3 with exponential backoff
  • Expired jobs pruned after 24 hours
  • Pre-signed URLs expire after 1 hour; re-fetch status for fresh link