diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-07-12 10:07:11 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-07-12 10:07:11 -0600 |
commit | b780f0f49b191a6724b7ec54aa62a97d23977231 (patch) | |
tree | 984a9f1f816fd9a092f0bba63a38c3099c43b7b1 /caddy.go | |
parent | 2141626269201d902b736711a808098e6d175cbb (diff) |
Standardize exit codes and improve shutdown handling; update gitignore
Diffstat (limited to 'caddy.go')
-rw-r--r-- | caddy.go | 46 |
1 files changed, 36 insertions, 10 deletions
@@ -150,21 +150,46 @@ func Run(newCfg *Config) error { currentCfg = newCfg // Stop, Cleanup each old app - if oldCfg != nil { - for name, a := range oldCfg.apps { - err := a.Stop() - if err != nil { - log.Printf("[ERROR] stop %s: %v", name, err) - } - } + unsyncedStop(oldCfg) - // clean up all old modules - oldCfg.cancelFunc() - } + return nil +} +// Stop stops running the current configuration. +// It is the antithesis of Run(). This function +// will log any errors that occur during the +// stopping of individual apps and continue to +// stop the others. +func Stop() error { + currentCfgMu.Lock() + defer currentCfgMu.Unlock() + unsyncedStop(currentCfg) + currentCfg = nil return nil } +// unsyncedStop stops oldCfg from running, but if +// applicable, you need to acquire locks yourself. +// It is a no-op if oldCfg is nil. If any app +// returns an error when stopping, it is logged +// and the function continues with the next app. +func unsyncedStop(oldCfg *Config) { + if oldCfg == nil { + return + } + + // stop each app + for name, a := range oldCfg.apps { + err := a.Stop() + if err != nil { + log.Printf("[ERROR] stop %s: %v", name, err) + } + } + + // clean up all old modules + oldCfg.cancelFunc() +} + // Duration is a JSON-string-unmarshable duration type. type Duration time.Duration @@ -199,6 +224,7 @@ func GoModule() *debug.Module { } // goModule is the name of this Go module. +// TODO: we should be able to find this at runtime, see https://github.com/golang/go/issues/29228 const goModule = "github.com/caddyserver/caddy/v2" // CtxKey is a value type for use with context.WithValue. |