Skip to content

Commit

Permalink
preventing extra state transition jobs from being created
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Dec 5, 2017
1 parent c26b925 commit 1666666
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Worker = require('./worker');
const plans = require('./plans');
const Attorney = require('./attorney');

const stateJobDelimiter = plans.stateJobDelimiter;
const expiredJobSuffix = plans.expiredJobSuffix;
const completedJobSuffix = plans.completedJobSuffix;
const failedJobSuffix = plans.failedJobSuffix;
Expand Down Expand Up @@ -252,6 +253,9 @@ class Manager extends EventEmitter {

job = result.rows[0];

if(!bypassNotify)
bypassNotify = job.name.indexOf(stateJobDelimiter) >= 0;

return bypassNotify
? null
: this.publish(job.name + stateSuffix, {request: job, response: data || null});
Expand Down
11 changes: 6 additions & 5 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const states = {
failed: 'failed'
};

const stateJobSuffix = '__state__';
const expiredJobSuffix = stateJobSuffix + states.expired;
const completedJobSuffix = stateJobSuffix + states.complete;
const failedJobSuffix = stateJobSuffix + states.failed;
const stateJobDelimiter = '__state__';
const expiredJobSuffix = stateJobDelimiter + states.expired;
const completedJobSuffix = stateJobDelimiter + states.complete;
const failedJobSuffix = stateJobDelimiter + states.failed;

module.exports = {
create,
Expand All @@ -30,6 +30,7 @@ module.exports = {
archive,
countStates,
states,
stateJobDelimiter,
expiredJobSuffix,
completedJobSuffix,
failedJobSuffix
Expand Down Expand Up @@ -260,7 +261,7 @@ function archive(schema) {
WHERE (completedOn + CAST($1 as interval) < now())
OR (
state = '${states.created}'
AND name LIKE '%${stateJobSuffix}%'
AND name LIKE '%${stateJobDelimiter}%'
AND createdOn + CAST($1 as interval) < now()
)
`;
Expand Down
18 changes: 18 additions & 0 deletions test/completeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,22 @@ describe('complete', function() {
})
});

it('should not create an extra state job after completion', function(finished){
const jobName = 'noMoreExtraStateJobs';

let jobId;

boss.publish(jobName)
.then(id => jobId = id)
.then(() => boss.fetch(jobName))
.then(() => boss.complete(jobId))
.then(() => boss.fetchCompleted(jobName))
.then(job => boss.complete(job.id))
.then(() => helper.countJobs(`name LIKE $1`, [`${jobName}${helper.stateJobDelimiter}%`]))
.then(stateJobCount => {
assert.strictEqual(stateJobCount, 1);
finished();
})
});

});
15 changes: 14 additions & 1 deletion test/testHelper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const Db = require('../src/db');
const PgBoss = require('../src/index');
const plans = require('../src/plans');

module.exports = {
init,
start,
extend,
getDb,
getJobById,
findJobs,
stateJobDelimiter: plans.stateJobDelimiter,
countJobs,
empty,
getConfig,
getConnectionString
Expand Down Expand Up @@ -44,7 +48,16 @@ function init(schema) {
}

function getJobById(id) {
return getDb().executeSql(`select * from ${getConfig().schema}.job where id = $1`, [id]);
return findJobs('id = $1', [id]);
}

function findJobs(where, values){
return getDb().executeSql(`select * from ${getConfig().schema}.job where ${where}`, values);
}

function countJobs(where, values){
return getDb().executeSql(`select count(*) as count from ${getConfig().schema}.job where ${where}`, values)
.then(result => parseFloat(result.rows[0].count));
}

function start(options) {
Expand Down

0 comments on commit 1666666

Please sign in to comment.