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 de12fcf50d..fe10d0bb90 100644 --- a/package.json +++ b/package.json @@ -162,6 +162,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", diff --git a/server/index.js b/server/index.js index 3552e66c3a..120425cbe8 100644 --- a/server/index.js +++ b/server/index.js @@ -7,7 +7,9 @@ import compress from "koa-compress"; import helmet from "koa-helmet"; import logger from "koa-logger"; import onerror from "koa-onerror"; +import Router from "koa-router"; import { uniq } from "lodash"; +import stoppable from "stoppable"; import throng from "throng"; import services from "./services"; import { initTracing } from "./tracing"; @@ -35,12 +37,14 @@ 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"); + const router = new Router(); + // install basic middleware shared by all services app.use(logger((str, args) => httpLogger(str))); app.use(compress()); app.use(helmet()); @@ -69,6 +73,10 @@ async function start() { 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; }); @@ -79,6 +87,14 @@ async function start() { }); server.listen(normalizedPortFlag || 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/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({ diff --git a/yarn.lock b/yarn.lock index 506f777d34..7c80d7c575 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13154,6 +13154,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"