-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ProcessContainer.js
312 lines (263 loc) · 8.84 KB
/
ProcessContainer.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Copyright 2013 the PM2 project authors. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*
* This file wrap target application
* - redirect stdin, stderr to bus + log files
* - rename process
* - pid
*/
var p = require('path');
var cst = require('../constants');
var Utility = require('./Utility.js');
var ProcessUtils = require('./ProcessUtils');
// Load all env-vars from master.
var pm2_env = JSON.parse(process.env.pm2_env);
for(var k in pm2_env) {
process.env[k] = pm2_env[k];
}
// Rename process
process.title = process.env.PROCESS_TITLE || 'node ' + pm2_env.pm_exec_path;
delete process.env.pm2_env;
/**
* Main entrance to wrap the desired code
*/
(function ProcessContainer() {
var fs = require('fs');
ProcessUtils.injectModules()
var stdFile = pm2_env.pm_log_path;
var outFile = pm2_env.pm_out_log_path;
var errFile = pm2_env.pm_err_log_path;
var pidFile = pm2_env.pm_pid_path;
var script = pm2_env.pm_exec_path;
var original_send = process.send;
if (typeof(process.env.source_map_support) != 'undefined' &&
process.env.source_map_support !== 'false') {
require('source-map-support').install();
}
process.send = function() {
if (process.connected)
original_send.apply(this, arguments);
};
//send node version
if (process.versions && process.versions.node) {
process.send({
'node_version': process.versions.node
});
}
if (cst.MODIFY_REQUIRE)
require.main.filename = pm2_env.pm_exec_path;
// Resets global paths for require()
require('module')._initPaths();
try {
fs.writeFileSync(pidFile, process.pid);
} catch (e) {
console.error(e.stack || e);
}
// Add args to process if args specified on start
if (process.env.args != null)
process.argv = process.argv.concat(pm2_env.args);
// stdio, including: out, err and entire (both out and err if necessary).
var stds = {
out: outFile,
err: errFile
};
stdFile && (stds.std = stdFile);
// uid/gid management
if (pm2_env.uid || pm2_env.gid) {
try {
if (pm2_env.uid)
process.setuid(pm2_env.uid);
if (process.env.gid)
process.setgid(pm2_env.gid);
} catch(e) {
setTimeout(function() {
console.error('%s on call %s', e.message, e.syscall);
console.error('%s is not accessible', pm2_env.uid);
return process.exit(1);
}, 100);
}
}
exec(script, stds);
})();
/**
* Description
* @method exec
* @param {} script
* @param {} stds
* @return
*/
function exec(script, stds) {
if (p.extname(script) == '.coffee') {
try {
require('coffee-script/register');
} catch (e) {
console.error('Failed to load CoffeeScript interpreter:', e.message || e);
}
}
if (p.extname(script) == '.ls') {
try {
require('livescript');
} catch (e) {
console.error('Failed to load LiveScript interpreter:', e.message || e);
}
}
if (p.extname(script) == '.ts' || p.extname(script) == '.tsx') {
try {
require('ts-node/register');
} catch (e) {
console.error('Failed to load Typescript interpreter:', e.message || e);
}
}
process.on('message', function (msg) {
if (msg.type === 'log:reload') {
for (var k in stds){
if (typeof stds[k] == 'object' && !isNaN(stds[k].fd)){
if (stds[k].destroy) stds[k].destroy();
else if (stds[k].end) stds[k].end();
else if (stds[k].close) stds[k].close();
stds[k] = stds[k]._file;
}
}
Utility.startLogging(stds, function (err) {
if (err)
return console.error('Failed to reload logs:', err.stack);
console.log('Reloading log...');
});
}
});
var dateFns = null;
if (pm2_env.log_date_format)
dateFns = require('date-fns');
Utility.startLogging(stds, function (err) {
if (err) {
process.send({
type : 'process:exception',
data : {
message: err.message,
syscall: 'ProcessContainer.startLogging'
}
});
throw err;
return;
}
process.stderr.write = (function(write) {
return function(string, encoding, cb) {
var log_data = null;
// Disable logs if specified
if (pm2_env.disable_logs === true) {
return cb ? cb() : false;
}
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : string.toString(),
timestamp : pm2_env.log_date_format && dateFns ?
dateFns.format(Date.now(), pm2_env.log_date_format) : new Date().toISOString(),
type : 'err',
process_id : pm2_env.pm_id,
app_name : pm2_env.name
}) + '\n';
}
else if (pm2_env.log_date_format && dateFns)
log_data = `${dateFns.format(Date.now(), pm2_env.log_date_format)}: ${string.toString()}`;
else
log_data = string.toString();
process.send({
type : 'log:err',
topic : 'log:err',
data : log_data
});
if (Utility.checkPathIsNull(pm2_env.pm_err_log_path) &&
(!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
return cb ? cb() : false;
stds.std && stds.std.write && stds.std.write(log_data, encoding);
stds.err && stds.err.write && stds.err.write(log_data, encoding, cb);
};
})(process.stderr.write);
process.stdout.write = (function(write) {
return function(string, encoding, cb) {
var log_data = null;
// Disable logs if specified
if (pm2_env.disable_logs === true) {
return cb ? cb() : false;
}
if (pm2_env.log_type && pm2_env.log_type === 'json') {
log_data = JSON.stringify({
message : string.toString(),
timestamp : pm2_env.log_date_format && dateFns ?
dateFns.format(Date.now(), pm2_env.log_date_format) : new Date().toISOString(),
type : 'out',
process_id : pm2_env.pm_id,
app_name : pm2_env.name
}) + '\n';
}
else if (pm2_env.log_date_format && dateFns)
log_data = `${dateFns.format(Date.now(), pm2_env.log_date_format)}: ${string.toString()}`;
else
log_data = string.toString();
process.send({
type : 'log:out',
data : log_data
});
if (Utility.checkPathIsNull(pm2_env.pm_out_log_path) &&
(!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
return cb ? cb() : null;
stds.std && stds.std.write && stds.std.write(log_data, encoding);
stds.out && stds.out.write && stds.out.write(log_data, encoding, cb);
};
})(process.stdout.write);
function getUncaughtExceptionListener(listener) {
return function uncaughtListener(err) {
var error = err && err.stack ? err.stack : err;
if (listener === 'unhandledRejection') {
error = 'You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:\n' + error;
}
logError(['std', 'err'], error);
// Notify master that an uncaughtException has been catched
try {
if (err) {
var errObj = {};
Object.getOwnPropertyNames(err).forEach(function(key) {
errObj[key] = err[key];
});
}
process.send({
type : 'log:err',
topic : 'log:err',
data : '\n' + error + '\n'
});
process.send({
type : 'process:exception',
data : errObj !== undefined ? errObj : {message: 'No error but ' + listener + ' was caught!'}
});
} catch(e) {
logError(['std', 'err'], 'Channel is already closed can\'t broadcast error:\n' + e.stack);
}
if (!process.listeners(listener).filter(function (listener) {
return listener !== uncaughtListener;
}).length) {
if (listener == 'uncaughtException') {
process.emit('disconnect');
process.exit(cst.CODE_UNCAUGHTEXCEPTION);
}
}
}
}
process.on('uncaughtException', getUncaughtExceptionListener('uncaughtException'));
process.on('unhandledRejection', getUncaughtExceptionListener('unhandledRejection'));
// Change dir to fix process.cwd
process.chdir(pm2_env.pm_cwd || process.env.PWD || p.dirname(script));
if (ProcessUtils.isESModule(script) === true)
import(process.env.pm_exec_path);
else
require('module')._load(script, null, true);
function logError(types, error){
try {
types.forEach(function(type){
stds[type] && typeof stds[type].write == 'function' && stds[type].write(error + '\n');
});
} catch(e) { }
}
});
}