notify_linux.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "net"
  4. "os"
  5. "time"
  6. "github.com/sirupsen/logrus"
  7. )
  8. // SdNotifyReady tells systemd the service is ready and dependent services can now be started
  9. // https://www.freedesktop.org/software/systemd/man/sd_notify.html
  10. // https://www.freedesktop.org/software/systemd/man/systemd.service.html
  11. const SdNotifyReady = "READY=1"
  12. func notifyReady(l *logrus.Logger) {
  13. sockName := os.Getenv("NOTIFY_SOCKET")
  14. if sockName == "" {
  15. l.Debugln("NOTIFY_SOCKET systemd env var not set, not sending ready signal")
  16. return
  17. }
  18. conn, err := net.DialTimeout("unixgram", sockName, time.Second)
  19. if err != nil {
  20. l.WithError(err).Error("failed to connect to systemd notification socket")
  21. return
  22. }
  23. defer conn.Close()
  24. err = conn.SetWriteDeadline(time.Now().Add(time.Second))
  25. if err != nil {
  26. l.WithError(err).Error("failed to set the write deadline for the systemd notification socket")
  27. return
  28. }
  29. if _, err = conn.Write([]byte(SdNotifyReady)); err != nil {
  30. l.WithError(err).Error("failed to signal the systemd notification socket")
  31. return
  32. }
  33. l.Debugln("notified systemd the service is ready")
  34. }