diff options
author | 2016-04-15 20:00:17 +0100 | |
---|---|---|
committer | 2016-04-15 20:00:17 +0100 | |
commit | dcd05032a4624dd03000f51573e3c8626a2fa76d (patch) | |
tree | 3b68a877fc08fe2c764da61218299fd88315b2d8 | |
parent | aa0ba0c3b4376c55f78091cac58e2237fc5c91ec (diff) | |
download | coredns-dcd05032a4624dd03000f51573e3c8626a2fa76d.tar.gz coredns-dcd05032a4624dd03000f51573e3c8626a2fa76d.tar.zst coredns-dcd05032a4624dd03000f51573e3c8626a2fa76d.zip |
Watch the entire directory
Instead of watching a single file watch the entire directory and
catch the .Name from the event. On any event, just reload the damn
thing. This also fixes the problem of loosing events when the inode
changes.
-rw-r--r-- | middleware/file/zone.go | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/middleware/file/zone.go b/middleware/file/zone.go index 41be5267c..25d131251 100644 --- a/middleware/file/zone.go +++ b/middleware/file/zone.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path" "sync" "github.com/miekg/coredns/middleware" @@ -107,7 +108,7 @@ func (z *Zone) Reload(shutdown chan bool) error { if err != nil { return err } - err = watcher.Add(z.file) + err = watcher.Add(path.Dir(z.file)) if err != nil { return err } @@ -117,11 +118,7 @@ func (z *Zone) Reload(shutdown chan bool) error { for { select { case event := <-watcher.Events: - if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Rename == fsnotify.Rename { - if err := watcher.Add(z.file); err != nil { - log.Printf("[ERROR] Failed to open `%s' for `%s': %v", z.file, z.origin, err) - } - + if 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) @@ -139,7 +136,7 @@ func (z *Zone) Reload(shutdown chan bool) error { z.SIG = zone.SIG z.Tree = zone.Tree z.reloadMu.Unlock() - log.Printf("[INFO] Successfully reload zone `%s'", z.origin) + log.Printf("[INFO] Successfully reloaded zone `%s'", z.origin) } case <-shutdown: watcher.Close() |