mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package monitor
|
|
|
|
import (
|
|
"github.com/yusing/go-proxy/internal/docker"
|
|
|
|
dockerTypes "github.com/docker/docker/api/types"
|
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
|
)
|
|
|
|
type DockerHealthMonitor struct {
|
|
*monitor
|
|
client *docker.SharedClient
|
|
containerID string
|
|
fallback health.HealthChecker
|
|
}
|
|
|
|
func NewDockerHealthMonitor(client *docker.SharedClient, containerID string, config *health.HealthCheckConfig, fallback health.HealthChecker) *DockerHealthMonitor {
|
|
mon := new(DockerHealthMonitor)
|
|
mon.client = client
|
|
mon.containerID = containerID
|
|
mon.monitor = newMonitor(fallback.URL(), config, mon.CheckHealth)
|
|
mon.fallback = fallback
|
|
return mon
|
|
}
|
|
|
|
func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
|
|
cont, err := mon.client.ContainerInspect(mon.task.Context(), mon.containerID)
|
|
if err != nil {
|
|
return mon.fallback.CheckHealth()
|
|
}
|
|
if cont.State.Health == nil {
|
|
return mon.fallback.CheckHealth()
|
|
}
|
|
result = new(health.HealthCheckResult)
|
|
result.Healthy = cont.State.Health.Status == dockerTypes.Healthy
|
|
if len(cont.State.Health.Log) > 0 {
|
|
lastLog := cont.State.Health.Log[len(cont.State.Health.Log)-1]
|
|
result.Detail = lastLog.Output
|
|
result.Latency = lastLog.End.Sub(lastLog.Start)
|
|
}
|
|
return
|
|
}
|