1- DO $$
2-
3- -- In order to create chain of tasks, We will create few base tasks and
4- -- each command_id will be associated with a task_id.
5- -- There will be only one HEAD chain (parent_id = null).
6- -- task_id of HEAD chain will be parent_id of other chains.
7-
8- DECLARE
9- v_parent_id bigint ;
10- v_task_id bigint ;
11- v_chain_id bigint ;
12- BEGIN
13- -- In order to implement chain pperation, we will create a table
14- CREATE TABLE IF NOT EXISTS timetable .chain_log (
15- chain_log BIGSERIAL ,
16- EVENT TEXT ,
17- time TIMESTAMPTZ ,
18- PRIMARY KEY (chain_log)
19- );
20-
21- -- Let's create a new chain and add tasks to it later
22- INSERT INTO timetable .chain (
23- chain_id,
24- chain_name,
25- run_at,
26- max_instances,
27- live,
28- self_destruct,
29- exclusive_execution
30- ) VALUES (
31- DEFAULT, -- chain_id,
32- ' chain operation' , -- chain_name
33- ' * * * * *' , -- run_at,
34- 1 , -- max_instances,
35- TRUE, -- live,
36- FALSE, -- self_destruct,
37- FALSE -- exclusive_execution,
38- ) RETURNING chain_id INTO v_chain_id;
39-
40- -- Add a head task
41- INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
42- VALUES (v_chain_id, 1 , ' INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)' , TRUE)
43- RETURNING task_id INTO v_parent_id;
44-
45- -- Add one more task, this task will keep parent_id value which is task_id of the HEAD task
46- INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
47- VALUES (v_chain_id, 2 , ' INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)' , TRUE)
48- RETURNING task_id INTO v_task_id;
49-
50- INSERT INTO timetable .parameter (task_id, order_id, value)
51- VALUES
52- -- Parameter for HEAD (parent) task
53- (v_parent_id, 1 , ' ["Added"]' :: jsonb),
54- -- Parameter for the next task
55- (v_task_id, 1 , ' ["Updated"]' :: jsonb);
56-
57- -- Add one more task swowing IDs for all tasks within the chain
58- INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
59- VALUES (v_chain_id, 3 ,
60- $CMD$
61- DO $BODY$
62- DECLARE tasks TEXT ;
63- BEGIN
64- SELECT array_agg(task_id ORDER BY task_order) FROM timetable .task
65- INTO tasks
66- WHERE chain_id = current_setting(' pg_timetable.current_chain_id' )::bigint ;
67- RAISE NOTICE ' Task IDs in chain: %' , tasks;
68- END;
69- $BODY$
70- $CMD$, TRUE);
71-
72- END;
1+ DO $$
2+
3+ -- In order to create chain of tasks, We will create few base tasks and
4+ -- each command_id will be associated with a task_id.
5+ -- There will be only one HEAD chain (parent_id = null).
6+ -- task_id of HEAD chain will be parent_id of other chains.
7+
8+ DECLARE
9+ v_parent_id bigint ;
10+ v_task_id bigint ;
11+ v_chain_id bigint ;
12+ BEGIN
13+ -- In order to implement chain pperation, we will create a table
14+ CREATE TABLE IF NOT EXISTS timetable .chain_log (
15+ chain_log BIGSERIAL ,
16+ EVENT TEXT ,
17+ time TIMESTAMPTZ ,
18+ PRIMARY KEY (chain_log)
19+ );
20+
21+ -- Let's create a new chain and add tasks to it later
22+ INSERT INTO timetable .chain (
23+ chain_id,
24+ chain_name,
25+ run_at,
26+ max_instances,
27+ live,
28+ self_destruct,
29+ exclusive_execution
30+ ) VALUES (
31+ DEFAULT, -- chain_id,
32+ ' chain operation' , -- chain_name
33+ ' * * * * *' , -- run_at,
34+ 1 , -- max_instances,
35+ TRUE, -- live,
36+ FALSE, -- self_destruct,
37+ FALSE -- exclusive_execution,
38+ ) RETURNING chain_id INTO v_chain_id;
39+
40+ -- Add a head task
41+ INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
42+ VALUES (v_chain_id, 1 , ' INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)' , TRUE)
43+ RETURNING task_id INTO v_parent_id;
44+
45+ -- Add one more task, this task will keep parent_id value which is task_id of the HEAD task
46+ INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
47+ VALUES (v_chain_id, 2 , ' INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)' , TRUE)
48+ RETURNING task_id INTO v_task_id;
49+
50+ INSERT INTO timetable .parameter (task_id, order_id, value)
51+ VALUES
52+ -- Parameter for HEAD (parent) task
53+ (v_parent_id, 1 , ' ["Added"]' :: jsonb),
54+ -- Parameter for the next task
55+ (v_task_id, 1 , ' ["Updated"]' :: jsonb);
56+
57+ -- Add one more task swowing IDs for all tasks within the chain
58+ INSERT INTO timetable .task (chain_id, task_order, command, ignore_error)
59+ VALUES (v_chain_id, 3 ,
60+ $CMD$
61+ DO $BODY$
62+ DECLARE tasks TEXT ;
63+ BEGIN
64+ SELECT array_agg(task_id ORDER BY task_order) FROM timetable .task
65+ INTO tasks
66+ WHERE chain_id = current_setting(' pg_timetable.current_chain_id' )::bigint ;
67+ RAISE NOTICE ' Task IDs in chain: %' , tasks;
68+ END;
69+ $BODY$
70+ $CMD$, TRUE);
71+
72+ END;
7373$$ LANGUAGE plpgsql;
0 commit comments