add a worker module
This commit is contained in:
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
15
src/worker.js
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user