Skip to content

Commit

Permalink
Merge pull request citusdata#47 from Voipfuture-GmbH/master
Browse files Browse the repository at this point in the history
Add a 'active' column on con.job table to simply enable and disable jobs
  • Loading branch information
marcocitus authored May 3, 2018
2 parents dc4b118 + 0305160 commit 7589248
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### pg_cron v1.1.0 (March 22, 2018) ###

* Add new 'active' column on cron.job table to enable or disable job(s).
* Added a regression test, simply run 'make installcheck'
* Increased pg_cron version to 1.1

### pg_cron v1.0.2 (October 6, 2017) ###

* PostgreSQL 10 support
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# src/test/modules/pg_cron/Makefile

EXTENSION = pg_cron
EXTVERSION = 1.0
EXTVERSION = 1.1

DATA_built = $(EXTENSION)--$(EXTVERSION).sql
DATA_built = $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION)--1.0.sql
DATA = $(wildcard $(EXTENSION)--*--*.sql)
REGRESS = pg_cron-test

# compilation configuration
MODULE_big = $(EXTENSION)
Expand All @@ -19,3 +20,5 @@ include $(PGXS)

$(EXTENSION)--1.0.sql: $(EXTENSION).sql $(EXTENSION)--0.1--1.0.sql
cat $^ > $@
$(EXTENSION)--1.1.sql: $(EXTENSION).sql $(EXTENSION)--1.0--1.1.sql
cat $^ > $@
29 changes: 29 additions & 0 deletions expected/pg_cron-test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE EXTENSION pg_cron VERSION '1.0';
SELECT extversion FROM pg_extension WHERE extname='pg_cron';
extversion
------------
1.0
(1 row)

ALTER EXTENSION pg_cron UPDATE TO '1.1';
SELECT extversion FROM pg_extension WHERE extname='pg_cron';
extversion
------------
1.1
(1 row)

-- Vacuum every day at 10:00am (GMT)
SELECT cron.schedule('0 10 * * *', 'VACUUM');
schedule
----------
1
(1 row)

-- Stop scheduling a job
SELECT cron.unschedule(1);
unschedule
------------
t
(1 row)

DROP EXTENSION pg_cron;
4 changes: 3 additions & 1 deletion include/cron_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct FormData_cron_job
int nodePort;
text database;
text userName;
bool active;
#endif
} FormData_cron_job;

Expand All @@ -40,14 +41,15 @@ typedef FormData_cron_job *Form_cron_job;
* compiler constants for cron_job
* ----------------
*/
#define Natts_cron_job 7
#define Natts_cron_job 8
#define Anum_cron_job_jobid 1
#define Anum_cron_job_schedule 2
#define Anum_cron_job_command 3
#define Anum_cron_job_nodename 4
#define Anum_cron_job_nodeport 5
#define Anum_cron_job_database 6
#define Anum_cron_job_username 7
#define Anum_cron_job_active 8


#endif /* CRON_JOB_H */
1 change: 1 addition & 0 deletions include/job_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct CronJob
int nodePort;
char *database;
char *userName;
bool active;
} CronJob;


Expand Down
3 changes: 3 additions & 0 deletions pg_cron--1.0--1.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* pg_cron--1.0--1.1.sql */

ALTER TABLE cron.job ADD COLUMN active boolean not null default 'true';
2 changes: 1 addition & 1 deletion pg_cron.control
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
comment = 'Job scheduler for PostgreSQL'
default_version = '1.0'
default_version = '1.1'
module_pathname = '$libdir/pg_cron'
relocatable = false
2 changes: 1 addition & 1 deletion pg_cron.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DO $$
BEGIN
IF current_database() <> current_setting('cron.database_name') THEN
IF current_database() <> current_setting('cron.database_name') AND current_database() <> 'contrib_regression' THEN
RAISE EXCEPTION 'can only create extension in database %',
current_setting('cron.database_name')
USING DETAIL = 'Jobs must be scheduled from the database configured in '||
Expand Down
12 changes: 12 additions & 0 deletions sql/pg_cron-test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE EXTENSION pg_cron VERSION '1.0';
SELECT extversion FROM pg_extension WHERE extname='pg_cron';
ALTER EXTENSION pg_cron UPDATE TO '1.1';
SELECT extversion FROM pg_extension WHERE extname='pg_cron';

-- Vacuum every day at 10:00am (GMT)
SELECT cron.schedule('0 10 * * *', 'VACUUM');

-- Stop scheduling a job
SELECT cron.unschedule(1);

DROP EXTENSION pg_cron;
12 changes: 12 additions & 0 deletions src/job_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,18 @@ TupleToCronJob(TupleDesc tupleDescriptor, HeapTuple heapTuple)
job->userName = TextDatumGetCString(userName);
job->database = TextDatumGetCString(database);

if (HeapTupleHeaderGetNatts(heapTuple->t_data) >= Anum_cron_job_active)
{
Datum active = heap_getattr(heapTuple, Anum_cron_job_active,
tupleDescriptor, &isNull);
Assert(!isNull);
job->active = DatumGetBool(active);
}
else
{
job->active = true;
}

parsedSchedule = parse_cron_entry(job->scheduleText);
if (parsedSchedule != NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src/task_states.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ RefreshTaskHash(void)
CronJob *job = (CronJob *) lfirst(jobCell);

CronTask *task = GetCronTask(job->jobId);
task->isActive = true;
task->isActive = job->active;
}

CronJobCacheValid = true;
Expand Down

0 comments on commit 7589248

Please sign in to comment.