diff --git a/package.json b/package.json index 245d9c1..e343d46 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "express": "^4.21.2", "formik": "^2.2.9", "js-cookie": "^3.0.5", + "memory-cache": "^0.2.0", "qs": "^6.11.2", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -135,4 +136,4 @@ "@semantic-release/github" ] } -} \ No newline at end of file +} diff --git a/src/server.js b/src/server.js index d6df5f6..49e765a 100644 --- a/src/server.js +++ b/src/server.js @@ -8,6 +8,7 @@ import fs from "node:fs/promises" import express from "express" +import { Cache } from "memory-cache" export default class Server { constructor( @@ -28,11 +29,17 @@ export default class Server { /** @type {string} */ this.hostname = this.envIsProduction ? "0.0.0.0" : "127.0.0.1" - /** @type {Express} */ + /** @type {import('express').Express} */ this.app = express() - /** @type {import('vite').ViteDevServer | undefined} */ this.vite = undefined + + /** @type {import('memory-cache').Cache>} */ + this.cache = new Cache() + /** @type {string} */ + this.healthCheckCacheKey = "health-check" + /** @type {number} */ + this.healthCheckCacheTimeout = 30000 } /** @type {(request: Request) => { healthStatus: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown"; additionalInfo: string; details?: Array<{ name: string; description: string; health: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown"; }> }} */ @@ -45,21 +52,32 @@ export default class Server { /** @type {(request: Request, response: Response) => void} */ handleHealthCheck(request, response) { - const healthCheck = this.getHealthCheck(request) + let value = this.cache.get(this.healthCheckCacheKey) + if (value === null) { + const healthCheck = this.getHealthCheck(request) + + if (healthCheck.healthStatus !== "healthy") { + console.warn(`health check: ${JSON.stringify(healthCheck)}`) + } + + value = { + appId: process.env.APP_ID || "REPLACE_ME", + healthStatus: healthCheck.healthStatus, + lastCheckedTimestamp: new Date().toISOString(), + additionalInformation: healthCheck.additionalInfo, + startupTimestamp: new Date().toISOString(), + appVersion: process.env.APP_VERSION || "REPLACE_ME", + details: healthCheck.details || [], + } - if (healthCheck.healthStatus !== "healthy") { - console.warn(`health check: ${JSON.stringify(healthCheck)}`) + this.cache.put( + this.healthCheckCacheKey, + value, + this.healthCheckCacheTimeout, + ) } - response.json({ - appId: process.env.APP_ID || "REPLACE_ME", - healthStatus: healthCheck.healthStatus, - lastCheckedTimestamp: new Date().toISOString(), - additionalInformation: healthCheck.additionalInfo, - startupTimestamp: new Date().toISOString(), - appVersion: process.env.APP_VERSION || "REPLACE_ME", - details: healthCheck.details || [], - }) + response.json(value) } /** @type {(request: Request, response: Response) => Promise} */ diff --git a/yarn.lock b/yarn.lock index ce6b543..82bcf3d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4628,6 +4628,11 @@ media-typer@0.3.0: resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ== +memory-cache@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/memory-cache/-/memory-cache-0.2.0.tgz#7890b01d52c00c8ebc9d533e1f8eb17e3034871a" + integrity sha512-OcjA+jzjOYzKmKS6IQVALHLVz+rNTMPoJvCztFaZxwG14wtAW7VRZjwTQu06vKCYOxh4jVnik7ya0SXTB0W+xA== + merge-descriptors@1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.3.tgz#d80319a65f3c7935351e5cfdac8f9318504dbed5" @@ -6336,4 +6341,4 @@ yup@^1.1.1: property-expr "^2.0.5" tiny-case "^1.0.3" toposort "^2.0.2" - type-fest "^2.19.0" \ No newline at end of file + type-fest "^2.19.0"