From 0020050f3f6c261922720ba5055a0bb23c10261f Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 12 May 2016 11:47:05 -0700 Subject: [PATCH] use contants for defaults, use a common doctype add tests, update readme --- readme.md | 1 + src/helpers/constants.js | 8 +++++++- src/helpers/create_index.js | 13 +++++-------- src/index.js | 15 ++++++++++----- src/job.js | 7 ++++--- test/src/helpers/create_index.js | 24 +++++++++++++++++++++--- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/readme.md b/readme.md index 66ad14e..31ff097 100644 --- a/readme.md +++ b/readme.md @@ -33,6 +33,7 @@ Option | Default | Description ------ | ----------- | ------- 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. +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. diff --git a/src/helpers/constants.js b/src/helpers/constants.js index b81294f..f764f8c 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -6,4 +6,10 @@ export const jobStatuses = { JOB_STATUS_CANCELLED: 'cancelled', }; -export default Object.assign({}, jobStatuses); \ No newline at end of file +export const defaultSettings = { + DEFAULT_SETTING_TIMEOUT: 10000, + DEFAULT_SETTING_INTERVAL: 'week', + DEFAULT_SETTING_DOCTYPE: 'esqueue', +}; + +export default Object.assign({}, jobStatuses, defaultSettings); \ No newline at end of file diff --git a/src/helpers/create_index.js b/src/helpers/create_index.js index 959ab51..c975e78 100644 --- a/src/helpers/create_index.js +++ b/src/helpers/create_index.js @@ -1,3 +1,5 @@ +import { defaultSettings } from './constants'; + const schema = { payload: { type: 'object', enabled: false }, priority: { type: 'short' }, @@ -19,14 +21,9 @@ const schema = { } }; -export default function createIndex(client, indexName) { - const indexBody = { - mappings: { - _default_: { - properties: schema - } - } - }; +export default function createIndex(client, indexName, doctype = defaultSettings.DEFAULT_SETTING_DOCTYPE) { + const indexBody = { mappings : {} }; + indexBody.mappings[doctype] = { properties: schema }; return client.indices.exists({ index: indexName, diff --git a/src/index.js b/src/index.js index 76f9b37..e1e676a 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import events from 'events'; import createClient from './helpers/es_client'; import indexTimestamp from './helpers/index_timestamp'; import logger from './helpers/logger'; +import { defaultSettings } from './helpers/constants'; import Job from './job.js'; import Worker from './worker.js'; import omit from 'lodash.omit'; @@ -15,8 +16,9 @@ export default class Esqueue extends events.EventEmitter { super(); this.index = index; this.settings = Object.assign({ - interval: 'week', - timeout: 10000, + interval: defaultSettings.DEFAULT_SETTING_INTERVAL, + timeout: defaultSettings.DEFAULT_SETTING_TIMEOUT, + doctype: defaultSettings.DEFAULT_SETTING_DOCTYPE, }, omit(options, [ 'client' ])); this.client = createClient(options.client || {}); @@ -38,10 +40,13 @@ export default class Esqueue extends events.EventEmitter { addJob(type, payload, opts = {}) { const timestamp = indexTimestamp(this.settings.interval); const index = `${this.index}-${timestamp}`; + const defaults = { + timeout: this.settings.timeout, + }; - const options = Object.assign({ - timeout: this.settings.timeout - }, opts); + const options = Object.assign(defaults, opts, { + doctype: this.settings.doctype + }); return new Job(this.client, index, type, payload, options); } diff --git a/src/job.js b/src/job.js index 1a53e1e..7ca56aa 100644 --- a/src/job.js +++ b/src/job.js @@ -2,7 +2,7 @@ import events from 'events'; import isPlainObject from 'lodash.isplainobject'; import Puid from 'puid'; import logger from './helpers/logger'; -import { jobStatuses } from './helpers/constants'; +import contstants from './helpers/constants'; import createIndex from './helpers/create_index'; const debug = logger('esqueue:job'); @@ -23,10 +23,11 @@ export default class Job extends events.EventEmitter { this.timeout = options.timeout || 10000; this.maxAttempts = options.max_attempts || 3; 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.ready = createIndex(client, index) + this.ready = createIndex(client, index, this.doctype) .then(() => { return this.client.index({ index: this.index, @@ -40,7 +41,7 @@ export default class Job extends events.EventEmitter { created_at: new Date(), attempts: 0, max_attempts: this.maxAttempts, - status: jobStatuses.JOB_STATUS_PENDING, + status: contstants.JOB_STATUS_PENDING, } }) .then((doc) => { diff --git a/test/src/helpers/create_index.js b/test/src/helpers/create_index.js index 09e26e1..a0dfc12 100644 --- a/test/src/helpers/create_index.js +++ b/test/src/helpers/create_index.js @@ -2,6 +2,7 @@ import expect from 'expect.js'; import sinon from 'sinon'; import createIndex from '../../../lib/helpers/create_index'; import elasticsearchMock from '../../fixtures/elasticsearch'; +import { defaultSettings } from '../../../lib/helpers/constants'; describe('Create Index', function () { 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 docType = defaultSettings.DEFAULT_SETTING_DOCTYPE; const result = createIndex(client, indexName); return result @@ -33,8 +35,24 @@ describe('Create Index', function () { 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('_default_'); - expect(payload.body.mappings._default_).to.have.property('properties'); + expect(payload.body.mappings).to.have.property(docType); + 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'); }); }); });