Permalink
Browse files

Implement Dir#Stop to close file events channel

  • Loading branch information...
1 parent 7c4e8b4 commit ea41c705994566584045b280cb0e60e3ca48b55b @mrnugget committed Mar 19, 2013
Showing with 42 additions and 22 deletions.
  1. +4 −1 watchgopher.go
  2. +21 −15 watchgopher/dir.go
  3. +9 −4 watchgopher/dir_test.go
  4. +8 −2 watchgopher/monitor.go
View
@@ -23,5 +23,8 @@ func main() {
path, _ = os.Getwd()
}
- watchgopher.Watch(path, *interval)
+ err := watchgopher.Watch(path, *interval)
+ if err != nil {
+ panic(err)
+ }
}
View
@@ -9,17 +9,23 @@ import (
const fileChanBuf = 500
-func NewDir(path string) (d *Dir) {
+func NewDir(path string) (d *Dir, err error) {
files := make(map[string]os.FileInfo)
events := make(chan *fsnotify.FileEvent, fileChanBuf)
- return &Dir{path, files, events}
+ watcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return nil, err
+ }
+
+ return &Dir{path, files, events, watcher}, nil
}
type Dir struct {
- Path string
- Files map[string]os.FileInfo
- Events chan *fsnotify.FileEvent
+ Path string
+ Files map[string]os.FileInfo
+ Events chan *fsnotify.FileEvent
+ watcher *fsnotify.Watcher
}
func (d *Dir) Listen() (err error) {
@@ -28,24 +34,24 @@ func (d *Dir) Listen() (err error) {
return
}
- watcher, err := fsnotify.NewWatcher()
+ err = d.watcher.Watch(d.Path)
if err != nil {
return
}
- err = watcher.Watch(d.Path)
- if err != nil {
- return
- }
-
- go d.handleEvents(watcher.Event, watcher.Error)
+ go d.proxyEvents()
return nil
}
-func (d *Dir) handleEvents(events chan *fsnotify.FileEvent, errs chan error) {
+func (d *Dir) Stop() (err error) {
+ err = d.watcher.Close()
+ return
+}
+
+func (d *Dir) proxyEvents() {
for {
select {
- case ev := <-events:
+ case ev := <-d.watcher.Event:
if ev.IsRename() || ev.IsDelete() {
delete(d.Files, ev.Name)
}
@@ -57,7 +63,7 @@ func (d *Dir) handleEvents(events chan *fsnotify.FileEvent, errs chan error) {
d.Files[ev.Name] = fi
}
d.Events <- ev
- case <-errs:
+ case <-d.watcher.Error:
break
}
}
View
@@ -15,10 +15,12 @@ var (
)
func TestFiles(t *testing.T) {
- dir := NewDir(fixtures)
+ dir, err := NewDir(fixtures)
+ checkErr(t, err)
- err := dir.Listen()
+ err = dir.Listen()
checkErr(t, err)
+ defer dir.Stop()
_, ok := dir.Files[fixtures+"/foobar.txt"]
if !ok {
@@ -33,10 +35,13 @@ func TestFiles(t *testing.T) {
func TestEvents(t *testing.T) {
testfilepath := fixtures + "/testfile.txt"
- dir := NewDir(fixtures)
- err := dir.Listen()
+ dir, err := NewDir(fixtures)
+ checkErr(t, err)
+
+ err = dir.Listen()
checkErr(t, err)
+ defer dir.Stop()
err = ioutil.WriteFile(testfilepath, []byte("Hello World!"), 0644)
checkErr(t, err)
View
@@ -5,18 +5,24 @@ import (
"time"
)
-func Watch(path string, interval time.Duration) {
+func Watch(path string, interval time.Duration) (err error) {
fmt.Println("PATH:", path)
fmt.Println("INTERVAL:", interval)
fmt.Println("Watchgopher is watching...")
- dir := NewDir(path)
+ dir, err := NewDir(path)
+ if err != nil {
+ return
+ }
+
ticker := time.Tick(interval)
actions := []Action{Unzipper}
queue := make(chan string)
monitor := &Monitor{dir, ticker, actions, queue}
monitor.start()
+
+ return nil
}
type Monitor struct {

0 comments on commit ea41c70

Please sign in to comment.