Skip to content

Commit

Permalink
Handle termination signals to stop container faster
Browse files Browse the repository at this point in the history
  • Loading branch information
coocos committed Apr 18, 2021
1 parent 756bc08 commit 40aea98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import http from "http";
import process from "process";

import db from "./db";
import logger from "./logger";
import { startServer } from "./server";

startServer();
function shutdown(server: http.Server): void {
logger.info("Shutting down");
server.close(() => {
db.destroy();
process.exit(1);
});
}

const server = startServer();
process.on("SIGINT", () => shutdown(server));
process.on("SIGTERM", () => shutdown(server));
4 changes: 1 addition & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export function startServer(): http.Server {
const broadcast = webSocketServer(httpServer);

const feedPoller = pollFeed(broadcast);
httpServer.on("close", () => {
feedPoller.destroy();
});
httpServer.on("close", () => feedPoller.destroy());

return httpServer;
}

0 comments on commit 40aea98

Please sign in to comment.