Skip to content

Commit

Permalink
[-] fix line endings in sample scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Nov 28, 2023
1 parent e483cf5 commit c6cc954
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 416 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files.eol": "\n"
}
82 changes: 41 additions & 41 deletions samples/Autonomous.sql
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
-- An advanced example showing how to use atutonomous tasks.
-- This one-task chain will execute test_proc() procedure.
-- Since procedure will make two commits (after f1() and f2())
-- we cannot use it as a regular task, because all regular tasks
-- must be executed in the context of a single chain transaction.
-- Same rule applies for some other SQL commands,
-- e.g. CREATE DATABASE, REINDEX, VACUUM, CREATE TABLESPACE, etc.
CREATE OR REPLACE FUNCTION f (msg TEXT) RETURNS void AS $$
BEGIN
RAISE notice '%', msg;
END;
$$ LANGUAGE PLPGSQL;

CREATE OR REPLACE PROCEDURE test_proc () AS $$
BEGIN
PERFORM f('hey 1');
COMMIT;
PERFORM f('hey 2');
COMMIT;
END;
$$
LANGUAGE PLPGSQL;

WITH
cte_chain (v_chain_id) AS (
INSERT INTO timetable.chain (chain_name, run_at, max_instances, live, self_destruct)
VALUES (
'call proc() every 10 sec', -- chain_name,
'@every 10 seconds', -- run_at,
1, -- max_instances,
TRUE, -- live,
FALSE -- self_destruct
) RETURNING chain_id
),
cte_task(v_task_id) AS (
INSERT INTO timetable.task (chain_id, task_order, kind, command, ignore_error, autonomous)
SELECT v_chain_id, 10, 'SQL', 'CALL test_proc()', TRUE, TRUE
FROM cte_chain
RETURNING task_id
)
SELECT v_chain_id, v_task_id FROM cte_task, cte_chain;
-- An advanced example showing how to use atutonomous tasks.
-- This one-task chain will execute test_proc() procedure.
-- Since procedure will make two commits (after f1() and f2())
-- we cannot use it as a regular task, because all regular tasks
-- must be executed in the context of a single chain transaction.
-- Same rule applies for some other SQL commands,
-- e.g. CREATE DATABASE, REINDEX, VACUUM, CREATE TABLESPACE, etc.
CREATE OR REPLACE FUNCTION f (msg TEXT) RETURNS void AS $$
BEGIN
RAISE notice '%', msg;
END;
$$ LANGUAGE PLPGSQL;

CREATE OR REPLACE PROCEDURE test_proc () AS $$
BEGIN
PERFORM f('hey 1');
COMMIT;
PERFORM f('hey 2');
COMMIT;
END;
$$
LANGUAGE PLPGSQL;

WITH
cte_chain (v_chain_id) AS (
INSERT INTO timetable.chain (chain_name, run_at, max_instances, live, self_destruct)
VALUES (
'call proc() every 10 sec', -- chain_name,
'@every 10 seconds', -- run_at,
1, -- max_instances,
TRUE, -- live,
FALSE -- self_destruct
) RETURNING chain_id
),
cte_task(v_task_id) AS (
INSERT INTO timetable.task (chain_id, task_order, kind, command, ignore_error, autonomous)
SELECT v_chain_id, 10, 'SQL', 'CALL test_proc()', TRUE, TRUE
FROM cte_chain
RETURNING task_id
)
SELECT v_chain_id, v_task_id FROM cte_task, cte_chain;
22 changes: 11 additions & 11 deletions samples/Basic.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
SELECT timetable.add_job(
job_name => 'notify every minute',
job_schedule => '* * * * *',
job_command => 'SELECT pg_notify($1, $2)',
job_parameters => '[ "TT_CHANNEL", "Ahoj from SQL base task" ]' :: jsonb,
job_kind => 'SQL'::timetable.command_kind,
job_client_name => NULL,
job_max_instances => 1,
job_live => TRUE,
job_self_destruct => FALSE,
job_ignore_errors => TRUE
SELECT timetable.add_job(
job_name => 'notify every minute',
job_schedule => '* * * * *',
job_command => 'SELECT pg_notify($1, $2)',
job_parameters => '[ "TT_CHANNEL", "Ahoj from SQL base task" ]' :: jsonb,
job_kind => 'SQL'::timetable.command_kind,
job_client_name => NULL,
job_max_instances => 1,
job_live => TRUE,
job_self_destruct => FALSE,
job_ignore_errors => TRUE
) as chain_id;
144 changes: 72 additions & 72 deletions samples/Chain.sql
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
DO $$

-- In order to create chain of tasks, We will create few base tasks and
-- each command_id will be associated with a task_id.
-- There will be only one HEAD chain (parent_id = null).
-- task_id of HEAD chain will be parent_id of other chains.

DECLARE
v_parent_id bigint;
v_task_id bigint;
v_chain_id bigint;
BEGIN
-- In order to implement chain pperation, we will create a table
CREATE TABLE IF NOT EXISTS timetable.chain_log (
chain_log BIGSERIAL,
EVENT TEXT,
time TIMESTAMPTZ,
PRIMARY KEY (chain_log)
);

-- Let's create a new chain and add tasks to it later
INSERT INTO timetable.chain (
chain_id,
chain_name,
run_at,
max_instances,
live,
self_destruct,
exclusive_execution
) VALUES (
DEFAULT, -- chain_id,
'chain operation', -- chain_name
'* * * * *', -- run_at,
1, -- max_instances,
TRUE, -- live,
FALSE, -- self_destruct,
FALSE -- exclusive_execution,
) RETURNING chain_id INTO v_chain_id;

--Add a head task
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 1, 'INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)', TRUE)
RETURNING task_id INTO v_parent_id;

