-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_index.js
171 lines (151 loc) · 4.4 KB
/
server_index.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
var forever = require('forever-monitor');
var winston = require('winston');
var fs = require('fs');
var clc = require("cli-color");
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var os = require("os");
var ini = require('ini');
var config = ini.parse(fs.readFileSync('./config/config.ini', 'utf-8'));
if (config.log.logToFile) {
var log_file = fs.createWriteStream(config.log.logFile, {flags: 'a'});
}
/**
* Should we send mails, and where to?
*
* @type {number|*|{}}
*/
global.config = config;
var appname = config.global.name;
var sendMails = config.monitor.sendmail;
var transport = nodemailer.createTransport(smtpTransport({
host: config.autostart.smtphost,
port: config.autostart.smtpport,
ignoreTLS: config.autostart.ignore_tls
}));
// Parse arguments.
var extraArguments = process.argv[2];
if (!extraArguments) {
extraArguments = [];
}
var pid = process.pid.toString();
//Autostart parameters
console.log('[' + pid + '][AUTOSTART] Running with arguments: ' + extraArguments);
if (typeof global.config.autostart.min_uptime == 'undefined') {
global.config.autostart.min_uptime = 1000;
}
if (typeof global.config.autostart.spin_sleeptime == 'undefined') {
global.config.autostart.spin_sleeptime = 1000;
}
extraArguments.push(pid);
//Start child process.
var monitorProcess = new (forever.Monitor)('server_child.js', {
silent: true,
uid: 'requestador',
watch: false,
killTree: true,
logFile: global.config.monitor.logfile,
logFile: global.config.monitor.logfile,
errFile: global.config.monitor.logfile,
minUptime: parseInt(global.config.autostart.min_uptime, 10),
spinSleepTime: parseInt(config.autostart.spin_sleeptime, 10),
args: [extraArguments]
});
monitorProcess.on('watch:restart', function (info) {
writeRestart(appname + ': Restaring script because ' + info.file + ' changed', 'watch:restart');
});
monitorProcess.on('restart', function () {
writeRestart(appname + ': Forever restarting script for ' + monitorProcess.times + ' time', 'restart');
});
monitorProcess.on('exit:code', function (code) {
if (code == 143) {
writeRestart(appname + ': Forever detected script exited with code ' + code, 'exit: code');
} else {
writeRestart(appname + ': Forever detected script restart (reload config).');
}
});
/**
* Logging
* @type {*|string|exports.argvOptions.logFile|{alias}}
*/
var logFileName = global.config.log.logFile;
var logger = new (winston.Logger)({
exitOnError: false,
transports: [new winston.transports.File({
filename: logFileName,
'timestamp': function () {
return new Date(new Date().setHours(new Date().getHours() + 2)).toUTCString() + '';
},
maxFiles: 10,
json: false
})]
});
/**
* On restart log + mail.
* @param message
* @param reason
*/
function writeRestart(message, reason) {
logger.info(message);
var d = new Date();
console.log(clc.red(d.getDate() + "-" + d.getMonth() + "-" + d.getFullYear() + " " + d.toLocaleTimeString() + ": " + message));
sendMail(appname + ': ' + reason, message);
}
/**
* Send mail
*
* @param subject
* @param text
*/
function sendMail(subject, text) {
if (sendMails) {
transport.sendMail({
from: global.config.monitor.monitorMailFrom,
to: global.config.monitor.monitorMailTo,
subject: subject,
text: text
}, function (error, info) {
if (error) {
logger.error('Error mail not sent!: ' + error);
} else {
logger.info('Mail sent: ' + info.response);
}
});
}
}
/**
* Listen to SIGTERM
*
*/
process.on('SIGTERM', function (code) {
monitorProcess.stop();
});
/**
* Reboot Process, reloads config et all.
*/
process.on('SIGHUP', function () {
gracefullRestart();
});
/**
* Reboot Process, reloads config et all.
*/
process.on('SIGUSR2', function () {
gracefullRestart();
});
/**
* Gracefull restart
*/
var gracefullRestart = function (code) {
console.log(code);
console.log('[' + pid + '][AUTOSTART] SIGHUP signal received.');
console.log('[' + pid + '][AUTOSTART] kill child');
monitorProcess.stop();
setTimeout(function () {
console.log('[' + pid + '][AUTOSTART] starting child');
monitorProcess.start();
}, 2000);
}
/**
* Start the works.
*/
monitorProcess.start();