feat: reimplement health checker from Python to Node.js.

This commit is contained in:
Marco Gallegos
2025-12-17 20:29:20 -06:00
parent 419b8a283b
commit 949a7c22cc
3 changed files with 237 additions and 25 deletions

View File

@@ -2,6 +2,7 @@ const express = require("express");
const path = require("path");
const fs = require("fs");
const { exec } = require("child_process");
const { runHealthChecker } = require("./scripts/health_checker");
const app = express();
const port = process.env.PORT || 3001;
@@ -17,28 +18,19 @@ app.use(express.static(rootDir, { index: "index.html" }));
/**
* @route GET /healthchecker
* @description Ejecuta un script de Python para un chequeo de salud avanzado.
* Invoca el script `scripts/health_checker.py`, que monitorea múltiples servicios
* y devuelve un reporte detallado en formato JSON.
* @description Ejecuta un chequeo de salud avanzado implementado en Node.js.
* Lee `data/sites.json`, consulta los servicios definidos y devuelve un reporte JSON.
* @returns {object} Un objeto JSON con el estado de los servicios monitoreados.
* @throws 500 - Si el script de Python falla o su salida no es un JSON válido.
* @throws 500 - Si ocurre algún error durante la ejecución del chequeo.
*/
app.get("/healthchecker", (req, res) => {
const pythonScriptPath = path.join(rootDir, "scripts", "health_checker.py");
exec(`python3 ${pythonScriptPath}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error al ejecutar health_checker.py: ${stderr}`);
return res.status(500).json({ error: "No se pudo ejecutar el chequeo de salud" });
}
try {
const healthData = JSON.parse(stdout);
res.status(200).json(healthData);
} catch (parseErr) {
console.error(`Error al parsear la salida del chequeo de salud: ${parseErr}`);
res.status(500).json({ error: "La respuesta del chequeo de salud no es un JSON válido" });
}
});
app.get("/healthchecker", async (req, res) => {
try {
const healthData = await runHealthChecker();
res.status(200).json(healthData);
} catch (error) {
console.error("Error al ejecutar el health checker:", error);
res.status(500).json({ error: "No se pudo ejecutar el chequeo de salud" });
}
});
/**