2020-01-07 14:03:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-22 23:04:20 +00:00
|
|
|
"os"
|
|
|
|
|
2020-02-12 19:44:35 +00:00
|
|
|
"github.com/coreos/go-systemd/v22/dbus"
|
2020-01-07 14:03:58 +00:00
|
|
|
)
|
|
|
|
|
2020-01-22 23:04:20 +00:00
|
|
|
func notifyHandler(n string, ch chan interface{}, sCh chan os.Signal) (string, error) {
|
2020-01-07 14:03:58 +00:00
|
|
|
select {
|
2020-01-22 23:04:20 +00:00
|
|
|
// alerts to restart systemd unit
|
2020-01-07 14:03:58 +00:00
|
|
|
case <-ch:
|
|
|
|
statusCh := make(chan string, 1)
|
|
|
|
c, err := dbus.New()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = c.TryRestartUnit(n, "fail", statusCh)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case result := <-statusCh:
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-01-22 23:04:20 +00:00
|
|
|
// SIGTERM
|
|
|
|
case <-sCh:
|
|
|
|
os.Exit(1)
|
|
|
|
return "", nil
|
2020-01-07 14:03:58 +00:00
|
|
|
}
|
|
|
|
}
|