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
·75 lines (59 loc) · 2.26 KB
/
server.js
File metadata and controls
executable file
·75 lines (59 loc) · 2.26 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
#!/usr/bin/env node
const config = require('config');
const app = require('engine/koa/app');
const log = require('engine/log')();
app.waitBootAndListen(config.server.host, config.server.port).then(() => {
log.info("App is listening");
}).catch(function(err) {
log.error(err);
process.exit(1); // fatal error, could not boot!
});
process.on('SIGINT', async () => {
// The process is going to be reloaded
// Have to close all database/socket.io/* connections
let 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 notifyAboutProblem() {
// just log, kill is accomplished by PM2
log.error("App is stopping for too long! Will be killed now!");
}, dieDelay - 100);
log.info("Closing the app...");
await app.close();
// messages below may be not in logs due to PM2 bug
log.info("App closed, exiting");
process.exit(0);
});
// отслеживаем unhandled ошибки
// https://iojs.org/api/process.html#process_event_rejectionhandled
let 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);
let 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;
}
});