forked from hacash/explorer1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatch.js
48 lines (40 loc) · 1.22 KB
/
watch.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
46
47
48
/**
*Created by zzl on 2017/1/8.
*/
const config = require('./config.js')
var fork = require('child_process').fork;
//保存被子进程实例数组
var workers = [];
//这里的被子进程理论上可以无限多
var appsPath = ['./server.js'];
var createWorker = function(appPath){
//保存fork返回的进程实例
var worker = fork(appPath);
setTimeout(() => {
worker.kill() // 定时自动重启
}, config.watch_restart_timeout * 1000)
//监听子进程exit事件
worker.on('exit',function(){
if(config.watch_restart_timeout>=10){
console.log('worker: ' + worker.pid + ' exited');
}
delete workers[worker.pid];
setTimeout(function(){
createWorker(appPath);
}, 1000)
});
workers[worker.pid] = worker;
if(config.watch_restart_timeout>=10){
console.log('Create worker:' + worker.pid);
}
};
//启动所有子进程
for (var i = appsPath.length - 1; i >= 0; i--) {
createWorker(appsPath[i]);
}
//父进程退出时杀死所有子进程
process.on('exit',function(){
for(var pid in workers){
workers[pid].kill();
}
});