use contants for defaults, use a common doctype

add tests, update readme
This commit is contained in:
2016-05-12 11:47:05 -07:00
parent fa784393e5
commit 0020050f3f
6 changed files with 48 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ Option | Default | Description
------ | ----------- | ------- ------ | ----------- | -------
interval | `week` | Valid choices are `year`, `month`, `week`, `day`, `hour`, and even `minute`. | `week` interval | `week` | Valid choices are `year`, `month`, `week`, `day`, `hour`, and even `minute`. | `week`
timeout | `10000` | The default job timeout, in `ms`. If workers take longer than this, the job is re-queued for another worker to complete it. timeout | `10000` | The default job timeout, in `ms`. If workers take longer than this, the job is re-queued for another worker to complete it.
doctype | `esqueue` | The doctype to use in Elasticsearch
client | | Options to use when creating a new client instance - see [the elasticsearch-js docs](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html). If you rather use your own client instance, just pass it in here instead. client | | Options to use when creating a new client instance - see [the elasticsearch-js docs](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html). If you rather use your own client instance, just pass it in here instead.

View File

@@ -6,4 +6,10 @@ export const jobStatuses = {
JOB_STATUS_CANCELLED: 'cancelled', JOB_STATUS_CANCELLED: 'cancelled',
}; };
export default Object.assign({}, jobStatuses); export const defaultSettings = {
DEFAULT_SETTING_TIMEOUT: 10000,
DEFAULT_SETTING_INTERVAL: 'week',
DEFAULT_SETTING_DOCTYPE: 'esqueue',
};
export default Object.assign({}, jobStatuses, defaultSettings);

View File

@@ -1,3 +1,5 @@
import { defaultSettings } from './constants';
const schema = { const schema = {
payload: { type: 'object', enabled: false }, payload: { type: 'object', enabled: false },
priority: { type: 'short' }, priority: { type: 'short' },
@@ -19,14 +21,9 @@ const schema = {
} }
}; };
export default function createIndex(client, indexName) { export default function createIndex(client, indexName, doctype = defaultSettings.DEFAULT_SETTING_DOCTYPE) {
const indexBody = { const indexBody = { mappings : {} };
mappings: { indexBody.mappings[doctype] = { properties: schema };
_default_: {
properties: schema
}
}
};
return client.indices.exists({ return client.indices.exists({
index: indexName, index: indexName,

View File

@@ -2,6 +2,7 @@ import events from 'events';
import createClient from './helpers/es_client'; import createClient from './helpers/es_client';
import indexTimestamp from './helpers/index_timestamp'; import indexTimestamp from './helpers/index_timestamp';
import logger from './helpers/logger'; import logger from './helpers/logger';
import { defaultSettings } from './helpers/constants';
import Job from './job.js'; import Job from './job.js';
import Worker from './worker.js'; import Worker from './worker.js';
import omit from 'lodash.omit'; import omit from 'lodash.omit';
@@ -15,8 +16,9 @@ export default class Esqueue extends events.EventEmitter {
super(); super();
this.index = index; this.index = index;
this.settings = Object.assign({ this.settings = Object.assign({
interval: 'week', interval: defaultSettings.DEFAULT_SETTING_INTERVAL,
timeout: 10000, timeout: defaultSettings.DEFAULT_SETTING_TIMEOUT,
doctype: defaultSettings.DEFAULT_SETTING_DOCTYPE,
}, omit(options, [ 'client' ])); }, omit(options, [ 'client' ]));
this.client = createClient(options.client || {}); this.client = createClient(options.client || {});
@@ -38,10 +40,13 @@ export default class Esqueue extends events.EventEmitter {
addJob(type, payload, opts = {}) { addJob(type, payload, opts = {}) {
const timestamp = indexTimestamp(this.settings.interval); const timestamp = indexTimestamp(this.settings.interval);
const index = `${this.index}-${timestamp}`; const index = `${this.index}-${timestamp}`;
const defaults = {
timeout: this.settings.timeout,
};
const options = Object.assign({ const options = Object.assign(defaults, opts, {
timeout: this.settings.timeout doctype: this.settings.doctype
}, opts); });
return new Job(this.client, index, type, payload, options); return new Job(this.client, index, type, payload, options);
} }

View File

@@ -2,7 +2,7 @@ import events from 'events';
import isPlainObject from 'lodash.isplainobject'; import isPlainObject from 'lodash.isplainobject';
import Puid from 'puid'; import Puid from 'puid';
import logger from './helpers/logger'; import logger from './helpers/logger';
import { jobStatuses } from './helpers/constants'; import contstants from './helpers/constants';
import createIndex from './helpers/create_index'; import createIndex from './helpers/create_index';
const debug = logger('esqueue:job'); const debug = logger('esqueue:job');
@@ -23,10 +23,11 @@ export default class Job extends events.EventEmitter {
this.timeout = options.timeout || 10000; this.timeout = options.timeout || 10000;
this.maxAttempts = options.max_attempts || 3; this.maxAttempts = options.max_attempts || 3;
this.priority = Math.max(Math.min(options.priority || 10, 20), -20); this.priority = Math.max(Math.min(options.priority || 10, 20), -20);
this.doctype = options.doctype || contstants.DEFAULT_SETTING_DOCTYPE;
this.debug = (...msg) => debug(...msg, `id: ${this.id}`); this.debug = (...msg) => debug(...msg, `id: ${this.id}`);
this.ready = createIndex(client, index) this.ready = createIndex(client, index, this.doctype)
.then(() => { .then(() => {
return this.client.index({ return this.client.index({
index: this.index, index: this.index,
@@ -40,7 +41,7 @@ export default class Job extends events.EventEmitter {
created_at: new Date(), created_at: new Date(),
attempts: 0, attempts: 0,
max_attempts: this.maxAttempts, max_attempts: this.maxAttempts,
status: jobStatuses.JOB_STATUS_PENDING, status: contstants.JOB_STATUS_PENDING,
} }
}) })
.then((doc) => { .then((doc) => {

View File

@@ -2,6 +2,7 @@ import expect from 'expect.js';
import sinon from 'sinon'; import sinon from 'sinon';
import createIndex from '../../../lib/helpers/create_index'; import createIndex from '../../../lib/helpers/create_index';
import elasticsearchMock from '../../fixtures/elasticsearch'; import elasticsearchMock from '../../fixtures/elasticsearch';
import { defaultSettings } from '../../../lib/helpers/constants';
describe('Create Index', function () { describe('Create Index', function () {
let client; let client;
@@ -23,8 +24,9 @@ describe('Create Index', function () {
}); });
}); });
it('should create the default mappings', function () { it('should create the type mappings', function () {
const indexName = 'test-index'; const indexName = 'test-index';
const docType = defaultSettings.DEFAULT_SETTING_DOCTYPE;
const result = createIndex(client, indexName); const result = createIndex(client, indexName);
return result return result
@@ -33,8 +35,24 @@ describe('Create Index', function () {
sinon.assert.callCount(createSpy, 1); sinon.assert.callCount(createSpy, 1);
expect(payload).to.have.property('body'); expect(payload).to.have.property('body');
expect(payload.body).to.have.property('mappings'); expect(payload.body).to.have.property('mappings');
expect(payload.body.mappings).to.have.property('_default_'); expect(payload.body.mappings).to.have.property(docType);
expect(payload.body.mappings._default_).to.have.property('properties'); expect(payload.body.mappings[docType]).to.have.property('properties');
});
});
it('should accept a custom doctype', function () {
const indexName = 'test-index';
const docType = 'my_type';
const result = createIndex(client, indexName, docType);
return result
.then(function () {
const payload = createSpy.getCall(0).args[0];
sinon.assert.callCount(createSpy, 1);
expect(payload).to.have.property('body');
expect(payload.body).to.have.property('mappings');
expect(payload.body.mappings).to.have.property(docType);
expect(payload.body.mappings[docType]).to.have.property('properties');
}); });
}); });
}); });