-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchdog.go
46 lines (36 loc) · 1.02 KB
/
watchdog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package common
import (
"context"
"errors"
"time"
"github.com/na4ma4/config"
"github.com/okzk/sdnotify"
"go.uber.org/zap"
)
// ErrWatchdogFailed is returned when the systemd watchdog tick fails.
var ErrWatchdogFailed = errors.New("systemd watchdog failed")
// ProcessWatchdog sends systemd watchdog heartbeats to the systemd process.
func ProcessWatchdog(ctx context.Context, cancel context.CancelFunc, cfg config.Conf, logger *zap.Logger) func() error {
if cfg.GetBool("watchdog.enabled") {
logger.Info("starting watchdog")
ticker := time.NewTicker(cfg.GetDuration("watchdog.tick"))
return func() error {
for {
select {
case <-ticker.C:
logger.Debug("watchdog tick sent")
if err := sdnotify.Watchdog(); err != nil {
logger.Error("systemd watchdog returned error", zap.Error(err))
cancel()
return ErrWatchdogFailed
}
case <-ctx.Done():
logger.Debug("ProcessWatchdog Done()")
ticker.Stop()
return nil
}
}
}
}
return func() error { return nil }
}