From d9e19bccf8a2b1642588fd9012d1de738c67e165 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Mon, 25 Apr 2016 11:01:46 -0700 Subject: [PATCH] new jobs take options again, update schema to match index, add max_attempts, update tests --- src/job.js | 11 ++++++++--- test/src/job.js | 26 ++++++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/job.js b/src/job.js index c3b8508..20db787 100644 --- a/src/job.js +++ b/src/job.js @@ -7,17 +7,20 @@ import createIndex from './helpers/create_index'; const debug = logger('job'); 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 (!isPlainObject(payload)) throw new Error('Payload must be a plain object'); 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.index = index; this.type = type; this.payload = payload; - this.timeout = timeout; this.ready = createIndex(client, index) .then(() => { @@ -26,9 +29,11 @@ export default class Job extends events.EventEmitter { type: this.type, body: { payload: this.payload, - timeout: this.timeout, + priority: priority, + timeout: timeout, created_at: new Date(), attempts: 0, + max_attempts: maxAttempts, status: JOB_STATUS_PENDING, } }) diff --git a/test/src/job.js b/test/src/job.js index 1c0dddd..68dc85a 100644 --- a/test/src/job.js +++ b/test/src/job.js @@ -47,7 +47,7 @@ describe('Job Class', function () { describe('construction', function () { let type; let payload; - let timeout; + let options; function validateDoc(spy) { sinon.assert.callCount(spy, 1); @@ -58,7 +58,10 @@ describe('Job Class', function () { beforeEach(function () { type = 'type1'; payload = { id: '123' }; - timeout = 4567; + options = { + timeout: 4567, + max_attempts: 9, + }; sinon.spy(client, 'index'); }); @@ -74,33 +77,32 @@ describe('Job Class', 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(() => { 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 () { - const job = new Job(client, index, type, payload, timeout); + const job = new Job(client, index, type, payload, options); return job.ready.then(() => { const newDoc = validateDoc(client.index); - expect(newDoc.body).to.have.property('created'); - expect(newDoc.body).to.have.property('started'); - expect(newDoc.body).to.have.property('completed'); + expect(newDoc.body).to.have.property('created_at'); }); }); 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(() => { 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 () { - const job = new Job(client, index, type, payload, timeout); + const job = new Job(client, index, type, payload, options); return job.ready.then(() => { const newDoc = validateDoc(client.index); 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 () { - const job = new Job(client, index, type, payload, timeout); + const job = new Job(client, index, type, payload, options); return job.ready.then(() => { sinon.assert.calledOnce(createIndexMock); });