7
4 Comments

Job queue for serverless apps #low-code

What's a job queue 🤔

A job queue allows apps to schedule jobs to run in the background.

No easy solution for serverless apps

Serverless apps are event-driven, so they only run when called upon. There's no running server that can host a job queue or even run cron-jobs.

But running jobs in background (at specific times) is a need that comes up all the time when building apps.

Idea 💡

So I decided to bootstrap a job queue using as little code as possible. My stack:

  • Airtable functions
  • Airtable scripting
  • Airtable automations
  • Nextjs/Vercel

Basically the app is in Nextjs hosted on Vercel with an endpoint /api/job where users can submit their job.

Here's an example api call to schedule a job:

fetch("https://www.tasklater.io/api/job", { 
    method: "POST", 
    headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        "x-api-key": "API_KEY_GOES_HERE", // replace with your API key
      }, 
    body: {
        timezone: "America/New_York", // optional (default: "America/New_York")
        run_time: 1639015224, // required (Unix timestamp)
        url: "https://www.example.com/api/send-delayed-email", // required (endpoint to trigger at run time)
        data: { // optional (data passed to your endpoint)
            email: "example@gmail.com"
        },
    }
});

Airtable Functions

The job gets recorded in airtable and there's one field that makes everything work. The to_run field is a function of whether the run_time is less than or equal to the current time. Seems like Airtable runs these functions every 10 minutes to update the fields.

That means, max 10 minutes after run_time Airtable will update the to_run field to 1.

(Usually delayed jobs are not sensitive to the exact minute, so it's a trade-off I'm willing to take)

Airtable Automations

The job then enters the "running" view in Airtable. That will run an Airtable automation that runs an Airtable script.

Airtable Scripting

The Airtable script does 2 things:

  1. logs what's happening into the logs table
  2. hits the endpoint that was requested to be hit at run_time

That's how Tasklater.io was born

You can currently get an API key at Tasklater.io and get started. This can only do delayed jobs for now, but there's more in the pipeline - here's the roadmap for who's interested.

And btw, it's free! Happy for anyone to test it out 😊

Comments/feedback are much appreciated!

on December 11, 2021
  1. 2

    Interesting concept, will try it out soon

    1. 1

      Cheers! Hit me up if you have questions ✌️

  2. 1

    How is this different from bull queue and agenda js npm packages?

    1. 1

      Good question, these are both great libs, although they're not fully compatible with serverless.

      You'll need to run a redis or mongo instance on some host and push your jobs to them. To process your jobs, you still need a running server. So you have to write a worker using these libs and host it somewhere.

      With Tasklater.io, no need to host anything, just send us your job by specifying its run_time and the url of the serverless function to trigger.