Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions internal/home/ready_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build linux

package home

import (
"fmt"
"net"
"os"
"time"
)

// Notifies the service manager that the program is ready to serve
func notifyReady() error {
return sdNotify("READY=1")
}

// Notifies the service manager that the program is beginning to reload its
// configuration
func notifyReload() error {
now := time.Now().UnixMicro()
return sdNotify(fmt.Sprintf("RELOADING=1\nMONOTONIC_USEC=%v", now))
}

// Implements the sd_notify mechanism
//
// Reference: https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
func sdNotify(message string) error {
socketPath := os.Getenv("NOTIFY_SOCKET")
if socketPath == "" {
return nil
}
socketAddr := net.UnixAddr{
Name: socketPath,
Net: "unixgram",
}

conn, err := net.DialUnix("unixgram", nil, &socketAddr)
if err != nil {
return fmt.Errorf("connecting to %q: %w", socketAddr.String(), err)
}
defer conn.Close()

_, err = conn.Write([]byte(message))
if err != nil {
return fmt.Errorf("sending %q: %w", message, err)
}

return nil
}
14 changes: 14 additions & 0 deletions internal/home/ready_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !linux

package home

// Notifies the service manager that the program is ready to serve
func notifyReady() error {
return nil
}

// Notifies the service manager that the program is beginning to reload its
// configuration
func notifyReload() error {
return nil
}
4 changes: 4 additions & 0 deletions internal/home/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,17 @@ var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
// 2. The StandardOutput and StandardError settings are set to redirect the
// output to the systemd journal, see
// https://man7.org/linux/man-pages/man5/systemd.exec.5.html#LOGGING_AND_STANDARD_INPUT/OUTPUT.
//
// 3. The Type setting has been configured to enable service readiness
// notification support.
const systemdScript = `[Unit]
Description={{.Description}}
ConditionFileIsExecutable={{.Path|cmdEscape}}
{{range $i, $dep := .Dependencies}}
{{$dep}} {{end}}

[Service]
Type=notify
StartLimitInterval=5
StartLimitBurst=10
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
Expand Down
14 changes: 13 additions & 1 deletion internal/home/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,26 @@ func (web *webAPI) start(ctx context.Context) {
errs <- web.httpServer.ListenAndServe()
}()

err := <-errs
// Tell the service manager that we are ready to serve requests
err := notifyReady()
if err != nil {
logger.ErrorContext(ctx, "sending service ready notification: %v", slogutil.KeyError, err)
}

err = <-errs
if !errors.Is(err, http.ErrServerClosed) {
cleanupAlways()
panic(err)
}

// We use ErrServerClosed as a sign that we need to rebind on a new
// address, so go back to the start of the loop.
//
// Let the service manager know that we are reloading
err = notifyReload()
if err != nil {
logger.ErrorContext(ctx, "sending service reload notification: %v", slogutil.KeyError, err)
}
}
}

Expand Down