-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatus.js
More file actions
31 lines (27 loc) · 1004 Bytes
/
status.js
File metadata and controls
31 lines (27 loc) · 1004 Bytes
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
const database = require('@cheevr/database');
const rateLimit = require('express-rate-limit');
let shutdownTimer = process.env.NODE_SHUTDOWN_TIMER || 10;
let shutdown = false;
module.exports = app => {
// Health check that will gracefully signal shutdown
// TODO doesn't say what's wrong with the database
app.get('/health', rateLimit({windowMs: 2500}), (req, res) => {
if (!database.ready || shutdown) {
return res.status(503).end();
}
req.db.ping(err => {
if (err) {
return res.status(503).end();
}
res.end();
});
});
// Shutdown handler for graceful termination with delay for drain
process.on('SIGTERM', () => {
app.emit('shutdown');
console.log('SIGTERM: Shutting down in', shutdownTimer, 'seconds!');
shutdown = true;
setInterval(() => console.log(--shutdownTimer), 1001);
setTimeout(process.exit, shutdownTimer * 999, 0);
});
};