1
- SELECT timetable .add_job (
2
- job_name => ' HelloWorld' || g .i ,
3
- job_schedule => ' * * * * *' ,
4
- job_kind => ' PROGRAM' ::timetable .command_kind ,
5
- job_command => ' bash' ,
6
- job_parameters => jsonb_build_array(' -c' , ' echo Hello World from ' || g .i ),
7
- job_live => TRUE,
8
- job_ignore_errors => TRUE
9
- ) as chain_id FROM generate_series(1 , 500 ) AS g(i);
1
+ WITH
2
+ cte_chain (v_chain_id) AS ( -- let's create a new chain and add tasks to it later
3
+ INSERT INTO timetable .chain (chain_name, run_at, max_instances, live)
4
+ VALUES (' many tasks' , ' * * * * *' , 1 , true)
5
+ RETURNING chain_id
6
+ ),
7
+ cte_tasks(v_task_id) AS ( -- now we'll add 500 tasks to the chain, some of them will fail
8
+ INSERT INTO timetable .task (chain_id, task_order, kind, command, ignore_error)
9
+ SELECT v_chain_id, g .s , ' SQL' , ' SELECT 1.0 / round(random())::int4;' , true
10
+ FROM cte_chain, generate_series(1 , 500 ) AS g(s)
11
+ RETURNING task_id
12
+ ),
13
+ report_task(v_task_id) AS ( -- and the last reporting task will calculate the statistic
14
+ INSERT INTO timetable .task (chain_id, task_order, kind, command)
15
+ SELECT v_chain_id, 501 , ' SQL' , $CMD$DO
16
+ $$
17
+ DECLARE
18
+ s TEXT ;
19
+ BEGIN
20
+ WITH report AS (
21
+ SELECT
22
+ count (* ) FILTER (WHERE returncode = 0 ) AS success,
23
+ count (* ) FILTER (WHERE returncode != 0 ) AS fail,
24
+ count (* ) AS total
25
+ FROM timetable .execution_log
26
+ WHERE chain_id = current_setting(' pg_timetable.current_chain_id' )::bigint
27
+ AND txid = txid_current()
28
+ )
29
+ SELECT ' Tasks executed:' || total ||
30
+ ' ; succeeded: ' || success ||
31
+ ' ; failed: ' || fail ||
32
+ ' ; ratio: ' || 100 .0 * success/ GREATEST(total,1 )
33
+ INTO s
34
+ FROM report;
35
+ RAISE NOTICE ' %' , s;
36
+ END;
37
+ $$
38
+ $CMD$
39
+ FROM cte_chain
40
+ RETURNING task_id
41
+ )
42
+ SELECT v_chain_id FROM cte_chain
0 commit comments