From 5c4340adfc4bce2a36d2592f8aef2002773e3cae Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Mon, 25 Apr 2016 16:28:30 -0700 Subject: [PATCH] start on the worker logic --- src/worker.js | 31 +++++++++++++++++++++++++++---- test/src/job.js | 1 - 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/worker.js b/src/worker.js index 00f9c8e..3cedfb5 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,15 +1,38 @@ import events from 'events'; import Puid from 'puid'; +import { jobStatuses } from './helpers/constants'; + +const puid = new Puid(); export default class Job extends events.EventEmitter { - constructor(queue, type, workerFn) { + constructor(queue, type, workerFn, opts = {}) { if (typeof type !== 'string') throw new Error('Type must be a string'); if (typeof workerFn !== 'function') throw new Error('Worker must be a function'); super(); - const puid = new Puid(); - this.id = puid.generate(); - // TODO: check for existing jobs to process + this.id = puid.generate(); + this.queue = queue; + this.client = this.queue.client; + this.type = type; + this.workerFn = workerFn; + this.checkInterval = opts.interval || 1500; + + this._processJobs(); + this._checker = setInterval(this._processJobs, this.checkInterval); + } + + destroy() { + clearInterval(this._checker); + } + + _processJobs() { + const query = {}; + + return this.client.search({ + body: { + query: query + } + }); } } \ No newline at end of file diff --git a/test/src/job.js b/test/src/job.js index 66fd8d3..6bd5e0d 100644 --- a/test/src/job.js +++ b/test/src/job.js @@ -184,7 +184,6 @@ describe('Job Class', function () { expect(job.document).to.be(undefined); const doc = job.toJSON(); - console.log(doc); expect(doc).to.have.property('index', index); expect(doc).to.have.property('type', type); expect(doc).to.have.property('timeout', options.timeout);