* update docs * plugins: use plugin specific logging Hooking up pkg/log also changed NewWithPlugin to just take a string instead of a plugin.Handler as that is more flexible and for instance the Root "plugin" doesn't implement it fully. Same logging from the reload plugin: .:1043 2018/04/22 08:56:37 [INFO] CoreDNS-1.1.1 2018/04/22 08:56:37 [INFO] linux/amd64, go1.10.1, CoreDNS-1.1.1 linux/amd64, go1.10.1, 2018/04/22 08:56:37 [INFO] plugin/reload: Running configuration MD5 = ec4c9c55cd19759ea1c46b8c45742b06 2018/04/22 08:56:54 [INFO] Reloading 2018/04/22 08:56:54 [INFO] plugin/reload: Running configuration MD5 = 9e2bfdd85bdc9cceb740ba9c80f34c1a 2018/04/22 08:56:54 [INFO] Reloading complete * update docs * better doc
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package reload
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"time"
|
|
|
|
"github.com/mholt/caddy"
|
|
)
|
|
|
|
// reload periodically checks if the Corefile has changed, and reloads if so
|
|
const (
|
|
unused = 0
|
|
maybeUsed = 1
|
|
used = 2
|
|
)
|
|
|
|
type reload struct {
|
|
interval time.Duration
|
|
usage int
|
|
quit chan bool
|
|
}
|
|
|
|
func hook(event caddy.EventName, info interface{}) error {
|
|
if event != caddy.InstanceStartupEvent {
|
|
return nil
|
|
}
|
|
|
|
// if reload is removed from the Corefile, then the hook
|
|
// is still registered but setup is never called again
|
|
// so we need a flag to tell us not to reload
|
|
if r.usage == unused {
|
|
return nil
|
|
}
|
|
|
|
// this should be an instance. ok to panic if not
|
|
instance := info.(*caddy.Instance)
|
|
md5sum := md5.Sum(instance.Caddyfile().Body())
|
|
log.Infof("Running configuration MD5 = %x\n", md5sum)
|
|
|
|
go func() {
|
|
tick := time.NewTicker(r.interval)
|
|
|
|
for {
|
|
select {
|
|
case <-tick.C:
|
|
corefile, err := caddy.LoadCaddyfile(instance.Caddyfile().ServerType())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
s := md5.Sum(corefile.Body())
|
|
if s != md5sum {
|
|
// Let not try to restart with the same file, even though it is wrong.
|
|
md5sum = s
|
|
// now lets consider that plugin will not be reload, unless appear in next config file
|
|
// change status iof usage will be reset in setup if the plugin appears in config file
|
|
r.usage = maybeUsed
|
|
_, err := instance.Restart(corefile)
|
|
if err != nil {
|
|
log.Errorf("Corefile changed but reload failed: %s\n", err)
|
|
continue
|
|
}
|
|
// we are done, if the plugin was not set used, then it is not.
|
|
if r.usage == maybeUsed {
|
|
r.usage = unused
|
|
}
|
|
return
|
|
}
|
|
case <-r.quit:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|