Skip to content

Instantly share code, notes, and snippets.

@justlaputa
Created September 21, 2019 09:52
Show Gist options
  • Save justlaputa/4d360fb5d6f7c555157ffa55b94f19c9 to your computer and use it in GitHub Desktop.
Save justlaputa/4d360fb5d6f7c555157ffa55b94f19c9 to your computer and use it in GitHub Desktop.
kubernetes-graceful-shutdown-poc
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
const APP_NAME = 'callee'
const WAIT_BEFORE_SERVER_CLOSE = parseInt(process.env.WAIT_BEFORE_SERVER_CLOSE) || 5
app.get('/api/ok', (req, res) => {
res.send('ok')
})
app.get('/health', (req, res) => {
if (app.locals.shutdown) {
console.log('unhealthy!!')
res.status(400).end()
return
}
console.log('healthy :)')
res.send('ok')
})
const server = app.listen(PORT, () => {
console.log('%s server started on :%d', APP_NAME, PORT)
})
process.on('SIGTERM', () => {
console.log('received SIGTERM')
app.locals.shutdown = true
console.log('waiting for %d sec to close server', WAIT_BEFORE_SERVER_CLOSE)
setTimeout(() => {
console.log('calling server close')
server.close(() => {
console.log('server closed, exit')
process.exit(0)
})
}, WAIT_BEFORE_SERVER_CLOSE*1000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment