Skip to content

Commit

Permalink
Handle SIGTERM (#782)
Browse files Browse the repository at this point in the history
* Handle SIGTERM

Resolve #781

* Add `Caught signal ...` messages

Container log when sending a `SIGHUP` signal and then a `SIGTERM` signal using
`docker kill --signal ...` :

```
Starting tileserver-gl v4.4.8
[INFO] Automatically creating config file for zurich_switzerland.mbtiles
[INFO] Only a basic preview style will be used.
[INFO] See documentation to learn how to create config.json file.
Run with --verbose to see the config file here.
Starting server
Listening at http://[::]:8080/
Startup complete
Caught signal SIGHUP, refreshing
Stopping server and reloading config
Starting server
Listening at http://[::]:8080/
Startup complete
Caught signal SIGTERM, stopping gracefully
```

Note that the numeric signal values, as used in the shell version, were
replaced by signal names, as used in nodeJS.

* run 'npm run lint:js:fix'

---------

Co-authored-by: acalcutt <[email protected]>
  • Loading branch information
zstadler and acalcutt authored Mar 1, 2023
1 parent b4d0619 commit 64adff0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,16 @@ function start(opts) {
};
}

/**
* Stop the server gracefully
*
* @param {string} signal Name of the received signal
*/
function stopGracefully(signal) {
console.log(`Caught signal ${signal}, stopping gracefully`);
process.exit();
}

/**
*
* @param opts
Expand All @@ -602,11 +612,11 @@ export function server(opts) {
process.exit(1);
});

process.on('SIGINT', () => {
process.exit();
});
process.on('SIGINT', stopGracefully);
process.on('SIGTERM', stopGracefully);

process.on('SIGHUP', () => {
process.on('SIGHUP', (signal) => {
console.log(`Caught signal ${signal}, refreshing`);
console.log('Stopping server and reloading config');

running.server.shutdown(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import glyphCompose from '@mapbox/glyph-pbf-composite';

/**
* Generate new URL object
*
* @param req
* @params {object} req - Express request
* @returns {URL} object
**/
*/
const getUrlObject = (req) => {
const urlObject = new URL(`${req.protocol}://${req.headers.host}/`);
// support overriding hostname by sending X-Forwarded-Host http header
Expand Down

0 comments on commit 64adff0

Please sign in to comment.