Skip to content

Commit c6cc954

Browse files
committed
[-] fix line endings in sample scripts
1 parent e483cf5 commit c6cc954

14 files changed

+422
-416
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"files.eol": "\n"
3+
}

samples/Autonomous.sql

+41-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
-- An advanced example showing how to use atutonomous tasks.
2-
-- This one-task chain will execute test_proc() procedure.
3-
-- Since procedure will make two commits (after f1() and f2())
4-
-- we cannot use it as a regular task, because all regular tasks
5-
-- must be executed in the context of a single chain transaction.
6-
-- Same rule applies for some other SQL commands,
7-
-- e.g. CREATE DATABASE, REINDEX, VACUUM, CREATE TABLESPACE, etc.
8-
CREATE OR REPLACE FUNCTION f (msg TEXT) RETURNS void AS $$
9-
BEGIN
10-
RAISE notice '%', msg;
11-
END;
12-
$$ LANGUAGE PLPGSQL;
13-
14-
CREATE OR REPLACE PROCEDURE test_proc () AS $$
15-
BEGIN
16-
PERFORM f('hey 1');
17-
COMMIT;
18-
PERFORM f('hey 2');
19-
COMMIT;
20-
END;
21-
$$
22-
LANGUAGE PLPGSQL;
23-
24-
WITH
25-
cte_chain (v_chain_id) AS (
26-
INSERT INTO timetable.chain (chain_name, run_at, max_instances, live, self_destruct)
27-
VALUES (
28-
'call proc() every 10 sec', -- chain_name,
29-
'@every 10 seconds', -- run_at,
30-
1, -- max_instances,
31-
TRUE, -- live,
32-
FALSE -- self_destruct
33-
) RETURNING chain_id
34-
),
35-
cte_task(v_task_id) AS (
36-
INSERT INTO timetable.task (chain_id, task_order, kind, command, ignore_error, autonomous)
37-
SELECT v_chain_id, 10, 'SQL', 'CALL test_proc()', TRUE, TRUE
38-
FROM cte_chain
39-
RETURNING task_id
40-
)
41-
SELECT v_chain_id, v_task_id FROM cte_task, cte_chain;
1+
-- An advanced example showing how to use atutonomous tasks.
2+
-- This one-task chain will execute test_proc() procedure.
3+
-- Since procedure will make two commits (after f1() and f2())
4+
-- we cannot use it as a regular task, because all regular tasks
5+
-- must be executed in the context of a single chain transaction.
6+
-- Same rule applies for some other SQL commands,
7+
-- e.g. CREATE DATABASE, REINDEX, VACUUM, CREATE TABLESPACE, etc.
8+
CREATE OR REPLACE FUNCTION f (msg TEXT) RETURNS void AS $$
9+
BEGIN
10+
RAISE notice '%', msg;
11+
END;
12+
$$ LANGUAGE PLPGSQL;
13+
14+
CREATE OR REPLACE PROCEDURE test_proc () AS $$
15+
BEGIN
16+
PERFORM f('hey 1');
17+
COMMIT;
18+
PERFORM f('hey 2');
19+
COMMIT;
20+
END;
21+
$$
22+
LANGUAGE PLPGSQL;
23+
24+
WITH
25+
cte_chain (v_chain_id) AS (
26+
INSERT INTO timetable.chain (chain_name, run_at, max_instances, live, self_destruct)
27+
VALUES (
28+
'call proc() every 10 sec', -- chain_name,
29+
'@every 10 seconds', -- run_at,
30+
1, -- max_instances,
31+
TRUE, -- live,
32+
FALSE -- self_destruct
33+
) RETURNING chain_id
34+
),
35+
cte_task(v_task_id) AS (
36+
INSERT INTO timetable.task (chain_id, task_order, kind, command, ignore_error, autonomous)
37+
SELECT v_chain_id, 10, 'SQL', 'CALL test_proc()', TRUE, TRUE
38+
FROM cte_chain
39+
RETURNING task_id
40+
)
41+
SELECT v_chain_id, v_task_id FROM cte_task, cte_chain;

