Watcher Stopped watching after updating the package
Akumzy opened this issue · comments
Thanks you for the this Package.
I noticed the w.AddFilterHook(watcher.RegexFilterHook(r, false))
method which was not available in my previous version so I just update the package since then the watcher is not working anymore
I reverted backgithub.com/radovskyb/watcher v1.0.2
and it's working again
The github.com/radovskyb/watcher v1.0.2
is not CPU friendly someone me help please...
Hi @Akumzy,
Can you show an example of what is not working for you in a newer version? Like v1.0.5 or v1.0.6.
Also in general, polling at a rate such as 100ms
will not be so CPU friendly unfortunately. There are other options such as fsnotify
which doesn't use polling if that's an issue for you :)
@radovskyb thanks for responding
I ran the exact same readme demo code and it still didn't work for me
but my code looks like this below and as of the versions I can't really say but that version has w.AddFilterHook(watcher.RegexFilterHook(r, false))
support
I just ran go get -u github.com/radovskyb/watcher/..
to update package and it stopped watching
But the working version am using is v1.0.2
and I know that because am using go mod
Am using Ubuntu 18.10
func StartWatcher(file string) {
waitBeforeUpload = true
w = watcher.New()
w.SetMaxEvents(1)
// Only notify rename and move events.
w.FilterOps(watcher.Rename, watcher.Move)
w.IgnoreHiddenFiles(true)
for _, reg := range ignoreReg {
r := regexp.MustCompile(reg)
w.AddFilterHook(watcher.RegexFilterHook(r, false))
}
// Enable hiden file channel
if err := w.AddRecursive(file); err != nil {
log.Println(err)
return
}
log.Println("Watching --> ", file)
if len(config.DONOTWATCH) > 0 {
notWatching <- config.DONOTWATCH
}
Emit("go:watcher-ready", true)
log.Println("Watcher is ready")
go handleChanges()
go uploadHandler()
go func() {
for {
select {
case event := <-w.Event:
if (event.IsDir() && event.Op == watcher.Write) && Contains(config.Department, event.Name()) {
continue
}
log.Println("New event")
log.Printf("%+v", event) // Print the event's info.
var (
err error
result []Items
)
store.Find(&result, bolthold.Where("Path").Eq(item.Path).And("IsDir").Eq(item.IsDir))
if err != nil {
log.Println(err)
continue
}
if len(result) > 0 {
err = store.Update(result[0].ID, item)
if err != nil {
log.Println(err)
continue
}
item.ID = result[0].ID
} else {
err = store.Insert(bolthold.NextSequence(), item)
if err != nil {
log.Println(err)
continue
}
}
changes <- item
Emit("watcher:change", item)
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Collect all the watched file or directory and store them in
// database if not avaliable
var items []Items
// log.Printf("Total files/directories watched is %d", len(w.WatchedFiles()))
for key, file := range w.WatchedFiles() {
log.Println(key)
if key == config.Drive {
continue
}
var result []Items
store.Find(&result, bolthold.Where("Path").Eq(key).And("IsDir").Eq(file.IsDir()).And("OnCloud").Eq(false))
if len(result) == 0 {
item := Items{
Name: file.Name(),
Parent: path.Dir(key),
Path: key,
ModTime: file.ModTime(),
Dirty: !Contains(config.Department, file.Name()),
IsDir: file.IsDir(),
Size: file.Size(),
UserID: config.UserID,
CloudPath: getCloudPath(key),
OnCloud: Contains(config.Department, file.Name()),
}
if item.Dirty {
item.DirtyType = CREATE
changes <- item
}
items = append(items, item)
}
}
if len(items) > 0 {
for _, item := range items {
err := store.Insert(bolthold.NextSequence(), item)
if err != nil {
log.Println(err)
continue
}
}
waitBeforeUpload = false
} else {
waitBeforeUpload = false
}
if err := w.Start(time.Millisecond * 1000); err != nil {
log.Fatalln(err)
}
}
I just noticed after commenting out the AddFilterHook
it started watching again
//I forgot to escape the dollar symbol
r := regexp.MustCompile("^~$")
w.AddFilterHook(watcher.RegexFilterHook(r, false))
Am very sorry for the false alarm
this is the right code
r := regexp.MustCompile("^~\\$")
No problem :)