Skip to content

Commit f9d889f

Browse files
committed
Send all sd_notify signals from main caddy process
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.
1 parent 802f80c commit f9d889f

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

admin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"sync"
4040
"time"
4141

42+
caddycmd "github.com/caddyserver/caddy/v2/cmd"
4243
"github.com/caddyserver/certmagic"
4344
"github.com/prometheus/client_golang/prometheus"
4445
"go.uber.org/zap"
@@ -905,6 +906,11 @@ func handleStop(w http.ResponseWriter, r *http.Request) error {
905906
Err: fmt.Errorf("method not allowed"),
906907
}
907908
}
909+
910+
if err := caddycmd.NotifyStopping(); err != nil {
911+
Log().Error("unable to notify stopping to service manager", zap.Error(err))
912+
}
913+
908914
exitProcess(Log().Named("admin.api"))
909915
return nil
910916
}

caddy.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sync"
3333
"time"
3434

35+
caddycmd "github.com/caddyserver/caddy/v2/cmd"
3536
"github.com/caddyserver/certmagic"
3637
"go.uber.org/zap"
3738
)
@@ -99,6 +100,16 @@ func Run(cfg *Config) error {
99100
// if it is different from the current config or
100101
// forceReload is true.
101102
func Load(cfgJSON []byte, forceReload bool) error {
103+
if err := caddycmd.NotifyReloading(); err != nil {
104+
Log().Error("unable to notify reloading to service manager", zap.Error(err))
105+
}
106+
107+
defer func() {
108+
if err := caddycmd.NotifyReadiness(); err != nil {
109+
Log().Error("unable to notify readiness to service manager", zap.Error(err))
110+
}
111+
}()
112+
102113
return changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, forceReload)
103114
}
104115

cmd/commandfuncs.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ func cmdRun(fl Flags) (int, error) {
269269
}
270270
}
271271

272-
if err := NotifyReadiness(); err != nil {
273-
caddy.Log().Error("unable to notify readiness to service manager", zap.Error(err))
274-
}
275-
276272
select {}
277273
}
278274

@@ -294,15 +290,6 @@ func cmdReload(fl Flags) (int, error) {
294290
reloadCmdAddrFlag := fl.String("address")
295291
reloadCmdForceFlag := fl.Bool("force")
296292

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-
306293
// get the config in caddy's native format
307294
config, configFile, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
308295
if err != nil {

cmd/notify.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ func NotifyReadiness() error {
2323
func NotifyReloading() error {
2424
return notifyReloading()
2525
}
26+
27+
// NotifyStopping notifies process manager of stopping.
28+
func NotifyStopping() error {
29+
return notifyStopping()
30+
}

cmd/notify_linux.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func notifyReadiness() error {
5555
return nil
5656
}
5757

58-
// notifyReadiness notifies systemd that caddy is reloading its config.
58+
// notifyReloading notifies systemd that caddy is reloading its config.
5959
func notifyReloading() error {
6060
val, ok := os.LookupEnv("NOTIFY_SOCKET")
6161
if !ok || val == "" {
@@ -66,3 +66,15 @@ func notifyReloading() error {
6666
}
6767
return nil
6868
}
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+
}

cmd/notify_other.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ func notifyReadiness() error {
2323
func notifyReloading() error {
2424
return nil
2525
}
26+
27+
func notifyStopping() error {
28+
return nil
29+
}

0 commit comments

Comments
 (0)