-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpm2-ant.js
96 lines (84 loc) · 2.36 KB
/
pm2-ant.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
var chalk = require('chalk'),
path = require('path'),
fs = require('fs'),
_ = require('lodash'),
Monitor = require('./lib/monitor'),
Log = require('./lib/util/log');
exports.start = function (options) {
process.title = 'pm2-ant slave';
options = options || {};
var confFile = options.confFile;
if (!confFile) {
confFile = './pm2-ant.ini';
if (process.argv.length > 2) {
confFile = process.argv[2];
}
confFile = path.resolve(__dirname, confFile);
if (!fs.existsSync(confFile)) {
console.error(chalk.bold(confFile), 'does not exist!');
return process.exit(0);
}
}
var monitor = Monitor({
confFile: confFile
});
Log(monitor.options.log);
console.log(chalk.cyan(
'\n' +
'█▀▀█ █▀▄▀█ █▀█ ░░ █▀▀█ █▀▀▄ ▀▀█▀▀ \n' +
'█░░█ █░▀░█ ░▄▀ ▀▀ █▄▄█ █░░█ ░░█░░ \n' +
'█▀▀▀ ▀░░░▀ █▄▄ ░░ ▀░░▀ ▀░░▀ ░░▀░░ \n'));
monitor.run();
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
process.on('SIGHUP', restart);
process.on('uncaughtException', caughtException);
process.on('exit', exports.exitGraceful)
function shutdown(code, signal) {
console.info('Shutting down....');
monitor.quit();
console.info('Shutdown complete!');
exports.exitGraceful(code, '-f');
}
function restart() {
if (process.send) {
process.send({
action: 'restart'
});
} else {
console.error('No IPC found, could not restart monitor, shutting down.');
shutdown(1);
}
}
function caughtException(err) {
console.error(err.stack);
shutdown(1);
}
};
exports.exitGraceful = function(code, signal) {
code = code || 0;
if (signal != '-f') {
console.debug('Slave has exited, code: ' + code + ', signal: ' + (signal || 'NULL'));
}
var fds = 0;
function tryToExit() {
if ((fds & 1) && (fds & 2)) {
process.exit(code);
}
}
[process.stdout, process.stderr].forEach(function (std) {
var fd = std.fd;
if (!std.bufferSize) {
fds = fds | fd;
} else {
std.write && std.write('', function () {
fds = fds | fd;
tryToExit();
});
}
});
tryToExit();
};
if (path.basename(process.mainModule.filename, '.js') == 'pm2-ant') {
exports.start();
}