Skip to content

Commit

Permalink
cache health check
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 13, 2024
1 parent c853ff0 commit 5260292
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -135,4 +136,4 @@
"@semantic-release/github"
]
}
}
}
46 changes: 32 additions & 14 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import fs from "node:fs/promises"
import express from "express"
import { Cache } from "memory-cache"

export default class Server {
constructor(
Expand All @@ -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<string, Record<string, any>>} */
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"; }> }} */
Expand All @@ -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<void>} */
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4628,6 +4628,11 @@ [email protected]:
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==

[email protected]:
version "1.0.3"
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.3.tgz#d80319a65f3c7935351e5cfdac8f9318504dbed5"
Expand Down Expand Up @@ -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"
type-fest "^2.19.0"

0 comments on commit 5260292

Please sign in to comment.