#https://zackproser.com/blog/how-to-run-background-jobs-on-vercel-without-a-queue

1 messages · Page 1 of 1 (latest)

royal jolt
#

How to Run Background Jobs on Vercel without using a queue

silk lance
#

There is a race condition in this code. You have to await starting background jobs in your POST handler

#

The fact that it’s been working is just luck

#

And that you’ve probably been testing it locally instead of in a serverless environment

#

Once a handler returns vercel terminates anything you have running in the background

#

Meaning the fetch operations to kick off your jobs in startBackgroundJob may not even execute by the time the POST function returns

royal jolt
#

Thanks for sharing your thoughts!

If you look carefully, you'll notice that the startBackgroundJobs function actually initiates a separate route's API call (/jobs

I've been successfully using this pattern in prod for a while now:

https://github.com/zackproser/panthalia/blob/main/app/api/jobs/route.ts

and I'm primarily concerned with its performance on Vercel - where it's worked without a hitch since I've deployed it.

Hope that helps!

GitHub

Contribute to zackproser/panthalia development by creating an account on GitHub.

silk lance
#

I’m referring to this line specifically in your blog post

#

This function returns a promise but it isn’t being awaited.

#

There’s no guarantee that it will finish calling its internal logic before the handler returns (when running in serverless environment like vercel),

#

It’s just timing luck that it has been running without issues. It can be tested by artificially slowing down the internals of startBackgroundJob function by adding a “await a sleep function for 5 seconds” before the fetch calls. Just make sure to test it on vercel not locally.