From 2024c6e64f6da6d6a15d197adad28a0bd7d8dde1 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 29 Aug 2021 14:48:12 -0700 Subject: [PATCH 1/2] chore: Graceful server shutdown, closes #2507 --- flow-typed/globals.js | 3 +++ package.json | 3 ++- server/index.js | 13 +++++++++++-- yarn.lock | 5 +++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/flow-typed/globals.js b/flow-typed/globals.js index d90dd3395f..0d22cde77b 100644 --- a/flow-typed/globals.js +++ b/flow-typed/globals.js @@ -3,6 +3,9 @@ declare var process: { exit: (code?: number) => void, cwd: () => string, argv: Array, + on: (event: string, listener: Function) => void, + once: (event: string, listener: Function) => void, + off: (event: string, listener: Function) => void, env: { [string]: string, }, diff --git a/package.json b/package.json index f8d1b949ac..dee5c5b22a 100644 --- a/package.json +++ b/package.json @@ -155,6 +155,7 @@ "socket.io": "^2.4.0", "socket.io-redis": "^5.4.0", "socketio-auth": "^0.1.1", + "stoppable": "^1.1.0", "string-replace-to-array": "^1.0.3", "styled-components": "^5.2.3", "styled-components-breakpoint": "^2.1.1", @@ -212,4 +213,4 @@ "js-yaml": "^3.13.1" }, "version": "0.58.0" -} \ No newline at end of file +} diff --git a/server/index.js b/server/index.js index ac675c6e02..7e06fa5cab 100644 --- a/server/index.js +++ b/server/index.js @@ -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({ diff --git a/yarn.lock b/yarn.lock index d7ea31fc27..39b51d6fc3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12413,6 +12413,11 @@ stealthy-require@^1.1.1: resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= +stoppable@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/stoppable/-/stoppable-1.1.0.tgz#32da568e83ea488b08e4d7ea2c3bcc9d75015d5b" + integrity sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw== + stream-browserify@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.2.tgz#87521d38a44aa7ee91ce1cd2a47df0cb49dd660b" From 08a8fea69af6cdab03e626c0a064845e57ea41e2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 29 Aug 2021 19:44:06 -0700 Subject: [PATCH 2/2] chore: Add /_health endpoint to all services closes #2506 --- server/index.js | 7 +++++++ server/routes/index.js | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/server/index.js b/server/index.js index 7e06fa5cab..4c5638038d 100644 --- a/server/index.js +++ b/server/index.js @@ -6,6 +6,7 @@ import Koa from "koa"; import compress from "koa-compress"; import helmet from "koa-helmet"; import logger from "koa-logger"; +import Router from "koa-router"; import { uniq } from "lodash"; import stoppable from "stoppable"; import throng from "throng"; @@ -40,7 +41,9 @@ async function start(id, disconnect) { const server = stoppable(http.createServer(app.callback())); const httpLogger = debug("http"); const log = debug("server"); + const router = new Router(); + // install basic middleware shared by all services app.use(logger((str, args) => httpLogger(str))); app.use(compress()); app.use(helmet()); @@ -56,6 +59,10 @@ async function start(id, disconnect) { await init(app, server); } + // install health check endpoint for all services + router.get("/_health", (ctx) => (ctx.body = "OK")); + app.use(router.routes()); + server.on("error", (err) => { throw err; }); diff --git a/server/routes/index.js b/server/routes/index.js index 8f71468665..8d82789326 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -93,8 +93,6 @@ koa.use( }) ); -router.get("/_health", (ctx) => (ctx.body = "OK")); - if (process.env.NODE_ENV === "production") { router.get("/static/*", async (ctx) => { ctx.set({