1
mirror of https://github.com/rclone/rclone synced 2024-12-22 13:03:02 +01:00
rclone/lib/systemd/notify.go
albertony bcdfad3c83 build: update logging statements to make json log work - fixes #6038
This changes log statements from log to fs package, which is required for --use-json-log
to properly make log output in JSON format. The recently added custom linting rule,
handled by ruleguard via gocritic via golangci-lint, warns about these and suggests
the alternative. Fixing was therefore basically running "golangci-lint run --fix",
although some manual fixup of mainly imports are necessary following that.
2024-09-06 17:04:18 +01:00

44 lines
1.2 KiB
Go

package systemd
import (
"fmt"
"sync"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/atexit"
)
// Notify systemd that the service is ready. 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.
// NOTE: this function should only be called once, and so it
// should generally only be used directly in a command's Run handler.
// It should not be called as a result of rc commands. See #7540.
func Notify() func() {
if _, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
fs.Logf(nil, "failed to notify ready to systemd: %v", err)
}
var finaliseOnce sync.Once
finalise := func() {
finaliseOnce.Do(func() {
if _, err := daemon.SdNotify(false, daemon.SdNotifyStopping); err != nil {
fs.Logf(nil, "failed to notify stopping to systemd: %v", err)
}
})
}
finaliseHandle := atexit.Register(finalise)
return func() {
atexit.Unregister(finaliseHandle)
finalise()
}
}
// UpdateStatus updates the systemd status
func UpdateStatus(status string) error {
systemdStatus := fmt.Sprintf("STATUS=%s", status)
_, err := daemon.SdNotify(false, systemdStatus)
return err
}