From 861062cd8dbb6a3a401fb677514dd7d710940a90 Mon Sep 17 00:00:00 2001 From: zyxd Date: Fri, 3 Oct 2025 12:59:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8?= =?UTF-8?q?=D0=BA=20/health/:service/:node?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index 96ac013..c7c74ff 100644 --- a/server.js +++ b/server.js @@ -5,11 +5,17 @@ const app = express() const PORT = 8080 const docker = new Docker({ socketPath: '/var/run/docker.sock' }) -async function getServiceStatusCode(service_name) { +async function getServiceStatusCode(service, node_id_or_name = null) { try { - const service = docker.getService(service_name) - await service.inspect() - const tasks = await docker.listTasks({ filters: { service: [service_name] } }) + await docker.getService(service).inspect() + + const filters = { service: [service] } + + if (node_id_or_name) { + filters.node = [node_id_or_name] + } + + const tasks = await docker.listTasks({ filters: filters }) if (tasks.length === 0) { return 503 @@ -19,16 +25,20 @@ async function getServiceStatusCode(service_name) { const state = task.Status.State const healthStatus = task.Status.ContainerStatus?.Health?.Status - if (healthStatus === 'healthy' || (state === 'running' && !healthStatus)) { + if (healthStatus === 'healthy') { return true } + if (state === 'running' && !healthStatus) { + return true + } + return false }) return isHealthy ? 200 : 503 } catch (error) { - console.error(error.message) + console.error(`Error checking service ${service} (Node: ${node_id_or_name || 'All'}): ${error.message}`) if (error.statusCode === 404) { return 404 @@ -38,6 +48,22 @@ async function getServiceStatusCode(service_name) { } } + +app.get('/health/:service/:node', async (req, res) => { + const service = req.params.service + const node = req.params.node + + if (!service || !/^[a-zA-Z0-9_-]+$/.test(service)) { + return res.sendStatus(400) + } + if (!node || !/^[a-zA-Z0-9_-]+$/.test(node)) { + return res.sendStatus(400) + } + + const statusCode = await getServiceStatusCode(service, node) + res.sendStatus(statusCode) +}) + app.get('/health/:service', async (req, res) => { const service = req.params.service @@ -45,10 +71,11 @@ app.get('/health/:service', async (req, res) => { return res.sendStatus(400) } - const statusCode = await getServiceStatusCode(service) + const statusCode = await getServiceStatusCode(service, null) res.sendStatus(statusCode) }) + app.use((_, response) => response.sendStatus(404)) const server = app.listen(PORT, () => { @@ -70,4 +97,4 @@ const shutdown_handler = (signal) => { } process.on('SIGTERM', () => shutdown_handler('SIGTERM')) -process.on('SIGINT', () => shutdown_handler('SIGINT')) \ No newline at end of file +process.on('SIGINT', () => shutdown_handler('SIGINT'))