Skip to content

Commit

Permalink
renamed expire command
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Apr 1, 2017
1 parent e1dc44c commit ccbb848
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Manager extends EventEmitter {
this.db = db;

this.nextJobCommand = plans.fetchNextJob(config.schema);
this.expireJobCommand = plans.expireJob(config.schema);
this.expireCommand = plans.expire(config.schema);
this.insertJobCommand = plans.insertJob(config.schema);
this.completeJobCommand = plans.completeJob(config.schema);
this.cancelJobCommand = plans.cancelJob(config.schema);
Expand All @@ -32,7 +32,7 @@ class Manager extends EventEmitter {
return expire().then(init);

function expire() {
return self.db.executeSql(self.expireJobCommand)
return self.db.executeSql(self.expireCommand)
.then(result => {
if (result.rows.length){
self.emit('expired-count', result.rows.length);
Expand Down
30 changes: 15 additions & 15 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module.exports = {
getVersion,
versionTableExists,
fetchNextJob,
expireJob,
completeJob,
cancelJob,
failJob,
insertJob,
expire,
archive,
expireJobSuffix
};
Expand Down Expand Up @@ -138,20 +138,6 @@ function fetchNextJob(schema) {
`;
}

function expireJob(schema) {
return `
WITH expired AS (
UPDATE ${schema}.job
SET state = CASE WHEN retryCount < retryLimit THEN 'retry'::${schema}.job_state ELSE 'expired'::${schema}.job_state END,
completedOn = CASE WHEN retryCount < retryLimit THEN NULL ELSE now() END
WHERE state = 'active'
AND (startedOn + expireIn) < now()
RETURNING id, name, state, data
)
SELECT id, name, data FROM expired WHERE state = 'expired';
`;
}

function completeJob(schema){
return `
UPDATE ${schema}.job
Expand Down Expand Up @@ -193,6 +179,20 @@ function insertJob(schema) {
`;
}

function expire(schema) {
return `
WITH expired AS (
UPDATE ${schema}.job
SET state = CASE WHEN retryCount < retryLimit THEN 'retry'::${schema}.job_state ELSE 'expired'::${schema}.job_state END,
completedOn = CASE WHEN retryCount < retryLimit THEN NULL ELSE now() END
WHERE state = 'active'
AND (startedOn + expireIn) < now()
RETURNING id, name, state, data
)
SELECT id, name, data FROM expired WHERE state = 'expired';
`;
}

function archive(schema) {
return `
DELETE FROM ${schema}.job
Expand Down

0 comments on commit ccbb848

Please sign in to comment.