Add support for systemd sd_notify

This commit is contained in:
2021-11-06 01:29:29 +01:00
parent f213d9cc29
commit 801dc967f8
5 changed files with 56 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import (
"os/signal"
"time"
"github.com/coreos/go-systemd/daemon"
sshlib "github.com/gliderlabs/ssh"
"github.com/urfave/cli/v2"
"github.uio.no/torjus/apiary"
@@ -165,6 +166,56 @@ func ActionServe(c *cli.Context) error {
}
}()
// If run by systemd, enable watchdog and notify ready
go func() {
notifyCtx, cancel := context.WithCancel(rootCtx)
defer cancel()
_, ok := os.LookupEnv("NOTIFY_SOCKET")
if !ok {
return
}
loggers.rootLogger.Info("Systemd notify socket detected. Sending ready and enabling watchdog.")
ok, err := daemon.SdNotify(false, daemon.SdNotifyReady)
if !ok {
loggers.rootLogger.Info("Systemd notify not enabled.")
return
}
if err != nil {
loggers.rootLogger.Warnw("Unable to connect to NOTIFY_SOCKET.", "error", err)
return
}
loggers.rootLogger.Debug("Sent READY=1 to NOTIFY_SOCKET.")
if _, err := daemon.SdNotify(false, "WATCHDOG_USEC=10000000"); err != nil {
loggers.rootLogger.Warnw("Unable to connect to NOTIFY_SOCKET to set watchdog timeout.", "error", err)
return
}
loggers.rootLogger.Debug("Sent WATCHDOG_USEC=10000000 to NOTIFY_SOCKET.")
if _, err := daemon.SdNotify(false, "WATCHDOG_USEC=10000000"); err != nil {
loggers.rootLogger.Warnw("Unable to connect to NOTIFY_SOCKET to set watchdog timeout.", "error", err)
return
}
timeout, err := daemon.SdWatchdogEnabled(false)
if err != nil {
loggers.rootLogger.Warnw("Unable to connect to NOTIFY_SOCKET to get watchdog timeout.", "error", err)
return
}
ticker := time.NewTicker(timeout / 2)
for {
healthy := s.IsHealthy()
select {
case <-ticker.C:
if healthy == nil {
daemon.SdNotify(false, daemon.SdNotifyWatchdog)
}
case <-notifyCtx.Done():
loggers.rootLogger.Debugw("Notify context cancelled.")
return
}
}
}()
go func() {
<-serversCtx.Done()