-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to notify systemd of service readiness on linux (#929)
- Loading branch information
Showing
6 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ func main() { | |
|
||
if !*configTest { | ||
ctrl.Start() | ||
notifyReady(l) | ||
ctrl.ShutdownBlock() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// SdNotifyReady tells systemd the service is ready and dependent services can now be started | ||
// https://www.freedesktop.org/software/systemd/man/sd_notify.html | ||
// https://www.freedesktop.org/software/systemd/man/systemd.service.html | ||
const SdNotifyReady = "READY=1" | ||
|
||
func notifyReady(l *logrus.Logger) { | ||
sockName := os.Getenv("NOTIFY_SOCKET") | ||
if sockName == "" { | ||
l.Debugln("NOTIFY_SOCKET systemd env var not set, not sending ready signal") | ||
return | ||
} | ||
|
||
conn, err := net.DialTimeout("unixgram", sockName, time.Second) | ||
if err != nil { | ||
l.WithError(err).Error("failed to connect to systemd notification socket") | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
err = conn.SetWriteDeadline(time.Now().Add(time.Second)) | ||
if err != nil { | ||
l.WithError(err).Error("failed to set the write deadline for the systemd notification socket") | ||
return | ||
} | ||
|
||
if _, err = conn.Write([]byte(SdNotifyReady)); err != nil { | ||
l.WithError(err).Error("failed to signal the systemd notification socket") | ||
return | ||
} | ||
|
||
l.Debugln("notified systemd the service is ready") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package main | ||
|
||
import "github.com/sirupsen/logrus" | ||
|
||
func notifyReady(_ *logrus.Logger) { | ||
// No init service to notify | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters