use contants for defaults, use a common doctype
add tests, update readme
This commit is contained in:
@@ -6,4 +6,10 @@ export const jobStatuses = {
|
||||
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);
|
||||
@@ -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,
|
||||
|
||||
15
src/index.js
15
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);
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user