diff --git a/src/manager.js b/src/manager.js index 1c5f1adc..55a9111c 100644 --- a/src/manager.js +++ b/src/manager.js @@ -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; @@ -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}); diff --git a/src/plans.js b/src/plans.js index 92492791..771980eb 100644 --- a/src/plans.js +++ b/src/plans.js @@ -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, @@ -30,6 +30,7 @@ module.exports = { archive, countStates, states, + stateJobDelimiter, expiredJobSuffix, completedJobSuffix, failedJobSuffix @@ -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() ) `; diff --git a/test/completeTest.js b/test/completeTest.js index 2b7aacf4..66aa2035 100644 --- a/test/completeTest.js +++ b/test/completeTest.js @@ -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(); + }) + }); + }); diff --git a/test/testHelper.js b/test/testHelper.js index e8b123e8..c0398dad 100644 --- a/test/testHelper.js +++ b/test/testHelper.js @@ -1,5 +1,6 @@ const Db = require('../src/db'); const PgBoss = require('../src/index'); +const plans = require('../src/plans'); module.exports = { init, @@ -7,6 +8,9 @@ module.exports = { extend, getDb, getJobById, + findJobs, + stateJobDelimiter: plans.stateJobDelimiter, + countJobs, empty, getConfig, getConnectionString @@ -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) {