diff --git a/caddy.go b/caddy.go index eaec7bc453a..b09b3777e9e 100644 --- a/caddy.go +++ b/caddy.go @@ -16,14 +16,14 @@ // // To use this package: // -// 1. Set the AppName and AppVersion variables. -// 2. Call LoadCaddyfile() to get the Caddyfile. -// Pass in the name of the server type (like "http"). -// Make sure the server type's package is imported -// (import _ "github.com/coredns/caddy/caddyhttp"). -// 3. Call caddy.Start() to start Caddy. You get back -// an Instance, on which you can call Restart() to -// restart it or Stop() to stop it. +// 1. Set the AppName and AppVersion variables. +// 2. Call LoadCaddyfile() to get the Caddyfile. +// Pass in the name of the server type (like "http"). +// Make sure the server type's package is imported +// (import _ "github.com/coredns/caddy/caddyhttp"). +// 3. Call caddy.Start() to start Caddy. You get back +// an Instance, on which you can call Restart() to +// restart it or Stop() to stop it. // // You should call Wait() on your instance to wait for // all servers to quit before your process exits. @@ -113,6 +113,7 @@ type Instance struct { OnRestartFailed []func() error // if restart failed OnShutdown []func() error // stopping, even as part of a restart OnFinalShutdown []func() error // stopping, not as part of a restart + OnUpgrade []func() error // stopping, not as part of a shutdown // storing values on an instance is preferable to // global state because these will get garbage- diff --git a/controller.go b/controller.go index 5b9a2ec4474..97836ad120b 100644 --- a/controller.go +++ b/controller.go @@ -104,6 +104,12 @@ func (c *Controller) OnFinalShutdown(fn func() error) { c.instance.OnFinalShutdown = append(c.instance.OnFinalShutdown, fn) } +// OnUpgrade adds fn to the list of callback functions to execute +// when the plugin need to do something before upgrade (eg. release port) +func (c *Controller) OnUpgrade(fn func() error) { + c.instance.OnUpgrade = append(c.instance.OnUpgrade, fn) +} + // Context gets the context associated with the instance associated with c. func (c *Controller) Context() Context { return c.instance.context diff --git a/upgrade.go b/upgrade.go index c35f9b1cca7..f3818e266dd 100644 --- a/upgrade.go +++ b/upgrade.go @@ -118,6 +118,13 @@ func Upgrade() error { } } } + + for i, onUpgradeFunc := range inst.OnUpgrade { + err := onUpgradeFunc() + if err != nil { + log.Printf("[WARN] OnUpgrade[%d] function returned error: %v", i, err) + } + } } // set up the command