From 4fae603d29d2fd1a44a8126287ce7afca0891b7e Mon Sep 17 00:00:00 2001 From: signature18632 Date: Wed, 30 Jul 2025 03:24:53 +0700 Subject: [PATCH] health check cache --- packages/indexer-cache-validator/src/index.ts | 9 ++++++ .../src/lib/healthCheck.ts | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 packages/indexer-cache-validator/src/lib/healthCheck.ts diff --git a/packages/indexer-cache-validator/src/index.ts b/packages/indexer-cache-validator/src/index.ts index 4f784d8..040aefa 100644 --- a/packages/indexer-cache-validator/src/index.ts +++ b/packages/indexer-cache-validator/src/index.ts @@ -1,6 +1,7 @@ import { cache, logger, shutdownOperation, timeOperation } from "@intmax2-function/shared"; import { name } from "../package.json"; import { INTERVAL_MS } from "./constants"; +import { startHealthCheckServer } from "./lib/healthCheck"; import { performJob } from "./service/job.service"; async function executeJob() { @@ -29,6 +30,14 @@ async function scheduleNextExecution() { } async function main() { + try { + await startHealthCheckServer(); + logger.info("Health check server started successfully"); + } catch (error) { + logger.error("Failed to start health check server:", error); + throw error; + } + logger.info(`Starting ${name} adaptive interval job (target interval: ${INTERVAL_MS / 1000}s)`); scheduleNextExecution(); diff --git a/packages/indexer-cache-validator/src/lib/healthCheck.ts b/packages/indexer-cache-validator/src/lib/healthCheck.ts new file mode 100644 index 0000000..ae7e74c --- /dev/null +++ b/packages/indexer-cache-validator/src/lib/healthCheck.ts @@ -0,0 +1,29 @@ +import http from "node:http"; +import { config, logger } from "@intmax2-function/shared"; +import { name } from "../../package.json"; + +const server = http.createServer(async (req, res) => { + if (req.url === "/v1/health" && req.method === "GET") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + status: "healthy", + service: name, + timestamp: new Date().toISOString(), + }), + ); + return; + } + + res.writeHead(404); + res.end(); +}); + +export const startHealthCheckServer = async () => { + return new Promise((resolve) => { + server.listen(config.PORT, () => { + logger.info(`Health check server is running on port ${config.PORT}`); + resolve(true); + }); + }); +};