具有 cron 作业的 Postgres 无法根据获取请求工作

Postgres with cron job doesn't work on get request

我正在做一个 cron 作业,每分钟 return 个结果。

select
  cron.schedule(
    'webhook-every-minutex', -- name of the cron job
    '* * * * *', -- every minute
    $$
    select content::json->'results'
from http_get('https://swapi.dev/api/people');
    $$
  );

不幸的是,它没有 return 结果,但是当我手动 运行 select 查询时,它 returns json 结果。 (代码在supabase上测试过)

我的目标是每分钟 运行 以上 cron 作业。

一个测试用例:

SELECT cron.schedule('test', '* * * * *', $$SELECT 1$$);
 schedule 
----------
        4
--Where 4 is job number

--In Postgres log
2022-03-14 11:18:00.001 PDT [63241] LOG:  cron job 4 starting: SELECT 1
2022-03-14 11:18:00.007 PDT [63241] LOG:  cron job 4 completed: 1 row
2022-03-14 11:19:00.002 PDT [63241] LOG:  cron job 4 starting: SELECT 1
2022-03-14 11:19:00.008 PDT [63241] LOG:  cron job 4 completed: 1 row
2022-03-14 11:20:00.002 PDT [63241] LOG:  cron job 4 starting: SELECT 1
2022-03-14 11:20:00.010 PDT [63241] LOG:  cron job 4 completed: 1 row

psql 中没有显示任何内容,我认为这就是您所指的控制台。

所以:

create table cron_test(fld_1 int, ts timestamptz  default now());

SELECT cron.schedule('test', '* * * * *', $$insert into cron_test(fld_1) select 1$$);
 schedule 
----------
        4

--Postgres log
2022-03-14 11:22:00.001 PDT [63241] LOG:  cron job 4 starting: insert into cron_test(fld_1) select 1
2022-03-14 11:22:00.008 PDT [63241] LOG:  cron job 4 COMMAND completed: INSERT 0 1 1
2022-03-14 11:23:00.002 PDT [63241] LOG:  cron job 4 starting: insert into cron_test(fld_1) select 1
2022-03-14 11:23:00.012 PDT [63241] LOG:  cron job 4 COMMAND completed: INSERT 0 1 1
2022-03-14 11:24:00.001 PDT [63241] LOG:  cron job 4 starting: insert into cron_test(fld_1) select 1
2022-03-14 11:24:00.006 PDT [63241] LOG:  cron job 4 COMMAND completed: INSERT 0 1 1
2022-03-14 11:25:00.003 PDT [63241] LOG:  cron job 4 starting: insert into cron_test(fld_1) select 1
2022-03-14 11:25:00.010 PDT [63241] LOG:  cron job 4 COMMAND completed: INSERT 0 1 1


select * from cron_test ;
 fld_1 |              ts               
-------+-------------------------------
     1 | 2022-03-14 11:22:00.007153-07
     1 | 2022-03-14 11:23:00.010206-07
     1 | 2022-03-14 11:24:00.004967-07
     1 | 2022-03-14 11:25:00.008177-07

如果您想查看命令的结果,您需要将它们放在您可以检索的地方。问题是 pg_cron 打开一个单独的 libpq 连接到 运行 作业。这意味着您将不会在启动作业的控制台中看到结果。