How do I run background or cron jobs?

Maybe you want to send emails, process data, run some lengthly calculations, fan out tasks to/from a queue. How would I do that with RedwoodJS? Truth be told, the long running and/or scheduled job has been a missing piece in the Jamstack puzzle for some time.

Bear in mind that you can deploy your RedwoodJS app to a serverless (Netlify, Vercel) or a serverful (Render, bare metal) provider. In the serverless world, lambdas only run for a short time – typically 10 seconds. So, your job you have to run rather quick.

Thankfully, there are now some options.

  1. If you deploy to a serverful context, like Render then you do have access to cron, redis, and a job runner that can perform these tasks. You may want to consider Creating a Background Worker with Exec and Faktory and Redwood’s exec script CLI command.

  2. Netlify’s background functions allow you to set up serverless function processes that run longer than 10 seconds and up to 15 minutes.

  3. Supabase offers cron inside Postgres where you can schedule requests that RedwoodJS can consume via a signed, secure webhook. It’s a great fit for that asynchronous background Netlify function.

  4. Want integrated cron? Netlify recently added scheduled functions that enable you to run them on a regular and consistent schedule, much like a cron job. But, you can define that schedule in code like schedule("@hourly", handler).

  5. Repeater is a Job Queue as a Service … service. It provides an API for you to schedule tasks that will call an http endpoint to run (similar to the Supabase cron approach).

  6. bare metal job running option TODO: @rob can you explain this? As done in Algostake

I’m going to be updating the baremetal deploy docs with this info soon, but here’s the Stack Overflow post that I used to unlock the magic! How to make a task job with PM2? - Stack Overflow

Note that you don’t have to use the baremetal deploy option to do this, but you do need a real server that you can connect to and run pm2.