Unleash the power of automation by scheduling Supabase Edge Functions

Unleash the power of automation by scheduling Supabase Edge Functions


In my last article, we covered how to create a manage Supabase Edge Functions. Today I'll be covering the process to create a cron job to automate the execution of our edge functions to run at whatever time we needed to run them.

Getting started

Supabase allows you to schedule edge functions using a built-in Cron service. This Cron service is based on the widely-used Unix/Linux Cron system and allows you to define scheduled tasks simply and flexibly.

To schedule an edge function with Cron, you first need to create a new Cron job using Supabase's web-based management console. You can specify the exact schedule for your task using standard Cron syntax, which allows you to specify minute, hour, day, month, and weekday values.

For this, we can work with this very useful tool given by the Cronitor website, where you can play with the different combinations of timing and it will display a text telling you how your combination is going to be executed.

Activate Postgres functions

Once we got this, let's go back to our Supabase dashboard and activate a couple of extensions required. So, go to Database -> Extensions and search for these two:

  • PG_CRON is a simple cron-based job scheduler for PostgreSQL that runs inside the database.

  • PG_NET allows us to invoke Edge Functions periodically on a set schedule.

Create a cron job

After that, we're good to go. Let's create a new Query in the SQL Editor tab:

  1. Click on New Query

  2. Name it as per your requirements

  3. This line is to delete the cron scheduling (I'll be talking more about this later in this article)

  4. This is how will look like our scheduling

Cool! Let's break it down!

    '* * * * *', -- “At every minute.”

In this first part, we're creating a cron task from the PG_CRON extension. Inside we are gonna set a unique identifier for this task, and as a second parameter our created cron schedule timing from Cronitor (in the example I copy and paste it as default, which means run this task at every minute)

    '* * * * *', -- “At every minute.”
      select net.http_post(
                "Content-Type": "application/json", 
                "Authorization": "Bearer your-function-token"}'::jsonb
      ) as request_id;

Once you've defined your Cron job, you can then specify the edge function that should be executed when the job runs. Supabase will automatically invoke your function at the specified time and pass any necessary parameters to it. Two things to note here are:

  • the function is invoked inside two dollar sign $$ body

  • the function is invoked using the recently activated PG_NET extension

Our last step would be to execute our cron task:

It is going to give us back a schedule ID. We can check it is up and running by going to the Table Editor tab and clicking on the schema/cron option.

In there, you'll see the currently running job

and in job_run_details how many times has run until now, in my case, 4 minutes have passed so, four times.

Delete a cron job

Now, if you want to stop it or just change something in your function schedule, let's head back to our third bullet point in the creation steps: Delete a cron job.

Add this line to the top of your code and comment on the code snipped related to the creation of the cron job. This unique line allows you to stop it, by passing the unique identifier of your job.

select cron.unschedule('unique-dashed-identifier');

Final thoughts

Supabase's Cron service offers a convenient and flexible way to schedule and automate tasks in your application. With the ability to schedule edge functions, run SQL queries, execute shell scripts, and send notifications, Supabase Cron makes it easy to streamline your workflow and improve productivity.

Overall, Supabase Cron is a powerful tool that can help developers save time and simplify their development workflow. Whether you're running a small project or a large-scale application, Supabase Cron can help you automate your tasks and focus on what matters most - building great software.

In addition to scheduling edge functions, Supabase Cron also supports sending notifications, running SQL queries, and executing shell scripts, making it a powerful tool for automating a wide range of tasks in your application. But this is gonna be content for future posts.

See you in the next one!

Skadoosh GIFs | Tenor

Did you find this article valuable?

Support Alain Iglesias by becoming a sponsor. Any amount is appreciated!