From 8ca1a2097d3036d3cc11dd751ad010e0f41ac58a Mon Sep 17 00:00:00 2001 From: Chakib Ben Ziane Date: Thu, 14 Jun 2018 02:30:18 +0200 Subject: [PATCH] wip need to use queue for firefox watcher --- browsers.go | 14 +++++++++++--- firefox.go | 11 +++++++++++ utils.go | 21 ++++++++++++++------- watcher.go | 14 ++++++-------- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/browsers.go b/browsers.go index f92cbc6..2e9792c 100644 --- a/browsers.go +++ b/browsers.go @@ -24,6 +24,9 @@ const ( TFirefox ) +// Channel parameters +const EventsChanLen = 1000 + // Used to store bookmark paths and other // data related to a particular browser kind type BrowserPaths struct { @@ -54,9 +57,10 @@ type IBrowser interface { // // `BufferDB`: sqlite buffer used across jobs type BaseBrowser struct { - watcher *fsnotify.Watcher - baseDir string - bkFile string + watcher *fsnotify.Watcher + eventsChan chan fsnotify.Event + baseDir string + bkFile string // In memory sqlite db (named `memcache`). // Used to keep a browser's state of bookmarks across jobs. @@ -103,6 +107,10 @@ func (bw *BaseBrowser) GetPath() string { return path.Join(bw.baseDir, bw.bkFile) } +func (bw *BaseBrowser) EventsChan() chan fsnotify.Event { + return bw.eventsChan +} + func (bw *BaseBrowser) GetDir() string { return bw.baseDir } diff --git a/firefox.go b/firefox.go index 1a814eb..b1142c0 100644 --- a/firefox.go +++ b/firefox.go @@ -1,5 +1,11 @@ package main +import ( + "time" + + "github.com/fsnotify/fsnotify" +) + var Firefox = BrowserPaths{ "places.sqlite", "/home/spike/.mozilla/firefox/p1rrgord.default/", @@ -17,12 +23,17 @@ func NewFFBrowser() IBrowser { browser.bkFile = Firefox.BookmarkFile browser.Stats = &ParserStats{} browser.NodeTree = &Node{Name: "root", Parent: nil} + browser.eventsChan = make(chan fsnotify.Event, EventsChanLen) // Across jobs buffer browser.InitBuffer() browser.SetupWatcher() + // Run debouncer to avoid duplicate running of jobs + // when a batch of events is received + go debouncer(3000*time.Millisecond, browser.eventsChan, browser) + return browser } diff --git a/utils.go b/utils.go index 6f12f82..27019e9 100644 --- a/utils.go +++ b/utils.go @@ -9,16 +9,23 @@ import ( // TODO // Run debounce in it's own thread when the watcher is started // It receives a struct{event, func} and runs the func only once in the interval -func debounce(interval time.Duration, input chan fsnotify.Event, w IWatchable) { - var item fsnotify.Event +func debouncer(interval time.Duration, input chan fsnotify.Event, w IWatchable) { + log.Debug("Running debouncer") + //var event fsnotify.Event + + ticker := time.NewTicker(interval) for { select { - case item = <-input: - log.Debugf("received an event %v on the spammy events channel", item.Op) - case <-time.After(interval): - log.Debug("Runngin parse method") - w.Run() + //case event = <-input: + //log.Debugf("received an event %v on the spammy events channel", event.Op) + + //// Run the job + ////w.Run() + + case <-ticker.C: + log.Debugf("debouncer ticker ... events len: %v", len(input)) + log.Debug("implement a queue ! Should not use channels as queues") } } } diff --git a/watcher.go b/watcher.go index 2cdd77a..33a5389 100644 --- a/watcher.go +++ b/watcher.go @@ -1,8 +1,6 @@ package main import ( - "time" - "github.com/fsnotify/fsnotify" ) @@ -15,13 +13,12 @@ type IWatchable interface { Watcher() *fsnotify.Watcher // returns linked watcher GetPath() string // returns watched path GetDir() string // returns watched dir + EventsChan() chan fsnotify.Event } // Main thread for watching file changes func WatcherThread(w IWatchable) { - spammyEventsChannel := make(chan fsnotify.Event) - bookmarkPath := w.GetPath() log.Infof("watching %s", bookmarkPath) @@ -46,11 +43,12 @@ func WatcherThread(w IWatchable) { } // Firefox keeps the file open and makes changes on it - if event.Op&fsnotify.Write == fsnotify.Write && - event.Name == bookmarkPath { + // It needs a debouncer + if event.Name == bookmarkPath { debugPrint("event: %v | eventName: %v", event.Op, event.Name) - go debounce(1000*time.Millisecond, spammyEventsChannel, w) - spammyEventsChannel <- event + //go debounce(1000*time.Millisecond, spammyEventsChannel, w) + ch := w.EventsChan() + ch <- event //w.Run() } case err := <-watcher.Errors: