File: notify.go

package info (click to toggle)
rclone 1.65.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 38,352 kB
  • sloc: sh: 1,056; xml: 857; python: 693; javascript: 612; makefile: 289; ansic: 101; php: 74
file content (32 lines) | stat: -rw-r--r-- 809 bytes parent folder | download
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
package systemd

import (
	"log"
	"sync"

	sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
	"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() {
	if err := sysdnotify.Ready(); err != nil {
		log.Printf("failed to notify ready to systemd: %v", err)
	}
	var finaliseOnce sync.Once
	finalise := func() {
		finaliseOnce.Do(func() {
			if err := sysdnotify.Stopping(); err != nil {
				log.Printf("failed to notify stopping to systemd: %v", err)
			}
		})
	}
	finaliseHandle := atexit.Register(finalise)
	return func() {
		atexit.Unregister(finaliseHandle)
		finalise()
	}
}