new jobs take options again, update schema to match index, add max_attempts, update tests
This commit is contained in:
11
src/job.js
11
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,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user