Use healthcheck for systemd watchdog
This commit is contained in:
parent
66591cf216
commit
24368747ab
2 changed files with 31 additions and 18 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -33,22 +34,7 @@ func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pingEntryPoint, ok := traefikConfiguration.EntryPoints[traefikConfiguration.Ping.EntryPoint]
|
resp, errPing := healthCheck(traefikConfiguration.GlobalConfiguration)
|
||||||
if !ok {
|
|
||||||
pingEntryPoint = &configuration.EntryPoint{Address: ":8080"}
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{Timeout: 5 * time.Second}
|
|
||||||
protocol := "http"
|
|
||||||
if pingEntryPoint.TLS != nil {
|
|
||||||
protocol = "https"
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}
|
|
||||||
client.Transport = tr
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, errPing := client.Head(protocol + "://" + pingEntryPoint.Address + traefikConfiguration.Web.Path + "ping")
|
|
||||||
if errPing != nil {
|
if errPing != nil {
|
||||||
fmt.Printf("Error calling healthcheck: %s\n", errPing)
|
fmt.Printf("Error calling healthcheck: %s\n", errPing)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -62,3 +48,22 @@ func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func healthCheck(globalConfiguration configuration.GlobalConfiguration) (*http.Response, error) {
|
||||||
|
pingEntryPoint, ok := globalConfiguration.EntryPoints[globalConfiguration.Ping.EntryPoint]
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("missing ping entrypoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
protocol := "http"
|
||||||
|
if pingEntryPoint.TLS != nil {
|
||||||
|
protocol = "https"
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
client.Transport = tr
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.Head(protocol + "://" + pingEntryPoint.Address + globalConfiguration.Web.Path + "ping")
|
||||||
|
}
|
||||||
|
|
|
@ -153,10 +153,12 @@ func run(globalConfiguration *configuration.GlobalConfiguration, configFile stri
|
||||||
svr := server.NewServer(*globalConfiguration)
|
svr := server.NewServer(*globalConfiguration)
|
||||||
svr.Start()
|
svr.Start()
|
||||||
defer svr.Close()
|
defer svr.Close()
|
||||||
|
|
||||||
sent, err := daemon.SdNotify(false, "READY=1")
|
sent, err := daemon.SdNotify(false, "READY=1")
|
||||||
if !sent && err != nil {
|
if !sent && err != nil {
|
||||||
log.Error("Fail to notify", err)
|
log.Error("Fail to notify", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := daemon.SdWatchdogEnabled(false)
|
t, err := daemon.SdWatchdogEnabled(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Problem with watchdog", err)
|
log.Error("Problem with watchdog", err)
|
||||||
|
@ -167,12 +169,18 @@ func run(globalConfiguration *configuration.GlobalConfiguration, configFile stri
|
||||||
safe.Go(func() {
|
safe.Go(func() {
|
||||||
tick := time.Tick(t)
|
tick := time.Tick(t)
|
||||||
for range tick {
|
for range tick {
|
||||||
|
_, errHealthCheck := healthCheck(*globalConfiguration)
|
||||||
|
if globalConfiguration.Ping == nil || errHealthCheck == nil {
|
||||||
if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok {
|
if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok {
|
||||||
log.Error("Fail to tick watchdog")
|
log.Error("Fail to tick watchdog")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Error(errHealthCheck)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
svr.Wait()
|
svr.Wait()
|
||||||
log.Info("Shutting down")
|
log.Info("Shutting down")
|
||||||
logrus.Exit(0)
|
logrus.Exit(0)
|
||||||
|
|
Loading…
Reference in a new issue