one watcher

This commit is contained in:
Joel Reymont 2015-07-01 17:15:15 +02:00
parent 15e71d383e
commit 035761b7bc
1 changed files with 12 additions and 27 deletions

View File

@ -4,11 +4,12 @@ package watch
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"github.com/ActiveState/tail/util" "github.com/ActiveState/tail/util"
"gopkg.in/fsnotify.v0" "gopkg.in/fsnotify.v0"
"gopkg.in/tomb.v1" "gopkg.in/tomb.v1"
"os"
"path/filepath"
) )
var inotifyTracker *InotifyTracker var inotifyTracker *InotifyTracker
@ -17,27 +18,23 @@ var inotifyTracker *InotifyTracker
type InotifyFileWatcher struct { type InotifyFileWatcher struct {
Filename string Filename string
Size int64 Size int64
w *fsnotify.Watcher
} }
func NewInotifyFileWatcher(filename string) *InotifyFileWatcher { func NewInotifyFileWatcher(filename string, w *fsnotify.Watcher) *InotifyFileWatcher {
fw := &InotifyFileWatcher{filename, 0} fw := &InotifyFileWatcher{filename, 0, w}
return fw return fw
} }
func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error { func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
w, err := inotifyTracker.NewWatcher()
if err != nil {
return err
}
defer inotifyTracker.CloseWatcher(w)
dirname := filepath.Dir(fw.Filename) dirname := filepath.Dir(fw.Filename)
// Watch for new files to be created in the parent directory. // Watch for new files to be created in the parent directory.
err = w.WatchFlags(dirname, fsnotify.FSN_CREATE) err := fw.w.WatchFlags(dirname, fsnotify.FSN_CREATE)
if err != nil { if err != nil {
return err return err
} }
defer fw.w.RemoveWatch(dirname)
// Do a real check now as the file might have been created before // Do a real check now as the file might have been created before
// calling `WatchFlags` above. // calling `WatchFlags` above.
@ -48,7 +45,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
for { for {
select { select {
case evt, ok := <-w.Event: case evt, ok := <-fw.w.Event:
if !ok { if !ok {
return fmt.Errorf("inotify watcher has been closed") return fmt.Errorf("inotify watcher has been closed")
} else if evt.Name == fw.Filename { } else if evt.Name == fw.Filename {
@ -64,11 +61,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileChanges { func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileChanges {
changes := NewFileChanges() changes := NewFileChanges()
w, err := inotifyTracker.NewWatcher() err := fw.w.Watch(fw.Filename)
if err != nil {
util.Fatal("Error creating fsnotify watcher: %v", err)
}
err = w.Watch(fw.Filename)
if err != nil { if err != nil {
util.Fatal("Error watching %v: %v", fw.Filename, err) util.Fatal("Error watching %v: %v", fw.Filename, err)
} }
@ -76,7 +69,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
fw.Size = fi.Size() fw.Size = fi.Size()
go func() { go func() {
defer inotifyTracker.CloseWatcher(w) defer fw.w.RemoveWatch(fw.Filename)
defer changes.Close() defer changes.Close()
for { for {
@ -86,7 +79,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
var ok bool var ok bool
select { select {
case evt, ok = <-w.Event: case evt, ok = <-fw.w.Event:
if !ok { if !ok {
return return
} }
@ -126,11 +119,3 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
return changes return changes
} }
func Cleanup() {
inotifyTracker.CloseAll()
}
func init() {
inotifyTracker = NewInotifyTracker()
}