Skip to content

Commit

Permalink
feat: implement graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
reyhansofian authored Apr 29, 2020
2 parents 594e8f5 + 9d1efb4 commit cbb2e86
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ module.exports = function (merapi) {
if (!isRoutesInMiddleware) app.use(yield router(injector, routes, routerOptions));

app.start = function () {
app.listen(port, host);
app.__listen = app.listen(port, host);
app.__logger = logger;
logger.info(`Starting express on ${host}:${port}`);
};
return app;
Expand All @@ -61,6 +62,17 @@ module.exports = function (merapi) {
let app = yield merapi.resolve(this.apps[i]);
app.start();
}
},

*onStop() {
for (let i = 0; i < this.apps.length; i++) {
let app = yield merapi.resolve(this.apps[i]);
if (app.__listen) {
app.__listen.close(() => {
app.__logger.info("Shutting down express plugin...");
})
}
}
}
};
};
72 changes: 39 additions & 33 deletions 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
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"merapi": "^0.17.1",
"merapi": "^0.21.0",
"type-check": "^0.3.2"
},
"devDependencies": {
Expand Down

0 comments on commit cbb2e86

Please sign in to comment.