Schedule Supabase Dart edge function to run at midnight every day?

170 views Asked by At

A couple of questions on Supabase edge functions in DART...

a). Running a job at mid night every day

I want to execute a job at midnight every day to perform various end of day data summarization and snapshot tasks. Can this be setup as a DART edge function? If so, how I would schedule this? (Another reason for having these as an edge function is that I may wish to launch the 'end-of-day' process on demand via a web call).

b). Creating edge project within an existing client side Flutter project?

Can I create a new edge project a sub folder within an existing Flutter project? This is because I want to reuse data model classes already defined in the Flutter project.

2

There are 2 answers

0
dshukertjr On

You can setup cron jobs using pg_cron and pg_net in Supabase.

Once you enable the extensions in your Supabase dashboard, you can run a query something like this to setup cron jobs.

select
  cron.schedule(
    'invoke-function-every-minute',
    '0 0 * * *', -- every minute
    $$
    select
      net.http_post(
          url:='https://project-ref.supabase.co/functions/v1/function-name',
          headers:='{"Content-Type": "application/json", "Authorization": "Bearer YOUR_ANON_KEY"}'::jsonb,
          body:=concat('{"time": "', now(), '"}')::jsonb
      ) as request_id;
    $$
  );

For more details, you can follow the official guide: https://supabase.com/docs/guides/database/extensions/pg_cron

0
Babak Abadkheir On

there is another solution but I am not sure it is efficent or not.

  1. create a table called MyCronTable .

  2. create a cron with pg_cron to insert a record inside MyCronTable .

  3. create a edge function on supabase that listen to the table for data insertion.

  4. run the job if table data changes .

hint : you can hold the state of the Jobs inside MyCronTable like (init, success, failed) with proper timestamp for logs.