diff --git a/index.js b/index.js index 890c92b..1a2cd97 100755 --- a/index.js +++ b/index.js @@ -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); + }); + }); }); } @@ -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) => { @@ -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. @@ -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(); } /** diff --git a/package-lock.json b/package-lock.json index 5dfb17a..349ef9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "hydra-express", - "version": "1.4.11", + "version": "1.4.12", "lockfileVersion": 1, "dependencies": { "accepts": { diff --git a/package.json b/package.json index 25c00c2..8b8ae41 100644 --- a/package.json +++ b/package.json @@ -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"