start on the worker logic
This commit is contained in:
@@ -1,15 +1,38 @@
|
|||||||
import events from 'events';
|
import events from 'events';
|
||||||
import Puid from 'puid';
|
import Puid from 'puid';
|
||||||
|
import { jobStatuses } from './helpers/constants';
|
||||||
|
|
||||||
|
const puid = new Puid();
|
||||||
|
|
||||||
export default class Job extends events.EventEmitter {
|
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 type !== 'string') throw new Error('Type must be a string');
|
||||||
if (typeof workerFn !== 'function') throw new Error('Worker must be a function');
|
if (typeof workerFn !== 'function') throw new Error('Worker must be a function');
|
||||||
|
|
||||||
super();
|
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
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,7 +184,6 @@ describe('Job Class', function () {
|
|||||||
expect(job.document).to.be(undefined);
|
expect(job.document).to.be(undefined);
|
||||||
|
|
||||||
const doc = job.toJSON();
|
const doc = job.toJSON();
|
||||||
console.log(doc);
|
|
||||||
expect(doc).to.have.property('index', index);
|
expect(doc).to.have.property('index', index);
|
||||||
expect(doc).to.have.property('type', type);
|
expect(doc).to.have.property('type', type);
|
||||||
expect(doc).to.have.property('timeout', options.timeout);
|
expect(doc).to.have.property('timeout', options.timeout);
|
||||||
|
|||||||
Reference in New Issue
Block a user