From 1114bb25b8cd8c5f50668044dbd333caf738e773 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Fri, 22 Apr 2016 17:47:48 -0700 Subject: [PATCH] create index on job creation --- src/job.js | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/job.js b/src/job.js index 567b70b..00fe716 100644 --- a/src/job.js +++ b/src/job.js @@ -1,6 +1,7 @@ import events from 'events'; -import { isPlainObject, omit } from 'lodash'; +import { isPlainObject } from 'lodash'; import { JOB_STATUS_PENDING } from './helpers/constants'; +import createIndex from './helpers/create_index'; export default class Job extends events.EventEmitter { constructor(client, index, type, payload, timeout = 10000) { @@ -16,25 +17,33 @@ export default class Job extends events.EventEmitter { this.timeout = timeout; this.status = JOB_STATUS_PENDING; - this.ready = this.client.index({ - index: this.index, - type: this.type, - body: { - payload: this.payload, - timeout: this.timeout, - created: new Date(), - started: null, - completed: null, - attempts: 0, - status: this.status, - } + this.ready = createIndex(client, index) + .then((...args) => { + this.client.index({ + index: this.index, + type: this.type, + body: { + payload: this.payload, + timeout: this.timeout, + created: new Date(), + started: null, + completed: null, + attempts: 0, + status: this.status, + } + }) + .then((doc) => { + this.document = { + id: doc._id, + type: doc._type, + version: doc._version, + }; + }); }) - .then((doc) => { - this.document = { - id: doc._id, - type: doc._type, - version: doc._version, - }; + .catch((err) => { + this.emit('error', err); + throw err; }); } -} \ No newline at end of file + +}