-- Add one more task, this task will keep parent_id value which is task_id of the HEAD task
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 2, 'INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)', TRUE)
RETURNING task_id INTO v_task_id;

INSERT INTO timetable.parameter(task_id, order_id, value)
VALUES
-- Parameter for HEAD (parent) task
(v_parent_id, 1, '["Added"]' :: jsonb),
-- Parameter for the next task
(v_task_id, 1, '["Updated"]' :: jsonb);

-- Add one more task swowing IDs for all tasks within the chain
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 3,
$CMD$
DO $BODY$
DECLARE tasks TEXT;
BEGIN
SELECT array_agg(task_id ORDER BY task_order) FROM timetable.task
INTO tasks
WHERE chain_id = current_setting('pg_timetable.current_chain_id')::bigint;
RAISE NOTICE 'Task IDs in chain: %', tasks;
END;
$BODY$
$CMD$, TRUE);

END;
DO $$

-- In order to create chain of tasks, We will create few base tasks and
-- each command_id will be associated with a task_id.
-- There will be only one HEAD chain (parent_id = null).
-- task_id of HEAD chain will be parent_id of other chains.

DECLARE
v_parent_id bigint;
v_task_id bigint;
v_chain_id bigint;
BEGIN
-- In order to implement chain pperation, we will create a table
CREATE TABLE IF NOT EXISTS timetable.chain_log (
chain_log BIGSERIAL,
EVENT TEXT,
time TIMESTAMPTZ,
PRIMARY KEY (chain_log)
);

-- Let's create a new chain and add tasks to it later
INSERT INTO timetable.chain (
chain_id,
chain_name,
run_at,
max_instances,
live,
self_destruct,
exclusive_execution
) VALUES (
DEFAULT, -- chain_id,
'chain operation', -- chain_name
'* * * * *', -- run_at,
1, -- max_instances,
TRUE, -- live,
FALSE, -- self_destruct,
FALSE -- exclusive_execution,
) RETURNING chain_id INTO v_chain_id;

--Add a head task
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 1, 'INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)', TRUE)
RETURNING task_id INTO v_parent_id;

-- Add one more task, this task will keep parent_id value which is task_id of the HEAD task
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 2, 'INSERT INTO timetable.chain_log (EVENT, time) VALUES ($1, CURRENT_TIMESTAMP)', TRUE)
RETURNING task_id INTO v_task_id;

INSERT INTO timetable.parameter(task_id, order_id, value)
VALUES
-- Parameter for HEAD (parent) task
(v_parent_id, 1, '["Added"]' :: jsonb),
-- Parameter for the next task
(v_task_id, 1, '["Updated"]' :: jsonb);

-- Add one more task swowing IDs for all tasks within the chain
INSERT INTO timetable.task (chain_id, task_order, command, ignore_error)
VALUES (v_chain_id, 3,
$CMD$
DO $BODY$
DECLARE tasks TEXT;
BEGIN
SELECT array_agg(task_id ORDER BY task_order) FROM timetable.task
INTO tasks
WHERE chain_id = current_setting('pg_timetable.current_chain_id')::bigint;
RAISE NOTICE 'Task IDs in chain: %', tasks;
END;
$BODY$
$CMD$, TRUE);

