systemd notify and watchdog (closes #12)

This commit is contained in:
boyska 2019-01-07 10:06:14 +01:00
parent eff3998eb7
commit 0121ba64b5

View file

@ -9,8 +9,10 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"git.lattuga.net/boyska/circolog" "git.lattuga.net/boyska/circolog"
"github.com/coreos/go-systemd/daemon"
syslog "gopkg.in/mcuadros/go-syslog.v2" 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 { for {
select { select {
case <-wdTick:
daemon.SdNotify(false, daemon.SdNotifyWatchdog)
case sig := <-interrupt: case sig := <-interrupt:
if sig == syscall.SIGUSR1 { if sig == syscall.SIGUSR1 {
response := make(chan circolog.CommandResponse) response := make(chan circolog.CommandResponse)
@ -130,6 +139,7 @@ func main() {
} }
if sig == syscall.SIGTERM || sig == syscall.SIGINT { if sig == syscall.SIGTERM || sig == syscall.SIGINT {
log.Println("Quitting because of signal", sig) log.Println("Quitting because of signal", sig)
daemon.SdNotify(false, daemon.SdNotifyStopping)
server.Kill() server.Kill()
if err := httpQueryServer.Shutdown(nil); err != nil { if err := httpQueryServer.Shutdown(nil); err != nil {
fmt.Fprintln(os.Stderr, "Error closing http server:", err) fmt.Fprintln(os.Stderr, "Error closing http server:", err)