remove job options passing
This commit is contained in:
@@ -3,7 +3,7 @@ import { isPlainObject, omit } from 'lodash';
|
|||||||
import { JOB_STATUS_PENDING } from './helpers/constants';
|
import { JOB_STATUS_PENDING } from './helpers/constants';
|
||||||
|
|
||||||
export default class Job extends events.EventEmitter {
|
export default class Job extends events.EventEmitter {
|
||||||
constructor(queue, type, payload, options = {}) {
|
constructor(queue, type, payload, timeout = 10000) {
|
||||||
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');
|
||||||
|
|
||||||
@@ -12,8 +12,7 @@ export default class Job extends events.EventEmitter {
|
|||||||
this.queue = queue;
|
this.queue = queue;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
this.timeout = options.timeout || 10000;
|
this.timeout = timeout;
|
||||||
this.options = omit(options, [ 'timeout' ]);
|
|
||||||
this.status = JOB_STATUS_PENDING;
|
this.status = JOB_STATUS_PENDING;
|
||||||
|
|
||||||
this.ready = this.queue.client.index({
|
this.ready = this.queue.client.index({
|
||||||
@@ -22,7 +21,6 @@ export default class Job extends events.EventEmitter {
|
|||||||
body: {
|
body: {
|
||||||
payload: this.payload,
|
payload: this.payload,
|
||||||
timeout: this.timeout,
|
timeout: this.timeout,
|
||||||
options: this.options,
|
|
||||||
created: new Date(),
|
created: new Date(),
|
||||||
started: null,
|
started: null,
|
||||||
completed: null,
|
completed: null,
|
||||||
|
|||||||
@@ -39,10 +39,9 @@ describe('Job Class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('construction', function () {
|
describe('construction', function () {
|
||||||
const PENDING = 0;
|
|
||||||
let type;
|
let type;
|
||||||
let payload;
|
let payload;
|
||||||
let options;
|
let timeout;
|
||||||
|
|
||||||
function validateDoc(spy) {
|
function validateDoc(spy) {
|
||||||
expect(spy.callCount).to.be(1);
|
expect(spy.callCount).to.be(1);
|
||||||
@@ -53,7 +52,7 @@ describe('Job Class', function () {
|
|||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
type = 'type1';
|
type = 'type1';
|
||||||
payload = { id: '123' };
|
payload = { id: '123' };
|
||||||
options = { timeout: 1234, test: 'options' };
|
timeout = 4567;
|
||||||
sinon.spy(mockQueue.client, 'index');
|
sinon.spy(mockQueue.client, 'index');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -66,26 +65,14 @@ describe('Job Class', function () {
|
|||||||
expect(newDoc.body).to.have.property('payload', payload);
|
expect(newDoc.body).to.have.property('payload', payload);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should index any optional params', function () {
|
|
||||||
new Job(mockQueue, type, payload, options);
|
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
|
||||||
expect(newDoc.body).to.have.property('options');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should index timeout value from options', function () {
|
it('should index timeout value from options', function () {
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, timeout);
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
expect(newDoc.body).to.have.property('timeout', options.timeout);
|
expect(newDoc.body).to.have.property('timeout', timeout);
|
||||||
});
|
|
||||||
|
|
||||||
it('should not use timeout as an option', function () {
|
|
||||||
new Job(mockQueue, type, payload, options);
|
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
|
||||||
expect(newDoc.body.options).to.eql(_.omit(options, [ 'timeout' ]));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set event times', function () {
|
it('should set event times', function () {
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, timeout);
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
expect(newDoc.body).to.have.property('created');
|
expect(newDoc.body).to.have.property('created');
|
||||||
expect(newDoc.body).to.have.property('started');
|
expect(newDoc.body).to.have.property('started');
|
||||||
@@ -93,13 +80,13 @@ describe('Job Class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should set attempt count', function () {
|
it('should set attempt count', function () {
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, timeout);
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
expect(newDoc.body).to.have.property('attempts');
|
expect(newDoc.body).to.have.property('attempts');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set status as pending', function () {
|
it('should set status as pending', function () {
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, timeout);
|
||||||
const newDoc = validateDoc(mockQueue.client.index);
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING);
|
expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user