Skip to content

Commit

Permalink
feat(docker): Handle SIGTERM event and add test/oa in .dockerignore (#46
Browse files Browse the repository at this point in the history
)

* feat(docker): Handle SIGTERM event and add test/oa in .dockerignore

* feat(docker): Avoid use of process.exit
  • Loading branch information
Joxit authored and missinglink committed Jun 11, 2019
1 parent 8132938 commit 5db7432
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ resources/whosonfirst/dictionaries/*/*.txt
!resources/whosonfirst/dictionaries/region/wof:shortcode.txt
!resources/whosonfirst/dictionaries/region/name:eng_x_preferred.txt
!resources/whosonfirst/dictionaries/region/abrv:eng_x_preferred.txt
!resources/whosonfirst/dictionaries/locality/name:eng_x_preferred.txt
!resources/whosonfirst/dictionaries/locality/name:eng_x_preferred.txt

# ignore any openaddresses test cases
test/oa
30 changes: 25 additions & 5 deletions server/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const envCpus = parseInt(process.env.CPUS, 10)
const cpus = Math.min(Math.max(envCpus || Infinity, 1), os.cpus().length)

// optionally override port/host using env var
var PORT = process.env.PORT || 3000
var HOST = process.env.HOST || undefined
var app = express()
const PORT = process.env.PORT || 3000
const HOST = process.env.HOST || undefined
const app = express()

// init placeholder and store it on $app
console.error('parser loading')
Expand Down Expand Up @@ -61,21 +61,41 @@ if (cpus > 1) {
console.error('[master] worker forked', worker.process.pid)
})

// handle SIGTERM (required for fast docker restarts)
process.on('SIGTERM', () => {
console.error('[master] closing app')
Object.keys(cluster.workers)
.map(id => cluster.workers[id])
.forEach(worker => worker.send('graceful-shutdown'))
})

// fork workers
for (var c = 0; c < cpus; c++) {
cluster.fork()
}
} else {
app.listen(PORT, HOST, () => {
const server = app.listen(PORT, HOST, () => {
console.error('[worker %d] listening on %s:%s', process.pid, HOST || '0.0.0.0', PORT)
})
process.on('message', (msg) => {
// handle SIGTERM (required for fast docker restarts)
if (msg === 'graceful-shutdown') {
console.error('[worker %d] closing server', process.pid)
server.close(() => cluster.worker.disconnect())
}
})
}

// start single-threaded server
} else {
console.error('[master] using %d cpus', cpus)

app.listen(PORT, HOST, () => {
const server = app.listen(PORT, HOST, () => {
console.log('[master] listening on %s:%s', HOST || '0.0.0.0', PORT)
// handle SIGTERM (required for fast docker restarts)
process.on('SIGTERM', () => {
console.error('[master] closing app')
server.close()
})
})
}

0 comments on commit 5db7432

Please sign in to comment.