add a worker module

This commit is contained in:
2016-04-18 16:39:35 -07:00
parent 4416a5508d
commit 1b5f2af004
3 changed files with 23 additions and 1 deletions

View File

@@ -31,6 +31,7 @@
}, },
"dependencies": { "dependencies": {
"elasticsearch": "~11.0.1", "elasticsearch": "~11.0.1",
"lodash": "~4.11.1" "lodash": "~4.11.1",
"puid": "~1.0.5"
} }
} }

View File

@@ -1,6 +1,7 @@
import events from 'events'; import events from 'events';
import createClient from './helpers/create_client'; import createClient from './helpers/create_client';
import Job from './job.js'; import Job from './job.js';
import Worker from './worker.js';
import { omit } from 'lodash'; import { omit } from 'lodash';
export default class Elastique extends events.EventEmitter { export default class Elastique extends events.EventEmitter {
@@ -24,4 +25,9 @@ export default class Elastique extends events.EventEmitter {
const job = new Job(this, type, payload, options); const job = new Job(this, type, payload, options);
return job; return job;
} }
registerWorker(type, workerFn) {
const worker = new Worker(this, type, workerFn);
return worker;
}
} }

15
src/worker.js Normal file
View File

@@ -0,0 +1,15 @@
import events from 'events';
import Puid from 'puid';
export default class Job extends events.EventEmitter {
constructor(queue, type, workerFn) {
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
}
}