Skip to content

Instantly share code, notes, and snippets.

@justlaputa
Created September 21, 2019 08:48
Show Gist options
  • Save justlaputa/d24e66afbcbc0eaf6ba904a434e1a457 to your computer and use it in GitHub Desktop.
Save justlaputa/d24e66afbcbc0eaf6ba904a434e1a457 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) || 10
app.get('/api/ok', (req, res) => {
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')
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