åæãšæ³šæäºé
- Supabase ã¢ã«ãŠã³ããäœææžã¿ã§ããããš
- Supabase ã®ãããžã§ã¯ããäœææžã¿ã§ããããš
ãã®èšäºã§ã¯ä»¥äžã®å ¬åŒããã¥ã¡ã³ããåèã«ããŸãã
pg_cron: Job Scheduling
The pg_cron extension is a simple cron-based job scheduler for PostgreSQL that runs inside the database.
supabase.com
ãã®èšäºã®ãŽãŒã«
ä»»æã® Supabase Database Function ã決ãŸã£ãæ¥æã«å®æå®è¡ã§ããããã«ããããšããã®èšäºã®ãŽãŒã«ã§ãã
pg_cron ãæå¹åãã
ãŸãã¯ããã«ãSupabase ã§pc_cron
ã䜿ããããã«ããããã«æå¹åããå¿
èŠããããŸãã
以äžã®ããã« Supabase ã® Web 管çç»é¢ã®ãµã€ãã¡ãã¥ãŒã«ãããDatabaseãããŒãžã®ãExtensionsãããŒãžã§ãpg_cron
ãšæ€çŽ¢ããŸãããããŠä»¥äžã®ããã«è¡šç€ºãããpg_cron
ã®ã¹ã€ããã ON ã«ããŸãã
ON ã«ãããšä»¥äžã®ããã«ç¢ºèªãã€ã¢ãã°ãéãã®ã§ãEnable extension
ãã¿ã³ãã¯ãªãã¯ããŸãã
Select a schema to enable the extension for
ã«ã€ããŠã¯ãextension
ãéžæããŸãã
以äžã®ããã« ON ã«ãªããŸãããããã§ã¹ã±ãžã¥ãŒã«ãå®æçã«å®è¡ããããã®pg_cron
ã Supabase å
ã§äœ¿çšã§ããããã«ãªããŸãã
以äžã§pg_cron
ã®æå¹åãå®äºã§ãã
ã¹ã±ãžã¥ãŒã«ãç»é²ãã Database Function
ãã¹ãçšã«ä»¥äžã®ãããªhello world
ãšè¡šç€ºããã ãã® Database Function ãhello_world()
ãšããåå㧠Supabase ã® Web 管çç»é¢ã«ããSQL Editor
ã§äœæããŠãããŸãã
create or replace function hello_world()
returns text
language sql
as $$
select 'hello world';
$$;
ãããŠåæ§ã«SQL Editor
ã§ä»¥äžã®ãã㪠Database Function äœæããRun
ãã¯ãªãã¯ããŠå®è¡ããŸãã
äžèšã§ã¯ã以äžã®ããã«cron.schedule
ã䜿çšããŠãhello_world
ãšãã Database Function ãæ¯æ¥ 13:30 ã«å®è¡ããããã«ç»é²ããŠããŸãã
select
cron.schedule(
'invoke-hello-world', -- ã¹ã±ãžã¥ãŒã«å
'30 4 * * *', -- æ¯æ¥13:30ã«å®è¡ãã
$$
select hello_world(); -- å®è¡ããå
容
$$
);
å®è¡ãããšç»åã«ããããã«cron.schedule
ã®å®è¡çµæãšããŠç»é²ããã¹ã±ãžã¥ãŒã«ã® IDïŒäžèšç»åå
ã ãš8
ïŒã衚瀺ãããŸãã
ããã§ã¹ã±ãžã¥ãŒã«ç»é²ãå®äºããæ¯æ¥ 13:30 ã«hello_world
ãšãã Database Function ãå®è¡ãããããã«ãªããŸãã
ãªãããã以äžã®ããã«schema "cron" does not exist
ãšãããšã©ãŒã衚瀺ãããå Žåã¯ãpg_cron
ãæå¹ã«ãªã£ãŠããªãããã«çºçããŸããå床pg_cron
ãæå¹ã«ãªã£ãŠãããšç¢ºèªãããšè¯ããããããŸããã
ã¹ã±ãžã¥ãŒã«ã®æå®ã«ã€ããŠ
äžèšã®cron.schedule
ã§ã¯ã30 4 * * *
ã®éšåã§13:30ã«å®è¡ããããã«æå®ããŠããŸãã
ããã¯cron
ãšããLinuxã§éåžžã«ãã䜿ãããã¹ã±ãžã¥ãŒã«å®è¡ã®ããã®ä»çµã¿ã䜿çšããŠããŸãã
30 4 * * *
ã®éšåã¯ãå æ æ¥ æ ææ¥
ã®é çªã§æå®ã§ããŸãããã£ãŠ30 4 * * *
ã¯ãæ¯æ¥ 4 æ 30 åã«å®è¡ãããšããæå³ã«ãªããŸãã
ãã ãããã©ã«ãã ãš UTC ã§ã®æå®ã«ãªããããæ¥æ¬æéã§ç®çã®æéã«æå®ããããã«ã13æã§ããã°-9æéã§4æãæå®ããŠããŸãã
cron
ã§ã©ã®ãããªæå®ãã§ãããã«ã€ããŠã¯ã以äžã®å
¬åŒããã¥ã¡ã³ããåèã«ããŠãã ããã
ãå ¥éãcronïŒã¯ãã³ïŒèšå®ã»æžãæ¹ã®åºæ¬
LinuxãµãŒããŒã®ç®¡çãããŠãããšãããã°ã©ã ãå®æçã«å®è¡ãããå ŽåããããŸãããã®ãããªãšãã¯ããcronããšããæ©èœããè©Šããã ããã
kagoya.jp
ãŸããèªåã®cron
ã®å
容ãå®éã«ã©ã®æ¥æã«å®è¡ããããã確èªãããå Žåã¯ã以äžã®ãµã€ãã䟿å©ã§ãã
äœææžã¿ã®ã¹ã±ãžã¥ãŒã«äžèŠ§ã確èªãã
以äžã® Database Function ãå®è¡ããããšã§ãäœææžã¿ã®ã¹ã±ãžã¥ãŒã«ãžã§ãäžèŠ§ã確èªã§ããŸãã
select * from cron.job;
以äžã Supabase ã® Web 管çç»é¢ã§äžèšã®ã¯ãšãªãå®è¡ããäŸã§ãã
ãªãã以äžã®ããã« Supabase ã® Web 管çç»é¢ã«ãã Table Editor ã§ãåæ§ã«ã¹ã±ãžã¥ãŒã«ãžã§ãã確èªã§ããŸããpg_cron
ã§äœæããã¹ã±ãžã¥ãŒã«ãžã§ãã¯job
ãå®è¡ããã¹ã±ãžã¥ãŒã«ãžã§ãã®å±¥æŽã¯job_run_details
ã§ç¢ºèªã§ããŸãããªããããããcron
ã¹ããŒãã®äžã«ãããŸãã
äœææžã¿ã®ã¹ã±ãžã¥ãŒã«ãæŽæ°ãã
èšäºå
¬éæç¹ã§ã¯ãã¹ã±ãžã¥ãŒã«åãåãcron.schedule
ãå床å®è¡ããã°æ°ããå
容ã§æŽæ°ãããŸãã
select
cron.schedule(
'invoke-hello-world',
'0 10 * * *', -- æ¯æ¥19æã«å®è¡ãã
$$
select hello_world();
$$
);
ãªããã¹ã±ãžã¥ãŒã«ã®æŽæ°ã«ã¯alter_job
ã䜿çšããããšãã§ããŸãã
äŸãã°ã以äžã¯alter_job
ã䜿çšããŠã¹ã±ãžã¥ãŒã«ã®ã¿ãæŽæ°ããŠããŸãã
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'invoke-hello-world'),
'0 10 * * *', -- æ¯æ¥19æã«å®è¡ãã
);
äžèšã§ã¯,job_id
ãšããŠselect jobid from cron.job where jobname = 'invoke-hello-world'
ã®å€ã䜿ã£ãŠããŸãããããã¯åçŽã«invoke-hello-world
ãšããååã®ã¹ã±ãžã¥ãŒã«ã® ID ãååŸããŠããŸãã
ããç®çã®job_id
ãããã£ãŠããå Žåã¯ããã®ãŸãŸãã®å€ãæž¡ã㊠OK ã§ãã
ãããŠã¹ã±ãžã¥ãŒã«ãžã§ãã®æŽæ°ãšããŠã¯alter_job
ãæ¬æ¥ã®æ¹æ³ã§ãalter_job
ã®æ¹ããã现ããèšå®ãæŽæ°ããããšãã§ããŸãã
äŸãã°ãalter_job
ã§ã¯ä»¥äžã®å
容ãç·šéããããšãã§ããŸãã
cron.alter_job(
job_id bigint,
schedule text default null,
command text default null,
database text default null,
username text default null,
active boolean default null
)
詳ããã¯ä»¥äžã®å ¬åŒããã¥ã¡ã³ãã«èšèŒãããŠããŸãã
ã¹ã±ãžã¥ãŒã«ãäžæåæ¢/åéãã
alter_job
ã䜿çšããããšã§ãäœææžã¿ã®ã¹ã±ãžã¥ãŒã«ãžã§ããäžæåæ¢ããããšã§ããŸãã
以äžã®ããã«active
ã«false
ãæå®ããããšã§ãã¹ã±ãžã¥ãŒã«ãžã§ããäžæåæ¢ããããšãã§ããŸãã
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'invoke-hello-world'),
active := false
);
å床ã¹ã±ãžã¥ãŒã«ãžã§ããåéããå Žåã¯ã以äžã®ããã«active
ã«true
ãæå®ããããšã§ãã¹ã±ãžã¥ãŒã«ãžã§ããåéããããšãã§ããŸãã
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'invoke-hello-world'),
active := true
);
ã¹ã±ãžã¥ãŒã«ãåé€ãã
以äžã®ã¯ãšãªãå®è¡ããããšã§ç»é²ããã¹ã±ãžã¥ãŒã«ãåé€ã§ããŸãã
select cron.unschedule('invoke-hello-world');
äžèšãå®è¡åŸãå床select * from cron.job;
ãå®è¡ããŠã¿ããšãåé€ãããŠããããšã確èªã§ãããšæããŸãã
ãŸãšã
ãã®èšäºã§ã¯ãhello world
ã衚瀺ããã ãã® Database Function ãã¹ã±ãžã¥ãŒã«å®è¡ãããããå®çšæ§ã¯ãããŸããããhello_world()
ã®ä»£ããã«ä»ã®å¥œã㪠Databse Function ãå®è¡ã§ããŸãã
ãã®ãã Supabase ã®äžã§å¥œããªåŠçãã¹ã±ãžã¥ãŒã«å®è¡ãã§ããäŸãã°æ¯æ¥ïŒæã«ç¹å®ã®ããŒãã«ã®ããŒã¿ãäžæãããããã€ãªãŒã¬ããŒããã¡ãŒã«ãã¬ãžã³ã®ãããªå
容ãæ¯æ¥æ±ºãŸã£ãæéã«ã¡ãŒã«ãéä¿¡ããããªã©è²ã
ãªããšãã§ããããã«ãªããŸãã