aboutsummaryrefslogtreecommitdiff
path: root/plugin/file/reload.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/file/reload.go')
-rw-r--r--plugin/file/reload.go62
1 files changed, 29 insertions, 33 deletions
diff --git a/plugin/file/reload.go b/plugin/file/reload.go
index 18e949a94..53b57fd5e 100644
--- a/plugin/file/reload.go
+++ b/plugin/file/reload.go
@@ -3,56 +3,52 @@ package file
import (
"log"
"os"
- "path"
-
- "github.com/fsnotify/fsnotify"
+ "time"
)
+// TickTime is the default time we use to reload zone. Exported to be tweaked in tests.
+var TickTime = 1 * time.Minute
+
// Reload reloads a zone when it is changed on disk. If z.NoRoload is true, no reloading will be done.
func (z *Zone) Reload() error {
if z.NoReload {
return nil
}
- watcher, err := fsnotify.NewWatcher()
- if err != nil {
- return err
- }
- err = watcher.Add(path.Dir(z.file))
- if err != nil {
- return err
- }
+
+ tick := time.NewTicker(TickTime)
go func() {
- // TODO(miek): needs to be killed on reload.
+
for {
select {
- case event := <-watcher.Events:
- if path.Clean(event.Name) == z.file {
- reader, err := os.Open(z.file)
- if err != nil {
- log.Printf("[ERROR] Failed to open `%s' for `%s': %v", z.file, z.origin, err)
- continue
- }
+ case <-tick.C:
+ reader, err := os.Open(z.file)
+ if err != nil {
+ log.Printf("[ERROR] Failed to open zone %q in %q: %v", z.origin, z.file, err)
+ continue
+ }
- serial := z.SOASerialIfDefined()
- zone, err := Parse(reader, z.origin, z.file, serial)
- if err != nil {
- log.Printf("[WARNING] Parsing zone `%s': %v", z.origin, err)
- continue
+ serial := z.SOASerialIfDefined()
+ zone, err := Parse(reader, z.origin, z.file, serial)
+ if err != nil {
+ if _, ok := err.(*serialErr); !ok {
+ log.Printf("[ERROR] Parsing zone %q: %v", z.origin, err)
}
+ continue
+ }
- // copy elements we need
- z.reloadMu.Lock()
- z.Apex = zone.Apex
- z.Tree = zone.Tree
- z.reloadMu.Unlock()
+ // copy elements we need
+ z.reloadMu.Lock()
+ z.Apex = zone.Apex
+ z.Tree = zone.Tree
+ z.reloadMu.Unlock()
+
+ log.Printf("[INFO] Successfully reloaded zone %q in %q with serial %d", z.origin, z.file, z.Apex.SOA.Serial)
+ z.Notify()
- log.Printf("[INFO] Successfully reloaded zone `%s'", z.origin)
- z.Notify()
- }
case <-z.ReloadShutdown:
- watcher.Close()
+ tick.Stop()
return
}
}