forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·91 lines (72 loc) · 2.47 KB
/
server.js
File metadata and controls
executable file
·91 lines (72 loc) · 2.47 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
#!/usr/bin/env node
const config = require('config');
const co = require('co');
const app = require('app');
const log = require('log')();
co(function*() {
yield* app.waitBootAndListen(config.server.host, config.server.port);
log.info("App is listening");
}).catch(function(err) {
log.error(err);
process.exit(1); // fatal error, could not boot!
});
process.on('message', function(msg) {
if (msg == 'shutdown') { // PM2 sends this on graceful reload
shutdown();
}
});
function shutdown() {
// The process is going to be reloaded
// Have to close all database/socket.io/* connections
co(function*() {
log.info("Closing the app...");
yield* app.close();
// messages below not in logs due to PM2 bug
log.info("App closed");
}).then(function() {
log.info("Exiting");
process.exit(0);
}, function(err) {
log.error(err);
});
var dieDelay = process.env.PM2_GRACEFUL_TIMEOUT || 4000;
// I have 4000ms to let all connections finish
// not accepting new connections, closing socket.io (if used)
// keep-alive connection to server may still be alive, but safe to nuke the server w/ them
setTimeout(function() {
log.error("App is stopping for too long! Will be killed now!");
// kill is accomplished by PM2
}, dieDelay - 100);
}
// отслеживаем unhandled ошибки
// https://iojs.org/api/process.html#process_event_rejectionhandled
var unhandledRejections = [];
process.on('unhandledRejection', function(reason, p) {
p.trackRejectionId = Math.random();
setTimeout(function() { // 100 ms to catch up and handle rejection
if (p.trackRejectionId) { // if not rejectionHandled yet, report
unhandledRejections.push(p);
var report = {
err: reason,
trackRejectionId: p.trackRejectionId,
length: unhandledRejections.length
};
log.error(report, "unhandledRejection");
}
}, 100);
});
// если вдруг есть catch, но позже - скажем и об этом, с указанием промиса /trackRejectionId/
process.on('rejectionHandled', function(p) {
if (~unhandledRejections.indexOf(p)) {
// too more than 100 ms to handle
// already in the rejection list, let's report
unhandledRejections.splice(unhandledRejections.indexOf(p), 1);
log.error({
trackRejectionId: p.trackRejectionId,
length: unhandledRejections.length
}, "rejectionHandled");
} else {
// handled soon, don't track
delete p.trackRejectionId;
}
});