Created
April 16, 2014 02:54
-
-
Save jouyouyun/10801017 to your computer and use it in GitHub Desktop.
Test Watch Dirs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/howeyc/fsnotify" | |
"os" | |
"regexp" | |
"sync" | |
"time" | |
) | |
const TEST_DIR = "/Datas/TestDir" | |
var ( | |
watchCloseChan = make(chan bool) | |
dirModChan = make(chan bool) | |
dirWatchMap = make(map[string]*fsnotify.Watcher) | |
prevEventStr = "" | |
mutex = new(sync.Mutex) | |
preTimestamp = int64(0) | |
) | |
func watchDir(dir string) { | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
fmt.Printf("New Watch Failed For Dir: '%s', Error: %v\n", | |
dir, err) | |
return | |
} | |
if err := watcher.Watch(dir); err != nil { | |
fmt.Printf("Watch '%s' Failed: %v\n", dir, err) | |
watcher.Close() | |
return | |
} | |
//fmt.Println("Watch Dir: ", dir) | |
dirWatchMap[dir] = watcher | |
go func() { | |
defer func() { | |
mutex.Lock() | |
if watcher != nil { | |
watcher.Close() | |
} | |
delete(dirWatchMap, dir) | |
mutex.Unlock() | |
}() | |
for { | |
select { | |
case ev := <-watcher.Event: | |
if ev == nil { | |
break | |
} | |
if ok, _ := regexp.MatchString(`\.swa?px?$`, | |
ev.Name); ok { | |
break | |
} | |
fmt.Println("\nEvent: ", ev) | |
//fmt.Printf("Prev Event: %s\n", | |
//prevEventStr) | |
if ev.IsCreate() || ev.IsDelete() { | |
if prevEventStr == ev.String() { | |
break | |
} | |
if getCurrentTimestamp()-preTimestamp < 1 { | |
break | |
} | |
prevEventStr = ev.String() | |
fmt.Println("Emit Dir Modify") | |
preTimestamp = getCurrentTimestamp() | |
dirModChan <- true | |
return | |
} | |
case err := <-watcher.Error: | |
if err == nil { | |
} | |
fmt.Println("Error: ", err) | |
case <-watchCloseChan: | |
return | |
} | |
} | |
}() | |
} | |
func startWatch(dir string) { | |
f, err := os.Open(dir) | |
if err != nil { | |
fmt.Printf("Open '%s' failed: %v\n", dir, err) | |
return | |
} | |
defer f.Close() | |
finfos, err1 := f.Readdir(0) | |
if err1 != nil { | |
fmt.Printf("Readdir '%s' failed: %v\n", dir, err1) | |
return | |
} | |
watchDir(dir) | |
for _, info := range finfos { | |
if info == nil || !info.IsDir() { | |
continue | |
} | |
startWatch(dir + "/" + info.Name()) | |
} | |
} | |
func resetWatchDir(dir string) { | |
for { | |
select { | |
case <-dirModChan: | |
mutex.Lock() | |
l := len(dirWatchMap) | |
fmt.Println("Reset Watchers: ", l) | |
for i := 0; i < l; i++ { | |
watchCloseChan <- true | |
} | |
mutex.Unlock() | |
//<-time.After(time.Microsecond * 3000) | |
startWatch(dir) | |
} | |
} | |
} | |
func getCurrentTimestamp() int64 { | |
t := time.Now().Unix() | |
fmt.Println("Timestamp:", t) | |
return t | |
} | |
func main() { | |
startWatch(TEST_DIR) | |
go resetWatchDir(TEST_DIR) | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment