assign job ids internally, update tests

useful for making toJSON fully sync and work without the elasticsearch document
This commit is contained in:
2016-04-25 15:28:12 -07:00
parent 58a0cf328f
commit 662d8a177c
2 changed files with 20 additions and 22 deletions

View File

@@ -1,11 +1,12 @@
import events from 'events'; import events from 'events';
import { isPlainObject } from 'lodash'; import { isPlainObject } from 'lodash';
import { omit } from 'lodash'; import Puid from 'puid';
import logger from './helpers/logger'; import logger from './helpers/logger';
import { jobStatuses } from './helpers/constants'; import { jobStatuses } from './helpers/constants';
import createIndex from './helpers/create_index'; import createIndex from './helpers/create_index';
const debug = logger('job'); const debug = logger('job');
const puid = new Puid();
export default class Job extends events.EventEmitter { export default class Job extends events.EventEmitter {
constructor(client, index, type, payload, options = {}) { constructor(client, index, type, payload, options = {}) {
@@ -15,6 +16,7 @@ export default class Job extends events.EventEmitter {
super(); super();
this.client = client; this.client = client;
this.id = puid.generate();
this.index = index; this.index = index;
this.type = type; this.type = type;
this.payload = payload; this.payload = payload;
@@ -27,6 +29,7 @@ export default class Job extends events.EventEmitter {
return this.client.index({ return this.client.index({
index: this.index, index: this.index,
type: this.type, type: this.type,
id: this.id,
body: { body: {
payload: this.payload, payload: this.payload,
priority: this.priority, priority: this.priority,
@@ -59,7 +62,7 @@ export default class Job extends events.EventEmitter {
return this.client.get({ return this.client.get({
index: this.index, index: this.index,
type: this.type, type: this.type,
id: this.document.id id: this.id
}); });
}) })
.then((doc) => { .then((doc) => {
@@ -73,16 +76,15 @@ export default class Job extends events.EventEmitter {
} }
toJSON() { toJSON() {
if (!this.document) return false;
return Object.assign({ return Object.assign({
id: this.id,
index: this.index, index: this.index,
type: this.type, type: this.type,
payload: this.payload, payload: this.payload,
timeout: this.timeout, timeout: this.timeout,
max_attempts: this.maxAttempts, max_attempts: this.maxAttempts,
priority: this.priority, priority: this.priority,
}, omit(this.document, ['version'])); });
} }
} }

View File

@@ -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 () { it('should return the static information about the job', function () {
const job = new Job(client, index, type, payload, options); 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(() => { // toJSON is sync, should work before doc is written to elasticsearch
const doc = job.toJSON(); expect(job.document).to.be(undefined);
expect(doc).to.have.property('index', index);
expect(doc).to.have.property('type', type); const doc = job.toJSON();
expect(doc).to.have.property('timeout', options.timeout); console.log(doc);
expect(doc).to.have.property('max_attempts', options.max_attempts); expect(doc).to.have.property('index', index);
expect(doc).to.have.property('priority', options.priority); expect(doc).to.have.property('type', type);
expect(doc).to.have.property('id'); expect(doc).to.have.property('timeout', options.timeout);
expect(doc).to.not.have.property('version'); 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');
}); });
}); });