2023-09-04 17:32:04 +02:00
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
2023-11-29 10:25:30 +01:00
|
|
|
"fmt"
|
2023-09-04 17:32:04 +02:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
2023-11-29 10:25:30 +01:00
|
|
|
"github.com/coreos/go-systemd/v22/daemon"
|
2023-09-04 17:32:04 +02:00
|
|
|
"github.com/rclone/rclone/lib/atexit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notify systemd that the service is starting. This returns a
|
|
|
|
// function which should be called to notify that the service is
|
|
|
|
// stopping. This function will be called on exit if the service exits
|
|
|
|
// on a signal.
|
|
|
|
func Notify() func() {
|
2023-11-29 10:25:30 +01:00
|
|
|
if _, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
|
2023-09-04 17:32:04 +02:00
|
|
|
log.Printf("failed to notify ready to systemd: %v", err)
|
|
|
|
}
|
|
|
|
var finaliseOnce sync.Once
|
|
|
|
finalise := func() {
|
|
|
|
finaliseOnce.Do(func() {
|
2023-11-29 10:25:30 +01:00
|
|
|
if _, err := daemon.SdNotify(false, daemon.SdNotifyStopping); err != nil {
|
2023-09-04 17:32:04 +02:00
|
|
|
log.Printf("failed to notify stopping to systemd: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
finaliseHandle := atexit.Register(finalise)
|
|
|
|
return func() {
|
|
|
|
atexit.Unregister(finaliseHandle)
|
|
|
|
finalise()
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 10:25:30 +01:00
|
|
|
|
|
|
|
// UpdateStatus updates the systemd status
|
|
|
|
func UpdateStatus(status string) error {
|
|
|
|
systemdStatus := fmt.Sprintf("STATUS=%s", status)
|
|
|
|
_, err := daemon.SdNotify(false, systemdStatus)
|
|
|
|
return err
|
|
|
|
}
|