From 662d8a177c5f4afaef617ef1d5d63ee20f06a055 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Mon, 25 Apr 2016 15:28:12 -0700 Subject: [PATCH] assign job ids internally, update tests useful for making toJSON fully sync and work without the elasticsearch document --- src/job.js | 12 +++++++----- test/src/job.js | 30 +++++++++++++----------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/job.js b/src/job.js index 54d1096..0232501 100644 --- a/src/job.js +++ b/src/job.js @@ -1,11 +1,12 @@ import events from 'events'; import { isPlainObject } from 'lodash'; -import { omit } from 'lodash'; +import Puid from 'puid'; import logger from './helpers/logger'; import { jobStatuses } from './helpers/constants'; import createIndex from './helpers/create_index'; const debug = logger('job'); +const puid = new Puid(); export default class Job extends events.EventEmitter { constructor(client, index, type, payload, options = {}) { @@ -15,6 +16,7 @@ export default class Job extends events.EventEmitter { super(); this.client = client; + this.id = puid.generate(); this.index = index; this.type = type; this.payload = payload; @@ -27,6 +29,7 @@ export default class Job extends events.EventEmitter { return this.client.index({ index: this.index, type: this.type, + id: this.id, body: { payload: this.payload, priority: this.priority, @@ -59,7 +62,7 @@ export default class Job extends events.EventEmitter { return this.client.get({ index: this.index, type: this.type, - id: this.document.id + id: this.id }); }) .then((doc) => { @@ -73,16 +76,15 @@ export default class Job extends events.EventEmitter { } toJSON() { - if (!this.document) return false; - return Object.assign({ + id: this.id, index: this.index, type: this.type, payload: this.payload, timeout: this.timeout, max_attempts: this.maxAttempts, priority: this.priority, - }, omit(this.document, ['version'])); + }); } } diff --git a/test/src/job.js b/test/src/job.js index 21e4732..66fd8d3 100644 --- a/test/src/job.js +++ b/test/src/job.js @@ -177,25 +177,21 @@ describe('Job Class', function () { }; }); - it('should return false if no elasticsearch document', function () { - const job = new Job(client, index, 'test', {}); - // same tick, document promise has not resolved - expect(job.toJSON()).to.equal(false); - }); - it('should return the static information about the job', function () { const job = new Job(client, index, type, payload, options); - // toJSON is sync, but only works once the document has been written - return job.ready.then(() => { - const doc = job.toJSON(); - expect(doc).to.have.property('index', index); - expect(doc).to.have.property('type', type); - expect(doc).to.have.property('timeout', options.timeout); - expect(doc).to.have.property('max_attempts', options.max_attempts); - expect(doc).to.have.property('priority', options.priority); - expect(doc).to.have.property('id'); - expect(doc).to.not.have.property('version'); - }); + + // toJSON is sync, should work before doc is written to elasticsearch + expect(job.document).to.be(undefined); + + const doc = job.toJSON(); + console.log(doc); + expect(doc).to.have.property('index', index); + expect(doc).to.have.property('type', type); + expect(doc).to.have.property('timeout', options.timeout); + expect(doc).to.have.property('max_attempts', options.max_attempts); + expect(doc).to.have.property('priority', options.priority); + expect(doc).to.have.property('id'); + expect(doc).to.not.have.property('version'); }); });