-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Methods.js
295 lines (265 loc) · 6.26 KB
/
Methods.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict';
/**
* @file Utilities for PM2
* @author Alexandre Strzelewicz <[email protected]>
* @project PM2
*/
var p = require('path');
var Common = require('../Common');
var treekill = require('../TreeKill');
var cst = require('../../constants.js');
var debug = require('debug')('god:methods');
/**
* Description
* @method exports
* @param {} God
* @return
*/
module.exports = function(God) {
/**
* Description
* @method logAndGenerateError
* @param {} err
* @return NewExpression
*/
God.logAndGenerateError = function(err) {
// Is an Error object
if (err instanceof Error) {
console.trace(err);
return err;
}
// Is a JSON or simple string
console.error(err);
return new Error(err);
};
/**
* Utility functions
* @method getProcesses
* @return MemberExpression
*/
God.getProcesses = function() {
return God.clusters_db;
};
/**
* Description
* @method getFormatedProcesses
* @return arr
*/
God.getFormatedProcesses = function getFormatedProcesses() {
var db = Common.clone(God.clusters_db);
var arr = [];
for (var key in db) {
if (db[key]) {
arr.push({
pid : db[key].process.pid,
name : db[key].pm2_env.name,
pm2_env : db[key].pm2_env,
pm_id : db[key].pm2_env.pm_id
});
}
}
db = null;
return arr;
};
/**
* Description
* @method findProcessById
* @param {} id
* @return ConditionalExpression
*/
God.findProcessById = function findProcessById(id) {
return God.clusters_db[id] ? God.clusters_db[id] : null;
};
/**
* Description
* @method findByName
* @param {} name
* @return arr
*/
God.findByName = function(name) {
var db = God.clusters_db;
var arr = [];
if (name == 'all') {
for (var key in db) {
// Avoid _old_proc process style
if (typeof(God.clusters_db[key].pm2_env.pm_id) === 'number')
arr.push(db[key]);
}
return arr;
}
for (var key in db) {
if (God.clusters_db[key].pm2_env.name == name ||
God.clusters_db[key].pm2_env.pm_exec_path == p.resolve(name)) {
arr.push(db[key]);
}
}
return arr;
};
/**
* Description
* @method findByScript
* @param {} script
* @param {} cb
* @return
*/
God.findByScript = function(script, cb) {
var db = Common.clone(God.clusters_db);
var arr = [];
for (var key in db) {
if (p.basename(db[key].pm2_env.pm_exec_path) == script) {
arr.push(db[key].pm2_env);
}
}
cb(null, arr.length == 0 ? null : arr);
};
/**
* Description
* @method findByPort
* @param {} port
* @param {} cb
* @return
*/
God.findByPort = function(port, cb) {
var db = God.clusters_db;
var arr = [];
for (var key in db) {
if (db[key].pm2_env.port && db[key].pm2_env.port == port) {
arr.push(db[key].pm2_env);
}
}
cb(null, arr.length == 0 ? null : arr);
};
/**
* Description
* @method findByFullPath
* @param {} path
* @param {} cb
* @return
*/
God.findByFullPath = function(path, cb) {
var db = God.clusters_db;
var procs = [];
for (var key in db) {
if (db[key].pm2_env.pm_exec_path == path) {
procs.push(db[key]);
}
}
cb(null, procs.length == 0 ? null : procs);
};
/**
* Check if a process is alive in system processes
* Return TRUE if process online
* @method checkProcess
* @param {} pid
* @return
*/
God.checkProcess = function(pid) {
if (!pid) return false;
try {
// Sending 0 signal do not kill the process
process.kill(pid, 0);
return true;
}
catch (err) {
return false;
}
};
/**
* Description
* @method processIsDead
* @param {} pid
* @param {} cb
* @return Literal
*/
God.processIsDead = function(pid, cb) {
if (!pid) return cb({type : 'param:missing', msg : 'no pid passed'});
var timeout = null;
var timer = setInterval(function() {
if (God.checkProcess(pid) === false) {
console.log('Process with pid %d killed', pid);
clearTimeout(timeout);
clearInterval(timer);
return cb(null, true);
}
console.log('Process with pid %d still not killed, retrying...', pid);
return false;
}, 50);
timeout = setTimeout(function() {
clearInterval(timer);
console.log('Process with pid %d still alive after %sms', pid, cst.KILL_TIMEOUT);
return cb({type : 'timeout', msg : 'timeout'});
}, cst.KILL_TIMEOUT);
return false;
};
/**
* Description
* @method killProcess
* @param int pid
* @param Object pm2_env
* @param function cb
* @return CallExpression
*/
God.killProcess = function(pid, pm2_env, cb) {
if (!pid) return cb({msg : 'no pid passed or null'});
var mode = pm2_env.exec_mode;
try {
if(pm2_env.treekill !== true)
process.kill(pid);
else {
if (mode.indexOf('cluster') === 0)
treekill(pid);
else
process.kill(-(pid), 'SIGINT');
}
} catch(e) {
console.error('%s pid can not be killed', pid, e);
return cb({type : 'kill', msg : pid + ' can not be killed'});
}
return God.processIsDead(pid, cb);
};
/**
* Description
* @method getNewId
* @return UpdateExpression
*/
God.getNewId = function() {
return God.next_id++;
};
/**
* When a process is restarted or reloaded reset fields
* to monitor unstable starts
* @method resetState
* @param {} pm2_env
* @return
*/
God.resetState = function(pm2_env) {
pm2_env.created_at = Date.now();
pm2_env.unstable_restarts = 0;
};
/**
* Description
* @method deepReset
* @param {} pm2_env
* @return
*/
God.deepReset = function(pm2_env) {
pm2_env.created_at = Date.now();
pm2_env.unstable_restarts = 0;
};
/**
* Description
* @method forcegc
* @return
*/
God.forceGc = function(opts, cb) {
if (global.gc) {
global.gc();
debug('Garbage collection triggered successfully');
if (cb) cb(null, {success: true});
}
else {
debug('Garbage collection failed');
if (cb) cb(null, {success: false});
}
};
};