File: notify.go

package info (click to toggle)
rclone 1.69.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 45,716 kB
  • sloc: sh: 1,115; xml: 857; python: 754; javascript: 612; makefile: 299; ansic: 101; php: 74
file content (43 lines) | stat: -rw-r--r-- 1,276 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}