new jobs take options again, update schema to match index, add max_attempts, update tests

This commit is contained in:
2016-04-25 11:01:46 -07:00
parent 73e302fdf0
commit d9e19bccf8
2 changed files with 22 additions and 15 deletions

View File

@@ -7,17 +7,20 @@ import createIndex from './helpers/create_index';
const debug = logger('job'); const debug = logger('job');
export default class Job extends events.EventEmitter { export default class Job extends events.EventEmitter {
constructor(client, index, type, payload, timeout = 10000) { constructor(client, index, type, payload, options = {}) {
if (typeof type !== 'string') throw new Error('Type must be a string'); if (typeof type !== 'string') throw new Error('Type must be a string');
if (!isPlainObject(payload)) throw new Error('Payload must be a plain object'); if (!isPlainObject(payload)) throw new Error('Payload must be a plain object');
super(); super();
const timeout = options.timeout || 10000;
const maxAttempts = options.max_attempts || 3;
const priority = Math.max(Math.min(options.priority || 10, 20), -20);
this.client = client; this.client = client;
this.index = index; this.index = index;
this.type = type; this.type = type;
this.payload = payload; this.payload = payload;
this.timeout = timeout;
this.ready = createIndex(client, index) this.ready = createIndex(client, index)
.then(() => { .then(() => {
@@ -26,9 +29,11 @@ export default class Job extends events.EventEmitter {
type: this.type, type: this.type,
body: { body: {
payload: this.payload, payload: this.payload,
timeout: this.timeout, priority: priority,
timeout: timeout,
created_at: new Date(), created_at: new Date(),
attempts: 0, attempts: 0,
max_attempts: maxAttempts,
status: JOB_STATUS_PENDING, status: JOB_STATUS_PENDING,
} }
}) })

View File

@@ -47,7 +47,7 @@ describe('Job Class', function () {
describe('construction', function () { describe('construction', function () {
let type; let type;
let payload; let payload;
let timeout; let options;
function validateDoc(spy) { function validateDoc(spy) {
sinon.assert.callCount(spy, 1); sinon.assert.callCount(spy, 1);
@@ -58,7 +58,10 @@ describe('Job Class', function () {
beforeEach(function () { beforeEach(function () {
type = 'type1'; type = 'type1';
payload = { id: '123' }; payload = { id: '123' };
timeout = 4567; options = {
timeout: 4567,
max_attempts: 9,
};
sinon.spy(client, 'index'); sinon.spy(client, 'index');
}); });
@@ -74,33 +77,32 @@ describe('Job Class', function () {
}); });
it('should index timeout value from options', function () { it('should index timeout value from options', function () {
const job = new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('timeout', timeout); expect(newDoc.body).to.have.property('timeout', options.timeout);
}); });
}); });
it('should set event times', function () { it('should set event times', function () {
const job = new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('created'); expect(newDoc.body).to.have.property('created_at');
expect(newDoc.body).to.have.property('started');
expect(newDoc.body).to.have.property('completed');
}); });
}); });
it('should set attempt count', function () { it('should set attempt count', function () {
const job = new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('attempts'); expect(newDoc.body).to.have.property('attempts', 0);
expect(newDoc.body).to.have.property('max_attempts', options.max_attempts);
}); });
}); });
it('should set status as pending', function () { it('should set status as pending', function () {
const job = new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING); expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING);
@@ -108,7 +110,7 @@ describe('Job Class', function () {
}); });
it('should create the target index', function () { it('should create the target index', function () {
const job = new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, options);
return job.ready.then(() => { return job.ready.then(() => {
sinon.assert.calledOnce(createIndexMock); sinon.assert.calledOnce(createIndexMock);
}); });