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

Improved error handling #90

Merged
merged 1 commit into from
Aug 1, 2017
Merged
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
45 changes: 31 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,20 @@ class HydraExpress {
/**
* @name _shutdown
* @summary Shutdown hydra-express safely.
* @return {undefined}
* @return {object} Promise - promise resolving to hydraexpress ready or failure
*/
_shutdown() {
this.server.close(() => {
this.log('error', 'Service is shutting down.');
hydra.shutdown();
return new Promise((resolve, reject) => {
this.server.close(() => {
this.log('error', 'Service is shutting down.');
hydra.shutdown()
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
});
}

Expand Down Expand Up @@ -333,15 +341,16 @@ class HydraExpress {
* @param {function} err - error handler function
*/
process.on('cleanup', () => {
this._shutdown();
process.exit(0);
});
process.on('SIGTERM', () => {
this.log('fatal', 'Received SIGTERM');
process.emit('cleanup');
this._shutdown()
.then(() => {
process.exit(1);
})
.catch((_err) => {
process.exit(1);
});
});
process.on('SIGINT', () => {
this.log('fatal', 'Received SIGINT');
process.on('exit', () => {
console.trace();
process.emit('cleanup');
});
process.on('unhandledRejection', (reason, _p) => {
Expand All @@ -361,6 +370,14 @@ class HydraExpress {
}));
process.emit('cleanup');
});
process.on('SIGTERM', () => {
this.log('fatal', 'Received SIGTERM');
process.emit('cleanup');
});
process.on('SIGINT', () => {
this.log('fatal', 'Received SIGINT');
process.emit('cleanup');
});

/**
* Security.
Expand Down Expand Up @@ -570,10 +587,10 @@ class IHydraExpress extends HydraExpress {
/**
* @name shutdown
* @summary Shutdown hydra-express safely.
* @return {undefined}
* @return {object} Promise - promise resolving to hydraexpress ready or failure
*/
shutdown() {
super._shutdown();
return super._shutdown();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hydra-express",
"version": "1.4.11",
"version": "1.4.12",
"description": "A module which wraps Hydra and ExpressJS to provide an out of the box microservice which can support API routes and handlers.",
"author": {
"name": "Carlos Justiniano"
Expand Down