-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (39 loc) · 1.42 KB
/
index.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
38
39
40
41
42
43
44
45
const cfork = require('cfork');
const childprocess = require('child_process');
cfork({
exec: './worker.js',
// slaves: ['/your/app/slave.js'],
count: 1,
refork: false,
})
.on('fork', function (worker) {
console.warn('[%s] [worker:%d] new worker start', Date(), worker.process.pid);
})
.on('disconnect', function (worker) {
console.warn('[%s] [master:%s] wroker:%s disconnect, suicide: %s, state: %s.',
Date(), process.pid, worker.process.pid, worker.suicide, worker.state);
})
.on('exit', function (worker, code, signal) {
var exitCode = worker.process.exitCode;
var err = new Error(util.format('worker %s died (code: %s, signal: %s, suicide: %s, state: %s)',
worker.process.pid, exitCode, signal, worker.suicide, worker.state));
err.name = 'WorkerDiedError';
console.error('[%s] [master:%s] wroker exit: %s', Date(), process.pid, err.stack);
})
// if you do not listen to this event
// cfork will output this message to stderr
.on('unexpectedExit', function (worker, code, signal) {
// logger what you want
});
// if you do not listen to this event
// cfork will listen it and output the error message to stderr
process.on('uncaughtException', function (err) {
// do what you want
});
const agentWorker = childprocess.fork('./agent.js', [], { execArgv: [] });
setInterval(() => {
console.log('hi master')
}, 1000)
process.once('exit', function (err) {
console.log('exit');
});