diff --git a/README.md b/README.md index 769c267..f6bcc86 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Requires: - a semantic.works stack, like mu-project - 'Develop your first microservice' + When developing inside an existing mu.semte.ch stack, it is easiest to set the development mode by setting the `NODE_ENV` environment variable to `development` and mount the sources directly. This makes it easy to setup links to the database and the dispatcher. Livereload is enabled automatically when running in development mode. ```yml @@ -216,6 +217,8 @@ The following SPARQL escape helpers are provided to construct safe SPARQL query - `sparqlEscapeDateTime(value) => string` - `sparqlEscapeBool(value) => string`: The given value is evaluated to a boolean value in javascript. E.g. the string value `'0'` evaluates to `false` in javascript. - `sparqlEscape(value, type) => string`: Function to escape a value in SPARQL according to the given type. Type must be one of `'string'`, `'uri'`, `'int'`, `'float'`, `'date'`, `'dateTime'`, `'bool'`. + - `setExitHandler`: *experimental* Sets a function to be ran on exit. The default implementation executes `process.exit` which allows for fast exiting of the service and therefore also fast restarts. + ### Error handling The template offers [an error handler](https://expressjs.com/en/guide/error-handling.html) to send error responses in a JSON:API compliant way. The handler can be imported from `'mu'` and need to be loaded at the end. diff --git a/helpers/mu/index.js b/helpers/mu/index.js index d8a40f4..4ad9c37 100644 --- a/helpers/mu/index.js +++ b/helpers/mu/index.js @@ -1,4 +1,4 @@ -import { app, errorHandler } from './server.js'; +import { app, errorHandler, setExitHandler } from './server.js'; import sparql from './sparql.js'; import { v1 as uuidV1 } from 'uuid'; @@ -53,7 +53,8 @@ export { sparqlEscapeDateTime, sparqlEscapeBool, uuid, - errorHandler + errorHandler, + setExitHandler }; export default mu; diff --git a/helpers/mu/server.js b/helpers/mu/server.js index 9ff7aeb..d8b9f87 100644 --- a/helpers/mu/server.js +++ b/helpers/mu/server.js @@ -36,14 +36,38 @@ const errorHandler = function(err, req, res, next) { }); }; + // start server -app.listen( port, hostname, function() { +const server = app.listen( port, hostname, function() { console.log(`Starting server on ${hostname}:${port} in ${app.get('env')} mode`); }); +// faster stopping +let exitHandler = function(server) { + console.debug("Preparing to shut down"); + server.close( () => { + console.debug("Shut down complete"); + }); +}; + +/** + * Sets a new handler for shutting down the server. + * + * @arg functor Function taking one argument (the result of app.listen + * when starting the server) which should gracefully stop the server. + */ +function setExitHandler( functor ) { + this.exitHandler = functor; +} + +process.on('SIGTERM', () => exitHandler(server) ); +process.on('SIGINT', () => exitHandler(server) ); + + export default app; export { app, - errorHandler + errorHandler, + setExitHandler }