Permalink
Browse files

Rearchitecturing: add file queue

  • Loading branch information...
1 parent ad25737 commit 442000d7285f9435115f18bed5bb52cfffb25778 @mrnugget committed Mar 17, 2013
Showing with 16 additions and 10 deletions.
  1. +2 −3 watchgopher/actions.go
  2. +14 −7 watchgopher/monitor.go
View
@@ -2,13 +2,12 @@ package watchgopher
import (
"fmt"
- "os"
"regexp"
)
-type Action func(string, os.FileInfo)
+type Action func(path string)
-func Unzipper(path string, fi os.FileInfo) {
+func Unzipper(path string) {
ok, err := regexp.MatchString(`^.*\.zip$`, path)
if err == nil && ok {
View
@@ -13,37 +13,44 @@ func Watch(path string, interval time.Duration) {
dir := NewDir(path)
ticker := time.Tick(interval)
actions := []Action{Unzipper}
+ queue := make(chan string)
- monitor := &Monitor{dir, ticker, actions}
+ monitor := &Monitor{dir, ticker, actions, queue}
monitor.start()
}
type Monitor struct {
dir *Dir
ticker <-chan time.Time
actions []Action
+ queue chan string
}
func (m *Monitor) start() {
+ go m.workOff(m.queue)
+
err := m.dir.StartWatching()
if err != nil {
panic(err)
}
for {
select {
- case <-m.dir.Events:
- m.run()
+ case ev := <-m.dir.Events:
+ m.queue <- ev.Name
case <-m.ticker:
- m.run()
+ m.dir.scan()
+ for fpath, _ := range m.dir.Files {
+ m.queue <- fpath
+ }
}
}
}
-func (m *Monitor) run() {
- for fpath, finfo := range m.dir.Files {
+func (m *Monitor) workOff(queue chan string) {
+ for fpath := range queue {
for _, action := range m.actions {
- action(fpath, finfo)
+ action(fpath)
}
}
}

0 comments on commit 442000d

Please sign in to comment.