Skip to content

Commit 81dc9f3

Browse files
committed
add OnUpgrade callback
1 parent b1ad5f2 commit 81dc9f3

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

caddy.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
//
1717
// To use this package:
1818
//
19-
// 1. Set the AppName and AppVersion variables.
20-
// 2. Call LoadCaddyfile() to get the Caddyfile.
21-
// Pass in the name of the server type (like "http").
22-
// Make sure the server type's package is imported
23-
// (import _ "github.com/coredns/caddy/caddyhttp").
24-
// 3. Call caddy.Start() to start Caddy. You get back
25-
// an Instance, on which you can call Restart() to
26-
// restart it or Stop() to stop it.
19+
// 1. Set the AppName and AppVersion variables.
20+
// 2. Call LoadCaddyfile() to get the Caddyfile.
21+
// Pass in the name of the server type (like "http").
22+
// Make sure the server type's package is imported
23+
// (import _ "github.com/coredns/caddy/caddyhttp").
24+
// 3. Call caddy.Start() to start Caddy. You get back
25+
// an Instance, on which you can call Restart() to
26+
// restart it or Stop() to stop it.
2727
//
2828
// You should call Wait() on your instance to wait for
2929
// all servers to quit before your process exits.
@@ -113,6 +113,7 @@ type Instance struct {
113113
OnRestartFailed []func() error // if restart failed
114114
OnShutdown []func() error // stopping, even as part of a restart
115115
OnFinalShutdown []func() error // stopping, not as part of a restart
116+
OnUpgrade []func() error // stopping, not as part of a shutdown
116117

117118
// storing values on an instance is preferable to
118119
// global state because these will get garbage-

controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ func (c *Controller) OnFinalShutdown(fn func() error) {
104104
c.instance.OnFinalShutdown = append(c.instance.OnFinalShutdown, fn)
105105
}
106106

107+
// OnUpgrade adds fn to the list of callback functions to execute
108+
// when the plugin need to do something before upgrade (eg. release port)
109+
func (c *Controller) OnUpgrade(fn func() error) {
110+
c.instance.OnUpgrade = append(c.instance.OnUpgrade, fn)
111+
}
112+
107113
// Context gets the context associated with the instance associated with c.
108114
func (c *Controller) Context() Context {
109115
return c.instance.context

upgrade.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ func Upgrade() error {
118118
}
119119
}
120120
}
121+
122+
for i, onUpgradeFunc := range inst.OnUpgrade {
123+
err := onUpgradeFunc()
124+
if err != nil {
125+
log.Printf("[WARN] OnUpgrade[%d] function returned error: %v", i, err)
126+
}
127+
}
121128
}
122129

123130
// set up the command

0 commit comments

Comments
 (0)