Skip to content

Commit 021f7da

Browse files
authored
[+] add more job control functions (#721)
- `timetable.pause_job()` - `timetable.resume_job()` - add schema migration
1 parent 12e9a8d commit 021f7da

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

internal/pgengine/migration.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ var Migrations func() migrator.Option = func() migrator.Option {
141141
return ExecuteMigrationScript(ctx, tx, "00629.sql")
142142
},
143143
},
144+
&migrator.Migration{
145+
Name: "00721 Add more job control functions",
146+
Func: func(ctx context.Context, tx pgx.Tx) error {
147+
return ExecuteMigrationScript(ctx, tx, "00721.sql")
148+
},
149+
},
144150
// adding new migration here, update "timetable"."migration" in "sql/init.sql"
145151
// and "dbapi" variable in main.go!
146152

internal/pgengine/sql/job_functions.sql

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ $$ LANGUAGE SQL;
125125

126126
COMMENT ON FUNCTION timetable.move_task_down IS 'Switch the order of the task execution with a following task within the chain';
127127

128+
-- delete_task() will delete the task from a chain
129+
CREATE OR REPLACE FUNCTION timetable.delete_task(IN task_id BIGINT) RETURNS boolean AS $$
130+
WITH del_task AS (DELETE FROM timetable.task WHERE task_id = $1 RETURNING task_id)
131+
SELECT EXISTS(SELECT 1 FROM del_task)
132+
$$ LANGUAGE SQL;
133+
134+
COMMENT ON FUNCTION timetable.delete_task IS 'Delete the task from a chain';
135+
128136
-- delete_job() will delete the chain and its tasks from the system
129137
CREATE OR REPLACE FUNCTION timetable.delete_job(IN job_name TEXT) RETURNS boolean AS $$
130138
WITH del_chain AS (DELETE FROM timetable.chain WHERE chain.chain_name = $1 RETURNING chain_id)
@@ -133,10 +141,18 @@ $$ LANGUAGE SQL;
133141

134142
COMMENT ON FUNCTION timetable.delete_job IS 'Delete the chain and its tasks from the system';
135143

136-
-- delete_task() will delete the task from a chain
137-
CREATE OR REPLACE FUNCTION timetable.delete_task(IN task_id BIGINT) RETURNS boolean AS $$
138-
WITH del_task AS (DELETE FROM timetable.task WHERE task_id = $1 RETURNING task_id)
139-
SELECT EXISTS(SELECT 1 FROM del_task)
144+
-- pause_job() will pause the chain (set live = false)
145+
CREATE OR REPLACE FUNCTION timetable.pause_job(IN job_name TEXT) RETURNS boolean AS $$
146+
WITH upd_chain AS (UPDATE timetable.chain SET live = false WHERE chain.chain_name = $1 RETURNING chain_id)
147+
SELECT EXISTS(SELECT 1 FROM upd_chain)
148+
$$ LANGUAGE SQL;
149+
150+
COMMENT ON FUNCTION timetable.pause_job IS 'Pause the chain (set live = false)';
151+
152+
-- resume_job() will resume the chain (set live = true)
153+
CREATE OR REPLACE FUNCTION timetable.resume_job(IN job_name TEXT) RETURNS boolean AS $$
154+
WITH upd_chain AS (UPDATE timetable.chain SET live = true WHERE chain.chain_name = $1 RETURNING chain_id)
155+
SELECT EXISTS(SELECT 1 FROM upd_chain)
140156
$$ LANGUAGE SQL;
141157

142-
COMMENT ON FUNCTION timetable.delete_task IS 'Delete the task from a chain';
158+
COMMENT ON FUNCTION timetable.resume_job IS 'Resume the chain (set live = true)';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- pause_job() will pause the chain (set live = false)
2+
CREATE OR REPLACE FUNCTION timetable.pause_job(IN job_name TEXT) RETURNS boolean AS $$
3+
WITH upd_chain AS (UPDATE timetable.chain SET live = false WHERE chain.chain_name = $1 RETURNING chain_id)
4+
SELECT EXISTS(SELECT 1 FROM upd_chain)
5+
$$ LANGUAGE SQL;
6+
7+
COMMENT ON FUNCTION timetable.pause_job IS 'Pause the chain (set live = false)';
8+
9+
-- resume_job() will resume the chain (set live = true)
10+
CREATE OR REPLACE FUNCTION timetable.resume_job(IN job_name TEXT) RETURNS boolean AS $$
11+
WITH upd_chain AS (UPDATE timetable.chain SET live = true WHERE chain.chain_name = $1 RETURNING chain_id)
12+
SELECT EXISTS(SELECT 1 FROM upd_chain)
13+
$$ LANGUAGE SQL;
14+
15+
COMMENT ON FUNCTION timetable.resume_job IS 'Resume the chain (set live = true)';

0 commit comments

Comments
 (0)