Skip to content

Commit

Permalink
retry test
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed May 1, 2016
1 parent f4275f9 commit 8b60bb9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/attorney.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ function checkConfig(config) {
assert(!/\W/.test(config.schema), `configuration assert: ${config.schema} cannot be used as a schema. Only alphanumeric characters and underscores are allowed`);
}

if(config.newJobCheckIntervalSeconds)
if('newJobCheckIntervalSeconds' in config)
assert(config.newJobCheckIntervalSeconds >=1, 'configuration assert: newJobCheckIntervalSeconds must be at least every second');

if(config.expireCheckIntervalMinutes)
if('expireCheckIntervalSeconds' in config)
assert(config.expireCheckIntervalSeconds >=1, 'configuration assert: expireCheckIntervalSeconds must be at least every second');

if('expireCheckIntervalMinutes' in config)
assert(config.expireCheckIntervalMinutes >=1, 'configuration assert: expireCheckIntervalMinutes must be at least every minute');

if(config.archiveCheckIntervalMinutes)
if('archiveCheckIntervalMinutes' in config)
assert(config.archiveCheckIntervalMinutes >=1, 'configuration assert: archiveCheckIntervalMinutes must be at least every minute');

if(config.archiveCompletedJobsEvery)
if('archiveCompletedJobsEvery' in config)
assert(typeof config.archiveCompletedJobsEvery == 'string', 'configuration assert: archiveCompletedJobsEvery should be a readable PostgreSQL interval such as "1 day"');

if(config.uuid)
if('uuid' in config)
assert(config.uuid == 'v1' || config.uuid == 'v4', 'configuration assert: uuid option only supports v1 or v4');


config.uuid = config.uuid || 'v1';
config.schema = config.schema || 'pgboss';

config.newJobCheckIntervalSeconds = config.newJobCheckIntervalSeconds || 1;
config.expireCheckIntervalMinutes = config.expireCheckIntervalMinutes || 1;
config.archiveCheckIntervalMinutes = config.archiveCheckIntervalMinutes || 60;
config.archiveCompletedJobsEvery = config.archiveCompletedJobsEvery || '1 day';

if('expireCheckIntervalMinutes' in config){
config.expireCheckIntervalSeconds = config.expireCheckIntervalMinutes * 60;
} else {
config.expireCheckIntervalSeconds = config.expireCheckIntervalSeconds || 60;
}
}
2 changes: 1 addition & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Manager extends EventEmitter {
this.db = new Db(config);

this.workerInterval = config.newJobCheckIntervalSeconds * 1000;
this.monitorInterval = config.expireCheckIntervalMinutes * 60 * 1000;
this.monitorInterval = config.expireCheckIntervalSeconds * 1000;
this.nextJobCommand = plans.fetchNextJob(config.schema);
this.expireJobCommand = plans.expireJob(config.schema);
this.insertJobCommand = plans.insertJob(config.schema);
Expand Down
2 changes: 1 addition & 1 deletion src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function fetchNextJob(schema) {
WITH nextJob as (
SELECT id
FROM ${schema}.job
WHERE (state = 'created' OR (state = 'expired' AND retryCount <= retryLimit))
WHERE (state = 'created' OR (state = 'expired' AND retryCount < retryLimit))
AND name = $1
AND (createdOn + startIn) < now()
AND completedOn IS NULL
Expand Down
3 changes: 2 additions & 1 deletion test/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"host": "127.0.0.1",
"database": "pgboss",
"user": "postgres",
"password": "postgres",
"schema": "pgboss",
"uuid": "v1",
"newJobCheckIntervalSeconds": 1,
"expireCheckIntervalMinutes": 1,
"expireCheckIntervalSeconds": 60,
"archiveCheckIntervalMinutes": 60,
"archiveCompletedJobsEvery": "1 day"
}
59 changes: 59 additions & 0 deletions test/retryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require('chai').assert;
var PgBoss = require('../lib/index');
var config = require('./config.json');
var helper = require('./testService');

describe('retries', function() {
it('should retry a job that didn\'t complete', function (finished) {

var expireIn = '1 second';
var retries = 2;

this.timeout(7000);

// todo: temp test for travis config override
if(process.env.TRAVIS) {
config.port = 5433;
config.password = '';
}

config.expireCheckIntervalSeconds = 1;

var boss = new PgBoss(config);

boss.on('error', logError);
boss.on('ready', ready);

boss.start();

function logError(error) {
console.error(error);
}

function ready() {
helper.init()
.then(test);
}

function test() {
var subscribeCount = 0;

boss.subscribe('unreliable', null, function(job, done) {
// not calling done so it will expire
subscribeCount++;
});

boss.publish('unreliable', null, {
expireIn: expireIn,
retryLimit: retries
});

setTimeout(function() {
assert.equal(subscribeCount, retries + 1);
finished();

}, 6000);
}

});
});

0 comments on commit 8b60bb9

Please sign in to comment.