Skip to content

add OnUpgrade callback #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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-
Expand Down
6 changes: 6 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down