Skip to content

Commit 0c08302

Browse files
committed
refactor healtchcheck
1 parent 2150856 commit 0c08302

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

healthcheck/watchdog.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,33 @@ func StartWatchDog(mainCtx context.Context, pingers []Pinger) (err error) {
6161
return
6262
}
6363
ticker := time.NewTicker(interval / 2)
64-
go func() {
65-
<-mainCtx.Done()
66-
ticker.Stop()
67-
}()
68-
for t := range ticker.C {
69-
ctx, cancel := context.WithDeadline(mainCtx, t.Add(interval/2))
70-
ok = true
71-
for i := range pingers {
72-
err = pingers[i].Ping(ctx)
73-
if err != nil {
74-
log.Error().Err(err).Msgf("error pinging %v: %s", i, err)
75-
ok = false
64+
for {
65+
select {
66+
case <-mainCtx.Done():
67+
ticker.Stop()
68+
return nil
69+
case t := <-ticker.C:
70+
ctx, cancel := context.WithDeadline(mainCtx, t.Add(interval/2))
71+
ok = true
72+
for i := range pingers {
73+
err = pingers[i].Ping(ctx)
74+
if err != nil {
75+
log.Error().Err(err).Msgf("error pinging %v: %s", i, err)
76+
ok = false
77+
}
7678
}
77-
}
78-
cancel()
79-
if ok {
80-
_, err = daemon.SdNotify(false, daemon.SdNotifyWatchdog)
81-
if err != nil {
82-
log.Error().Err(err).Msgf("error sending watchdog notification: %s", err)
83-
continue
79+
cancel()
80+
81+
if ok {
82+
_, err = daemon.SdNotify(false, daemon.SdNotifyWatchdog)
83+
if err != nil {
84+
log.Error().Err(err).Msgf("error sending watchdog notification: %s", err)
85+
continue
86+
}
87+
log.Trace().Msgf("Service is healthy!")
88+
} else {
89+
log.Warn().Msgf("Service is broken!")
8490
}
85-
log.Trace().Msgf("Service is healthy!")
86-
} else {
87-
log.Warn().Msgf("Service is broken!")
8891
}
8992
}
90-
return nil
9193
}

0 commit comments

Comments
 (0)