You've already forked healthcheck
Инициализация
All checks were successful
Build and push Docker image / build (push) Successful in 1m2s
All checks were successful
Build and push Docker image / build (push) Successful in 1m2s
This commit is contained in:
53
server.js
Normal file
53
server.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import express from 'express'
|
||||
import Docker from 'dockerode'
|
||||
|
||||
const app = express()
|
||||
const PORT = 8080
|
||||
const docker = new Docker({ socketPath: '/var/run/docker.sock' })
|
||||
|
||||
async function getServiceStatusCode(serviceName) {
|
||||
try {
|
||||
const service = docker.getService(serviceName)
|
||||
const tasks = await service.tasks({ filters: { service: [serviceName] } })
|
||||
|
||||
if (tasks.length === 0) {
|
||||
return 503
|
||||
}
|
||||
|
||||
const isHealthy = tasks.some(task => {
|
||||
const state = task.Status.State
|
||||
const healthStatus = task.Status.ContainerStatus?.Health?.Status
|
||||
|
||||
if (healthStatus === 'healthy' || (state === 'running' && !healthStatus)) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
return isHealthy ? 200 : 503
|
||||
} catch (error) {
|
||||
if (error.statusCode === 404) {
|
||||
return 404
|
||||
}
|
||||
|
||||
return 503
|
||||
}
|
||||
}
|
||||
|
||||
app.get('/health/:service', async (req, res) => {
|
||||
const service = req.params.service
|
||||
|
||||
if (!service || !/^[a-zA-Z0-9_-]+$/.test(service)) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
const statusCode = await getServiceStatusCode(service)
|
||||
res.sendStatus(statusCode)
|
||||
})
|
||||
|
||||
app.use((_, response) => response.sendStatus(404))
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Health Check Proxy listening on port ${PORT}`)
|
||||
})
|
||||
Reference in New Issue
Block a user