Skip to content
Edge Functions

安排边缘函数

托管的 Supabase 平台支持 pg_cron 扩展,这是 Postgres 中的一个定期任务调度器。

🌐 The hosted Supabase Platform supports the pg_cron extension, a recurring job scheduler in Postgres.

结合pg_net扩展,这让我们可以按设定的时间表定期调用 Edge 函数。

🌐 In combination with the pg_net extension, this allows us to invoke Edge Functions periodically on a set schedule.

示例 #

🌐 Examples

每分钟调用一次 Edge 函数 #

🌐 Invoke an Edge Function every minute

project_urlpublishable_key 存储在 Supabase Vault 中:

🌐 Store project_url and publishable_key in Supabase Vault:

1
select vault.create_secret('https://project-ref.supabase.co', 'project_url');
2
select vault.create_secret('YOUR_SUPABASE_PUBLISHABLE_KEY', 'publishable_key');

每分钟向 Supabase Edge Function 发送一次 POST 请求:

🌐 Make a POST request to a Supabase Edge Function every minute:

1
select
2
cron.schedule(
3
'invoke-function-every-minute',
4
'* * * * *', -- every minute
5
$$
6
select
7
net.http_post(
8
url:= (select decrypted_secret from vault.decrypted_secrets where name = 'project_url') || '/functions/v1/function-name',
9
headers:=jsonb_build_object(
10
'Content-type', 'application/json',
11
'apikey', (select decrypted_secret from vault.decrypted_secrets where name = 'publishable_key')
12
),
13
body:=concat('{"time": "', now(), '"}')::jsonb
14
) as request_id;
15
$$
16
);

资源 #

🌐 Resources