forked from vharidas/node-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobWorker.js
37 lines (33 loc) · 1.08 KB
/
jobWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class JobWorker {
constructor(name, reply, disconnect) {
this.name = name;
this.reply = reply;
this.disconnect = disconnect;
}
onMessage(msg) {
console.log(`worker ${this.name} got message`, msg);
if (msg.name == 'stop') this.disconnect();
if (msg.name == 'run') this.runTask(msg.text);
}
runTask(text) {
const [task, ...args] = text.trim().split(' ');
if (task == 'remind') {
const [seconds] = args;
const remind = () => this.reply({ name: 'reminder', seconds });
setTimeout(remind, seconds * 1000);
return;
}
this.reply({name:'unhandled task',text});
}
}
const main = () => {
let worker;
[, , name] = process.argv;
console.log(`starting worker ${name} with pid ${process.pid}`);
const receive = msg => worker.onMessage(msg);
process.on('message', receive);
const disconnect = () => process.off('message', receive);
const reply = msg => process.send(msg);
worker = new JobWorker(name, reply, disconnect);
}
main();