-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdnotify.go
96 lines (82 loc) · 2.84 KB
/
sdnotify.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Package sdnotify implements systemd readiness notifications as described in
// https://www.freedesktop.org/software/systemd/man/sd_notify.html.
package sdnotify
import (
"fmt"
"io"
"net"
"os"
"strings"
)
// Socket is the predefined systemd notification socket environment variable.
const Socket = "NOTIFY_SOCKET"
// Common notification values. For a description of each, see:
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description.
const (
Ready = "READY=1"
Reloading = "RELOADING=1"
Stopping = "STOPPING=1"
)
// Statusf creates a formatted STATUS notification with the input format string
// and values.
func Statusf(format string, v ...interface{}) string {
return fmt.Sprintf("STATUS=%s", fmt.Sprintf(format, v...))
}
// A Notifier can notify systemd of service status and readiness. Any methods
// called on a nil Notifier will result in a no-op, allowing graceful
// functionality degradation when a Go program is not running under systemd
// supervision.
type Notifier struct{ wc io.WriteCloser }
// New creates a Notifier which sends notifications to the UNIX socket specified
// by the NOTIFY_SOCKET environment variable. See Open for more details.
func New() (*Notifier, error) {
s := os.Getenv(Socket)
if s == "" {
// Don't bother stat'ing an empty socket, just return now.
return nil, os.ErrNotExist
}
return Open(s)
}
// Open creates a Notifier which sends notifications to the UNIX socket
// specified by sock.
//
// If sock does not exist or is unset (meaning the service is not running under
// systemd supervision, or is not using systemd unit Type=notify), Open will
// return an error which can be checked with 'errors.Is(err, os.ErrNotExist)'.
// Calling any of the resulting nil Notifier's methods will result in a no-op.
func Open(sock string) (*Notifier, error) {
// Don't stat Linux abstract namespace sockets, as would be created with a
// net.ListenPacket with no path.
if !strings.HasPrefix(sock, "@") {
if _, err := os.Stat(sock); err != nil {
return nil, fmt.Errorf("failed to stat notify socket: %w", err)
}
}
c, err := net.Dial("unixgram", sock)
if err != nil {
return nil, err
}
return &Notifier{wc: c}, nil
}
// Notify sends zero or more notifications to systemd. See the package constants
// for a list of common notifications or use the Statusf function to create a
// STATUS notification.
//
// For advanced use cases, see:
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description.
//
// If n is nil or no strings are specified, Notify is a no-op.
func (n *Notifier) Notify(s ...string) error {
if n == nil || len(s) == 0 {
return nil
}
_, err := io.WriteString(n.wc, strings.Join(s, "\n"))
return err
}
// Close closes the Notifier's socket. If n is nil, Close is a no-op.
func (n *Notifier) Close() error {
if n == nil {
return nil
}
return n.wc.Close()
}