Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make boot.sh process PID 1 #53

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN chmod +x /usr/src/app/build-production.sh

EXPOSE ${PORT}

CMD bash boot.sh
CMD ["/usr/src/app/boot.sh"]

# This stuff only runs when building an image from the template
ONBUILD RUN rm -Rf /app/scripts
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions helpers/mu/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, errorHandler } from './server';
import { app, errorHandler, setExitHandler } from './server';
import sparql from './sparql';
import { v1 as uuidV1 } from 'uuid';

Expand Down Expand Up @@ -53,7 +53,8 @@ export {
sparqlEscapeDateTime,
sparqlEscapeBool,
uuid,
errorHandler
errorHandler,
setExitHandler
};

export default mu;
28 changes: 26 additions & 2 deletions helpers/mu/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}