This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (106 loc) · 3.54 KB
/
index.js
File metadata and controls
117 lines (106 loc) · 3.54 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var Child = require('./lib/Child'),
utils = require('./lib/utils');
var children = {};
var respawnMax = null;
var respawnDelay = 0;
var shutdownInitiated = false;
function forkCommand( command, options ){
if(shutdownInitiated == true) return;
options = options || {};
options.command = command;
options.respawnMax = (typeof options.respawnMax === 'undefined') ? respawnMax : options.respawnMax
options.onExit = (function(onExit){
return function managerChildOnExit( code, signal ){
utils.debugLog( 0, '[EXIT]',child.id, child.command, 'Code:', code, 'Signal:', signal );
if(onExit) onExit();
if( child.bury){
utils.debugLog( 0, '[Info]',child.id, child.command, 'Stopped' );
delete children[child.id];
return;
}
if( !shutdownInitiated ){
if( child.respawnMax !== null && child.respawnCount > child.respawnMax ){
utils.debugLog( 0, '[DEAD]',child.id, child.command, 'reached max respawn count.' );
return;
}
setTimeout(function(){
utils.debugLog( 0, '[INFO]',child.id, child.command, 'restarting for the', child.respawnCount+1, 'time')
child.respawn();
},respawnDelay);
}
}
})(options.onExit);
options.onClose = (function(onClose){
return function managerChildOnClose(){
utils.debugLog( 1, '[CLOSE]', child.id, child.command );
if(onClose) onClose();
};
})(options.onClose);
options.onDisconnect = (function(onDisconnect){
return function managerChildOnDisconnect(){
utils.debugLog( 1, '[DISCONNECT]', child.id, child.command );
if(onDisconnect) onDisconnect();
};
})(options.onDisconnect);
options.onMessage = (function(onMessage){
return function managerChildOnMessage( msg ){
utils.debugLog( 2, '[MESSAGE]', child.id, child.command, JSON.stringify(msg) );
if(onMessage) onMessage(msg);
};
})(options.onMessage);
var child = new Child(options);
child.fork();
children[child.id] = child;
return child.id;
};
function listTracked(){
var list = {};
for(var i in children){
var rawChild = children[i];
var child = {};
for(var j in rawChild){
if( j !== 'process' ) child[j] = rawChild[j];
}
list[i] = child;
}
console.log( '[INFO] Tracked Processes:', JSON.stringify(list) );
return list;
}
function killProcess( id, signal ){
return children[id] && children[id].kill( signal );
};
function buryProcess( id ){
var child = children[id];
if(child){
child.bury = true;
child.kill();
}
else return;
}
function shutdown(){
shutdownInitiated = true;
for(var i in children){
children[i].kill();
}
process.exit();
}
function changeRespawnMax( max ){
if(typeof respawnMax === 'null') respawnMax = 0;
if(typeof respawnMax === 'number') respawnMax = max;
return respawnMax;
}
function changeRespawnDelay( delay ){
if(typeof delay === 'null') respawnDelay = 0;
if(typeof delay === 'number') respawnDelay = delay;
return respawnDelay;
}
module.exports = {
forkCommand: forkCommand,
listTracked: listTracked,
killProcess: killProcess,
buryProcess: buryProcess,
shutdown: shutdown,
debugLevel: utils.debugLevel,
respawnMax: changeRespawnMax,
respawnDelay: changeRespawnDelay
}