@marcel91200 / caddy Mirror · read-only / commits / 45fb7202
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

notify: Send all sd_notify signals from main caddy process (#4060)

Initial sd_notify support was added in #3963, but that sent signals from both cmdRun and cmdReload. This approach has two drawbacks: - Reloads initiated via the API do not send signals. - The signals are sent from different processes, which requires the `NotifyAccess=exec` directive in the unit file. This change moves the NotifyReloading and NotifyReadiness invocations to Load, which address both of those drawbacks. It also adds a complimentary NotifyStopping method which is invoked from handleStop. All the notify methods are defined in a notify package to avoid an import loop.

Carl George committed Apr 5, 2021 at 15:01 UTC 45fb7202ac0e606ccb7b4fe95f169424f0a6cabc
6 files changed +43 -18
admin.go
+6
@@ -39,6 +39,7 @@ import (
39 "sync"
40 "time"
41
42 + "github.com/caddyserver/caddy/v2/notify"
43 "github.com/caddyserver/certmagic"
44 "github.com/prometheus/client_golang/prometheus"
45 "go.uber.org/zap"
@@ -905,6 +906,11 @@ func handleStop(w http.ResponseWriter, r *http.Request) error {
906 Err: fmt.Errorf("method not allowed"),
907 }
908 }
909 +
910 + if err := notify.NotifyStopping(); err != nil {
911 + Log().Error("unable to notify stopping to service manager", zap.Error(err))
912 + }
913 +
914 exitProcess(Log().Named("admin.api"))
915 return nil
916 }
caddy.go
+11
@@ -32,6 +32,7 @@ import (
32 "sync"
33 "time"
34
35 + "github.com/caddyserver/caddy/v2/notify"
36 "github.com/caddyserver/certmagic"
37 "github.com/google/uuid"
38 "go.uber.org/zap"
@@ -100,6 +101,16 @@ func Run(cfg *Config) error {
101 // if it is different from the current config or
102 // forceReload is true.
103 func Load(cfgJSON []byte, forceReload bool) error {
104 + if err := notify.NotifyReloading(); err != nil {
105 + Log().Error("unable to notify reloading to service manager", zap.Error(err))
106 + }
107 +
108 + defer func() {
109 + if err := notify.NotifyReadiness(); err != nil {
110 + Log().Error("unable to notify readiness to service manager", zap.Error(err))
111 + }
112 + }()
113 +
114 return changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, forceReload)
115 }
116
cmd/commandfuncs.go
-13
@@ -269,10 +269,6 @@ func cmdRun(fl Flags) (int, error) {
269 }
270 }
271
272 - if err := NotifyReadiness(); err != nil {
273 - caddy.Log().Error("unable to notify readiness to service manager", zap.Error(err))
274 - }
275 -
272 select {}
273 }
274
@@ -294,15 +290,6 @@ func cmdReload(fl Flags) (int, error) {
290 reloadCmdAddrFlag := fl.String("address")
291 reloadCmdForceFlag := fl.Bool("force")
292
297 - if err := NotifyReloading(); err != nil {
298 - caddy.Log().Error("unable to notify reloading to service manager", zap.Error(err))
299 - }
300 - defer func() {
301 - if err := NotifyReadiness(); err != nil {
302 - caddy.Log().Error("unable to notify readiness to service manager", zap.Error(err))
303 - }
304 - }()
305 -
293 // get the config in caddy's native format
294 config, configFile, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
295 if err != nil {
notify/notify.go renamed
+6 -1
@@ -12,7 +12,7 @@
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 -package caddycmd
15 +package notify
16
17 // NotifyReadiness notifies process manager of readiness.
18 func NotifyReadiness() error {
@@ -23,3 +23,8 @@ func NotifyReadiness() error {
23 func NotifyReloading() error {
24 return notifyReloading()
25 }
26 +
27 +// NotifyStopping notifies process manager of stopping.
28 +func NotifyStopping() error {
29 + return notifyStopping()
30 +}
notify/notify_linux.go renamed
+15 -3
@@ -12,7 +12,7 @@
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 -package caddycmd
15 +package notify
16
17 import (
18 "io"
@@ -42,7 +42,7 @@ func sdNotify(path, payload string) error {
42 return nil
43 }
44
45 -// notifyReadiness notifies systemd caddy that has finished its
45 +// notifyReadiness notifies systemd that caddy has finished its
46 // initialization routines.
47 func notifyReadiness() error {
48 val, ok := os.LookupEnv("NOTIFY_SOCKET")
@@ -55,7 +55,7 @@ func notifyReadiness() error {
55 return nil
56 }
57
58 -// notifyReadiness notifies systemd that caddy is reloading its config.
58 +// notifyReloading notifies systemd that caddy is reloading its config.
59 func notifyReloading() error {
60 val, ok := os.LookupEnv("NOTIFY_SOCKET")
61 if !ok || val == "" {
@@ -66,3 +66,15 @@ func notifyReloading() error {
66 }
67 return nil
68 }
69 +
70 +// notifyStopping notifies systemd that caddy is stopping.
71 +func notifyStopping() error {
72 + val, ok := os.LookupEnv("NOTIFY_SOCKET")
73 + if !ok || val == "" {
74 + return nil
75 + }
76 + if err := sdNotify(val, "STOPPING=1"); err != nil {
77 + return err
78 + }
79 + return nil
80 +}
notify/notify_other.go renamed
+5 -1
@@ -14,7 +14,7 @@
14
15 // +build !linux
16
17 -package caddycmd
17 +package notify
18
19 func notifyReadiness() error {
20 return nil
@@ -23,3 +23,7 @@ func notifyReadiness() error {
23 func notifyReloading() error {
24 return nil
25 }
26 +
27 +func notifyStopping() error {
28 + return nil
29 +}