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 { 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']));
});
}
}

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 () {
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');
});
});