From 0121ba64b5ebfac3d7fe05c908dd9319b8866dbe Mon Sep 17 00:00:00 2001 From: boyska Date: Mon, 7 Jan 2019 10:06:14 +0100 Subject: [PATCH] systemd notify and watchdog (closes #12) --- cmd/circologd/main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/circologd/main.go b/cmd/circologd/main.go index cf0a202..41fb95b 100644 --- a/cmd/circologd/main.go +++ b/cmd/circologd/main.go @@ -9,8 +9,10 @@ import ( "os" "os/signal" "syscall" + "time" "git.lattuga.net/boyska/circolog" + "github.com/coreos/go-systemd/daemon" syslog "gopkg.in/mcuadros/go-syslog.v2" ) @@ -103,10 +105,17 @@ func main() { }() } - // TODO: now we are ready + daemon.SdNotify(false, daemon.SdNotifyReady) + var wdTick <-chan time.Time + if watchdogTime, err := daemon.SdWatchdogEnabled(false); err == nil && watchdogTime != 0 { + fmt.Println("systemd watchdog enabled") + wdTick = time.Tick(watchdogTime / 2) // much less than systemd default of 30s; TODO: make it configurable + } for { select { + case <-wdTick: + daemon.SdNotify(false, daemon.SdNotifyWatchdog) case sig := <-interrupt: if sig == syscall.SIGUSR1 { response := make(chan circolog.CommandResponse) @@ -130,6 +139,7 @@ func main() { } if sig == syscall.SIGTERM || sig == syscall.SIGINT { log.Println("Quitting because of signal", sig) + daemon.SdNotify(false, daemon.SdNotifyStopping) server.Kill() if err := httpQueryServer.Shutdown(nil); err != nil { fmt.Fprintln(os.Stderr, "Error closing http server:", err)