chore: Graceful server shutdown, closes #2507

This commit is contained in:
Tom Moor
2021-08-29 14:48:12 -07:00
parent 3dfd336f59
commit 2024c6e64f
4 changed files with 21 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import compress from "koa-compress";
import helmet from "koa-helmet";
import logger from "koa-logger";
import { uniq } from "lodash";
import stoppable from "stoppable";
import throng from "throng";
import "./sentry";
import services from "./services";
@ -34,9 +35,9 @@ const serviceNames = uniq(
.map((service) => service.trim())
);
async function start() {
async function start(id, disconnect) {
const app = new Koa();
const server = http.createServer(app.callback());
const server = stoppable(http.createServer(app.callback()));
const httpLogger = debug("http");
const log = debug("server");
@ -65,6 +66,14 @@ async function start() {
});
server.listen(env.PORT || "3000");
process.once("SIGTERM", shutdown);
process.once("SIGINT", shutdown);
function shutdown() {
console.log("\n> Stopping server");
server.stop(disconnect);
}
}
throng({