END;
$$ LANGUAGE plpgsql;
44 changes: 22 additions & 22 deletions samples/ClientMessages.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
CREATE OR REPLACE FUNCTION raise_func(text)
RETURNS void LANGUAGE plpgsql AS
$BODY$
BEGIN
RAISE NOTICE 'Message by % from chain %: "%"',
current_setting('pg_timetable.current_client_name')::text,
current_setting('pg_timetable.current_chain_id')::text,
$1;
END;
$BODY$;

SELECT timetable.add_job(
job_name => 'raise client message every minute',
job_schedule => '* * * * *',
job_command => 'SELECT raise_func($1)',
job_parameters => '[ "Hey from client messages task" ]' :: jsonb,
job_kind => 'SQL'::timetable.command_kind,
job_client_name => NULL,
job_max_instances => 1,
job_live => TRUE,
job_self_destruct => FALSE,
job_ignore_errors => TRUE
CREATE OR REPLACE FUNCTION raise_func(text)
RETURNS void LANGUAGE plpgsql AS
$BODY$
BEGIN
RAISE NOTICE 'Message by % from chain %: "%"',
current_setting('pg_timetable.current_client_name')::text,
current_setting('pg_timetable.current_chain_id')::text,
$1;
END;
$BODY$;

SELECT timetable.add_job(
job_name => 'raise client message every minute',
job_schedule => '* * * * *',
job_command => 'SELECT raise_func($1)',
job_parameters => '[ "Hey from client messages task" ]' :: jsonb,
job_kind => 'SQL'::timetable.command_kind,
job_client_name => NULL,
job_max_instances => 1,
job_live => TRUE,
job_self_destruct => FALSE,
job_ignore_errors => TRUE
) as chain_id;
46 changes: 23 additions & 23 deletions samples/CronStyle.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- Create a job with the timetable.add_job function in cron style

-- In order to demonstrate Cron style schduling of job execution, we will create a table(One time) for inserting of data
CREATE TABLE IF NOT EXISTS timetable.dummy_log (
log_ID BIGSERIAL,
event_name TEXT,
timestmp TIMESTAMPTZ DEFAULT TRANSACTION_TIMESTAMP(),
PRIMARY KEY (log_ID));

----CRON-Style
-- * * * * * command to execute
-- ┬ ┬ ┬ ┬ ┬
-- │ │ │ │ │
-- │ │ │ │ └──── day of the week (0 - 7) (Sunday to Saturday)(0 and 7 is Sunday);
-- │ │ │ └────── month (1 - 12)
-- │ │ └──────── day of the month (1 - 31)
-- │ └────────── hour (0 - 23)
-- └──────────── minute (0 - 59)

SELECT timetable.add_job (
job_name => 'cron_Job run after 40th minutes after 2 hour on 27th of every month ',
job_schedule => '40 */2 27 * *',
job_command => $$INSERT INTO timetable.dummy_log (event_name) VALUES ('Cron test')$$
-- Create a job with the timetable.add_job function in cron style

-- In order to demonstrate Cron style schduling of job execution, we will create a table(One time) for inserting of data
CREATE TABLE IF NOT EXISTS timetable.dummy_log (
log_ID BIGSERIAL,
event_name TEXT,
timestmp TIMESTAMPTZ DEFAULT TRANSACTION_TIMESTAMP(),
PRIMARY KEY (log_ID));

----CRON-Style
-- * * * * * command to execute
-- ┬ ┬ ┬ ┬ ┬
-- │ │ │ │ │
-- │ │ │ │ └──── day of the week (0 - 7) (Sunday to Saturday)(0 and 7 is Sunday);
-- │ │ │ └────── month (1 - 12)
-- │ │ └──────── day of the month (1 - 31)
-- │ └────────── hour (0 - 23)
-- └──────────── minute (0 - 59)

SELECT timetable.add_job (
job_name => 'cron_Job run after 40th minutes after 2 hour on 27th of every month ',
job_schedule => '40 */2 27 * *',
job_command => $$INSERT INTO timetable.dummy_log (event_name) VALUES ('Cron test')$$
);
Loading

0 comments on commit c6cc954

Please sign in to comment.