mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
Systemd readiness notification
This change implements the systemd readiness notification, using the sd_notify protocol. See https://www.freedesktop.org/software/systemd/man/sd_notify.html.
This commit is contained in:
parent
1d80c12e18
commit
89c1b3b4d8
4 changed files with 51 additions and 5 deletions
|
@ -44,6 +44,11 @@ func startDaemon(store *storage.Storage) {
|
|||
go collector.GatherStorageMetrics()
|
||||
}
|
||||
|
||||
// Notify systemd that we are ready.
|
||||
if err := sdNotify(sdNotifyReady); err != nil {
|
||||
logger.Error("Unable to send readiness notification to systemd: %v", err)
|
||||
}
|
||||
|
||||
<-stop
|
||||
logger.Info("Shutting down the process...")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
|
|
42
cli/sd_notify.go
Normal file
42
cli/sd_notify.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2021 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package cli // import "miniflux.app/cli"
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
// sdNotifyReady tells the service manager that service startup is
|
||||
// finished, or the service finished loading its configuration.
|
||||
sdNotifyReady = "READY=1"
|
||||
)
|
||||
|
||||
// sdNotify sends a message to systemd using the sd_notify protocol.
|
||||
// See https://www.freedesktop.org/software/systemd/man/sd_notify.html.
|
||||
func sdNotify(state string) error {
|
||||
addr := &net.UnixAddr{
|
||||
Net: "unixgram",
|
||||
Name: os.Getenv("NOTIFY_SOCKET"),
|
||||
}
|
||||
|
||||
if addr.Name == "" {
|
||||
// We're not running under systemd (NOTIFY_SOCKET has not set).
|
||||
return nil
|
||||
}
|
||||
|
||||
conn, err := net.DialUnix(addr.Net, nil, addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if _, err = conn.Write([]byte(state)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue