Discussions
How should an AI image generator handle long-running jobs on Cloudflare Workers?
13 days ago
I am building Photo2Ads, an AI product-ad studio:
The application uses TanStack Start, React 19, Cloudflare Workers, Cloudflare Workflows, D1, R2, and external image-generation providers.
The current workflow is:
- The user uploads a product photo.
- The server creates a pending generation record in D1.
- A Cloudflare Workflow consumes credits and submits the request to an AI provider.
- The Workflow polls the provider until the result is ready.
- The result is first exposed through the provider URL and then persisted to R2.
- The client polls the generation status and displays the result in the user's history.
I would like to hear how other developers handle the following edge cases:
- Making provider retries idempotent.
- Refunding credits when a provider fails after charging.
- Separating provider completion from durable R2 storage.
- Recovering unfinished jobs after a browser refresh.
- Handling tasks that take several minutes or never return.
What database state machine and retry strategy would you recommend for this type of AI generation workflow on Cloudflare Workers?