Hakolkal
Start a Project

Article · Published December 3, 2026

Running long AI jobs on serverless hosting without a server waiting

Long-running AI jobs fit badly on serverless hosting, where a server is only meant to run while it answers a request. An audio split or a video render takes minutes, and a server that waits for it costs money and loses the result when the connection drops. Here is how two of my products run those jobs in the background, what the user sees meanwhile, and what happens when a job fails.

Snapstems after a split: the separated tracks, each with its own player

Snapstems: a webhook calls back when the job is done

Snapstems is my AI stem separator, a paid web app at snapstems.com: drop a song, get the voice, drums and bass back as separate tracks. The separation runs on Replicate, a service that hosts AI models. With each split, my server gives Replicate a webhook: a return address to call when the work is finished.

The request answers at once with a job number, and my server sits idle while the model works. When the callback arrives, the server checks a secret, fetches the result from Replicate itself instead of trusting the message, and copies the tracks into my own storage. A callback delivered twice does nothing the second time.

Fashnable puts the work in a Cloud Tasks queue

Fashnable is my AI virtual try-on for online stores, at fashnable.app: drop a flat garment photo, get a video of an AI model wearing it. A render is two steps, a still image then the animation, and my own code drives both. The request saves the job, adds it to Cloud Tasks, Google's job queue, and answers at once.

The queue then calls a worker address on my server, protected by its own secret. Here a server is busy during the render, which the webhook avoids. But the work no longer hangs on a browser tab, and it has a budget of eight minutes, under the ten-minute limit of my hosting.

What the user sees while the job runs

In both products the page asks the server every three seconds whether the job is done. Each question is a short request, so a dropped connection loses nothing. Snapstems shows the upload percentage, then says that warming up the model can take one to three minutes. Fashnable names the step it is on.

People leave, so both products keep the result in a History list and send an email when a job was slow. After twelve minutes the Snapstems page stops asking and says the split will appear in History. Fashnable's step label advances on a timer: it is an estimate, not a measurement.

What happens when a background job fails

Credits are taken before a job starts, so every failure must return them unasked. In Snapstems, a split that cannot start tells the user no credits were used, offers a Try again button and alerts me by email. A split still running after ten minutes is cancelled and refunded.

In Fashnable, a failed render shows one sentence: "This render failed and your credits were refunded. Please try again." If a worker dies mid-render and the queue retries the task, the retry refunds once and stops, instead of paying the AI provider for the same render twice. In a four-photo shoot, only the failed photos are refunded.

Where the webhook and the queue stop

A webhook depends on the callback arriving. If one is lost, the job stays marked as running with the credits held. I have a report that lists jobs stuck that way, but the automatic repair is still on my list. A webhook also needs a provider that offers one.

A queue retries on its own, which helps until a task that can never succeed is retried for hours with the user's credits held. So I wait for the queue to be empty before each release. And the worker is still one web request: for a job longer than my hosting allows, I would use a dedicated machine.

What to decide before building long-running AI jobs

Three decisions come before the code. How long will a user wait: thirty seconds on the page, or ten minutes and an email? What do they see meanwhile: a percentage, a named step, a History entry? And what does a failed job cost you, and does the user get the credit back unasked?

Once those are answered, the choice between a callback and a queue mostly makes itself. The rest is engineering. If your product has a step that takes minutes, tell me about it and I will say how I would run it.

Want something like this?

Tell me what you are building, or what you are still doing by hand, and I will tell you how I would approach it. No commitment.