Moved InotifyTracker initialization to constructor function
This commit is contained in:
parent
a5dc0d39ba
commit
d46611791d
|
@ -28,11 +28,11 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
|
||||||
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 := shared.WatchFlags(dirname, fsnotify.FSN_CREATE)
|
err := WatchFlags(dirname, fsnotify.FSN_CREATE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer shared.RemoveWatch(dirname)
|
defer 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.
|
||||||
|
@ -41,7 +41,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
events := shared.Events(fw.Filename)
|
events := Events(fw.Filename)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -61,7 +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()
|
||||||
|
|
||||||
err := shared.Watch(fw.Filename)
|
err := Watch(fw.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
go changes.NotifyDeleted()
|
go changes.NotifyDeleted()
|
||||||
}
|
}
|
||||||
|
@ -69,10 +69,10 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
|
||||||
fw.Size = fi.Size()
|
fw.Size = fi.Size()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer shared.RemoveWatch(fw.Filename)
|
defer RemoveWatch(fw.Filename)
|
||||||
defer changes.Close()
|
defer changes.Close()
|
||||||
|
|
||||||
events := shared.Events(fw.Filename)
|
events := Events(fw.Filename)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
prevSize := fw.Size
|
prevSize := fw.Size
|
||||||
|
|
|
@ -30,18 +30,19 @@ type watchInfo struct {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// globally shared InotifyTracker; ensures only one fsnotify.Watcher is used
|
// globally shared InotifyTracker; ensures only one fsnotify.Watcher is used
|
||||||
shared = &InotifyTracker{
|
shared *InotifyTracker
|
||||||
mux: sync.Mutex{},
|
|
||||||
chans: make(map[string]chan *fsnotify.FileEvent),
|
|
||||||
done: make(map[string]chan bool),
|
|
||||||
watch: make(chan *watchInfo),
|
|
||||||
remove: make(chan string),
|
|
||||||
error: make(chan error),
|
|
||||||
}
|
|
||||||
|
|
||||||
// these are used to ensure the shared InotifyTracker is run exactly once
|
// these are used to ensure the shared InotifyTracker is run exactly once
|
||||||
once = &sync.Once{}
|
once = sync.Once{}
|
||||||
goRun = func() {
|
goRun = func() {
|
||||||
|
shared = &InotifyTracker{
|
||||||
|
mux: sync.Mutex{},
|
||||||
|
chans: make(map[string]chan *fsnotify.FileEvent),
|
||||||
|
done: make(map[string]chan bool),
|
||||||
|
watch: make(chan *watchInfo),
|
||||||
|
remove: make(chan string),
|
||||||
|
error: make(chan error),
|
||||||
|
}
|
||||||
go shared.run()
|
go shared.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,13 +51,13 @@ var (
|
||||||
|
|
||||||
// WatchFlags signals the run goroutine to begin watching the input filename using
|
// WatchFlags signals the run goroutine to begin watching the input filename using
|
||||||
// using all flags.
|
// using all flags.
|
||||||
func (shared *InotifyTracker) Watch(fname string) error {
|
func Watch(fname string) error {
|
||||||
return shared.WatchFlags(fname, fsnotify.FSN_ALL)
|
return WatchFlags(fname, fsnotify.FSN_ALL)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WatchFlags signals the run goroutine to begin watching the input filename using
|
// WatchFlags signals the run goroutine to begin watching the input filename using
|
||||||
// using the input flags.
|
// using the input flags.
|
||||||
func (shared *InotifyTracker) WatchFlags(fname string, flags uint32) error {
|
func WatchFlags(fname string, flags uint32) error {
|
||||||
// start running the shared InotifyTracker if not already running
|
// start running the shared InotifyTracker if not already running
|
||||||
once.Do(goRun)
|
once.Do(goRun)
|
||||||
|
|
||||||
|
@ -68,7 +69,7 @@ func (shared *InotifyTracker) WatchFlags(fname string, flags uint32) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveWatch signals the run goroutine to remove the watch for the input filename
|
// RemoveWatch signals the run goroutine to remove the watch for the input filename
|
||||||
func (shared *InotifyTracker) RemoveWatch(fname string) {
|
func RemoveWatch(fname string) {
|
||||||
// start running the shared InotifyTracker if not already running
|
// start running the shared InotifyTracker if not already running
|
||||||
once.Do(goRun)
|
once.Do(goRun)
|
||||||
|
|
||||||
|
@ -86,17 +87,16 @@ func (shared *InotifyTracker) RemoveWatch(fname string) {
|
||||||
// Events returns a channel to which FileEvents corresponding to the input filename
|
// Events returns a channel to which FileEvents corresponding to the input filename
|
||||||
// will be sent. This channel will be closed when removeWatch is called on this
|
// will be sent. This channel will be closed when removeWatch is called on this
|
||||||
// filename.
|
// filename.
|
||||||
func (shared *InotifyTracker) Events(fname string) chan *fsnotify.FileEvent {
|
func Events(fname string) chan *fsnotify.FileEvent {
|
||||||
shared.mux.Lock()
|
shared.mux.Lock()
|
||||||
defer shared.mux.Unlock()
|
defer shared.mux.Unlock()
|
||||||
|
|
||||||
return shared.chans[fname]
|
return shared.chans[fname]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup removes the watch for the input filename and closes the shared Watcher
|
// Cleanup removes the watch for the input filename if necessary.
|
||||||
// if there are no more targets.
|
|
||||||
func Cleanup(fname string) {
|
func Cleanup(fname string) {
|
||||||
shared.RemoveWatch(fname)
|
RemoveWatch(fname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating
|
// watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating
|
||||||
|
|
Loading…
Reference in New Issue