samples/Basic.sql

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
SELECT timetable.add_job(
2-
job_name => 'notify every minute',
3-
job_schedule => '* * * * *',
4-
job_command => 'SELECT pg_notify($1, $2)',
5-
job_parameters => '[ "TT_CHANNEL", "Ahoj from SQL base task" ]' :: jsonb,
6-
job_kind => 'SQL'::timetable.command_kind,
7-
job_client_name => NULL,
8-
job_max_instances => 1,
9-
job_live => TRUE,
10-
job_self_destruct => FALSE,
11-
job_ignore_errors => TRUE
1+
SELECT timetable.add_job(
2+
job_name => 'notify every minute',
3+
job_schedule => '* * * * *',
4+
job_command => 'SELECT pg_notify($1, $2)',
5+
job_parameters => '[ "TT_CHANNEL", "Ahoj from SQL base task" ]' :: jsonb,
6+
job_kind => 'SQL'::timetable.command_kind,
7+
job_client_name => NULL,
8+
job_max_instances => 1,
9+
job_live => TRUE,
10+
job_self_destruct => FALSE,
11+
job_ignore_errors => TRUE
1212
) as chain_id;

samples/Chain.sql

+72-72
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
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;

samples/ClientMessages.sql

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
CREATE OR REPLACE FUNCTION raise_func(text)
2-
RETURNS void LANGUAGE plpgsql AS
3-
$BODY$
4-
BEGIN
5-
RAISE NOTICE 'Message by % from chain %: "%"',
6-
current_setting('pg_timetable.current_client_name')::text,
7-
current_setting('pg_timetable.current_chain_id')::text,
8-
$1;
9-
END;
10-
$BODY$;
11-
12-
SELECT timetable.add_job(
13-
job_name => 'raise client message every minute',
14-
job_schedule => '* * * * *',
15-
job_command => 'SELECT raise_func($1)',
16-
job_parameters => '[ "Hey from client messages task" ]' :: jsonb,
17-
job_kind => 'SQL'::timetable.command_kind,
18-
job_client_name => NULL,
19-
job_max_instances => 1,
20-
job_live => TRUE,
21-
job_self_destruct => FALSE,
22-
job_ignore_errors => TRUE
1+
CREATE OR REPLACE FUNCTION raise_func(text)
2+
RETURNS void LANGUAGE plpgsql AS
3+
$BODY$
4+
BEGIN
5+
RAISE NOTICE 'Message by % from chain %: "%"',
6+
current_setting('pg_timetable.current_client_name')::text,
7+
current_setting('pg_timetable.current_chain_id')::text,
8+
$1;
9+
END;
10+
$BODY$;
11+
12+
SELECT timetable.add_job(
13+
job_name => 'raise client message every minute',
14+
job_schedule => '* * * * *',
15+
job_command => 'SELECT raise_func($1)',
16+
job_parameters => '[ "Hey from client messages task" ]' :: jsonb,
17+
job_kind => 'SQL'::timetable.command_kind,
18+
job_client_name => NULL,
19+
job_max_instances => 1,
20+
job_live => TRUE,
21+
job_self_destruct => FALSE,
22+
job_ignore_errors => TRUE
2323
) as chain_id;

samples/CronStyle.sql

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
-- Create a job with the timetable.add_job function in cron style
2-
3-
-- In order to demonstrate Cron style schduling of job execution, we will create a table(One time) for inserting of data
4-
CREATE TABLE IF NOT EXISTS timetable.dummy_log (
5-
log_ID BIGSERIAL,
6-
event_name TEXT,
7-
timestmp TIMESTAMPTZ DEFAULT TRANSACTION_TIMESTAMP(),
8-
PRIMARY KEY (log_ID));
9-
10-
----CRON-Style
11-
-- * * * * * command to execute
12-
-- ┬ ┬ ┬ ┬ ┬
13-
-- │ │ │ │ │
14-
-- │ │ │ │ └──── day of the week (0 - 7) (Sunday to Saturday)(0 and 7 is Sunday);
15-
-- │ │ │ └────── month (1 - 12)
16-
-- │ │ └──────── day of the month (1 - 31)
17-
-- │ └────────── hour (0 - 23)
18-
-- └──────────── minute (0 - 59)
19-
20-
SELECT timetable.add_job (
21-
job_name => 'cron_Job run after 40th minutes after 2 hour on 27th of every month ',
22-
job_schedule => '40 */2 27 * *',
23-
job_command => $$INSERT INTO timetable.dummy_log (event_name) VALUES ('Cron test')$$
1+
-- Create a job with the timetable.add_job function in cron style
2+
3+
-- In order to demonstrate Cron style schduling of job execution, we will create a table(One time) for inserting of data
4+
CREATE TABLE IF NOT EXISTS timetable.dummy_log (
5+
log_ID BIGSERIAL,
6+
event_name TEXT,
7+
timestmp TIMESTAMPTZ DEFAULT TRANSACTION_TIMESTAMP(),
8+
PRIMARY KEY (log_ID));
9+
10+
----CRON-Style
11+
-- * * * * * command to execute
12+
-- ┬ ┬ ┬ ┬ ┬
13+
-- │ │ │ │ │
14+
-- │ │ │ │ └──── day of the week (0 - 7) (Sunday to Saturday)(0 and 7 is Sunday);
15+
-- │ │ │ └────── month (1 - 12)
16+
-- │ │ └──────── day of the month (1 - 31)
17+
-- │ └────────── hour (0 - 23)
18+
-- └──────────── minute (0 - 59)
19+
20+
SELECT timetable.add_job (
21+
job_name => 'cron_Job run after 40th minutes after 2 hour on 27th of every month ',
22+
job_schedule => '40 */2 27 * *',
23+
job_command => $$INSERT INTO timetable.dummy_log (event_name) VALUES ('Cron test')$$
2424
);

0 commit comments

Comments
 (0)