From 68640860fb9cf584f155348ae06e4536226bef16 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 18 Mar 2023 12:37:34 -0400 Subject: [PATCH] /_health endpoint now checks the database and redis connections --- server/index.ts | 2 -- server/routes/index.ts | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/server/index.ts b/server/index.ts index de546d29d..2af4591c9 100644 --- a/server/index.ts +++ b/server/index.ts @@ -90,8 +90,6 @@ async function start(id: number, disconnect: () => void) { // Apply default rate limit to all routes app.use(defaultRateLimiter()); - // install health check endpoint for all services - router.get("/_health", (ctx) => (ctx.body = "OK")); app.use(router.routes()); // loop through requested services at startup diff --git a/server/routes/index.ts b/server/routes/index.ts index 6daa6154c..d3b074c74 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -6,9 +6,11 @@ import send from "koa-send"; import userAgent, { UserAgentContext } from "koa-useragent"; import { languages } from "@shared/i18n"; import { IntegrationType } from "@shared/types"; +import { sequelize } from "@server/database/sequelize"; import env from "@server/env"; import { NotFoundError } from "@server/errors"; import { Integration } from "@server/models"; +import RedisAdapter from "@server/redis"; import { opensearchResponse } from "@server/utils/opensearch"; import { getTeamFromContext } from "@server/utils/passport"; import { robotsResponse } from "@server/utils/robots"; @@ -116,6 +118,22 @@ router.get("/opensearch.xml", (ctx) => { ctx.body = opensearchResponse(ctx.request.URL.origin); }); +router.get("/_health", async (ctx) => { + try { + await sequelize.query("SELECT 1"); + } catch (err) { + throw new Error("Database connection failed"); + } + + try { + await RedisAdapter.defaultClient.ping(); + } catch (err) { + throw new Error("Redis ping failed"); + } + + ctx.body = "OK"; +}); + router.get("/s/:shareId", renderShare); router.get("/s/:shareId/doc/:documentSlug", renderShare); router.get("/s/:shareId/*", renderShare);