Put work on a queue
The pattern behind every "we'll email you when it's ready".
This takes <n> seconds during a request and should be in the background: <paste> Stack: <stack>. I'd rather not run extra infrastructure unless needed. - Recommend the simplest queue that fits, and say what it costs to operate. - Enqueue returns immediately with an ID the user can poll or be notified against. - The job is safe to run twice. Say how you made it so. - Retries with backoff, a maximum attempt count, and a dead letter destination for jobs that never succeed. - I must be able to see: pending, running, failed, and why. Show me what the user sees at each stage, including failure.
A queue with no visibility into failures is a place jobs go to disappear. The failed-jobs view is not optional.