wip need to use queue for firefox watcher

This commit is contained in:
Chakib Ben Ziane 2018-06-14 02:30:18 +02:00
parent 90f1b0d494
commit 8ca1a2097d
4 changed files with 42 additions and 18 deletions

View File

@ -24,6 +24,9 @@ const (
TFirefox TFirefox
) )
// Channel parameters
const EventsChanLen = 1000
// Used to store bookmark paths and other // Used to store bookmark paths and other
// data related to a particular browser kind // data related to a particular browser kind
type BrowserPaths struct { type BrowserPaths struct {
@ -55,6 +58,7 @@ type IBrowser interface {
// `BufferDB`: sqlite buffer used across jobs // `BufferDB`: sqlite buffer used across jobs
type BaseBrowser struct { type BaseBrowser struct {
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
eventsChan chan fsnotify.Event
baseDir string baseDir string
bkFile string bkFile string
@ -103,6 +107,10 @@ func (bw *BaseBrowser) GetPath() string {
return path.Join(bw.baseDir, bw.bkFile) return path.Join(bw.baseDir, bw.bkFile)
} }
func (bw *BaseBrowser) EventsChan() chan fsnotify.Event {
return bw.eventsChan
}
func (bw *BaseBrowser) GetDir() string { func (bw *BaseBrowser) GetDir() string {
return bw.baseDir return bw.baseDir
} }

View File

@ -1,5 +1,11 @@
package main package main
import (
"time"
"github.com/fsnotify/fsnotify"
)
var Firefox = BrowserPaths{ var Firefox = BrowserPaths{
"places.sqlite", "places.sqlite",
"/home/spike/.mozilla/firefox/p1rrgord.default/", "/home/spike/.mozilla/firefox/p1rrgord.default/",
@ -17,12 +23,17 @@ func NewFFBrowser() IBrowser {
browser.bkFile = Firefox.BookmarkFile browser.bkFile = Firefox.BookmarkFile
browser.Stats = &ParserStats{} browser.Stats = &ParserStats{}
browser.NodeTree = &Node{Name: "root", Parent: nil} browser.NodeTree = &Node{Name: "root", Parent: nil}
browser.eventsChan = make(chan fsnotify.Event, EventsChanLen)
// Across jobs buffer // Across jobs buffer
browser.InitBuffer() browser.InitBuffer()
browser.SetupWatcher() 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 return browser
} }

View File

@ -9,16 +9,23 @@ import (
// TODO // TODO
// Run debounce in it's own thread when the watcher is started // 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 // 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) { func debouncer(interval time.Duration, input chan fsnotify.Event, w IWatchable) {
var item fsnotify.Event log.Debug("Running debouncer")
//var event fsnotify.Event
ticker := time.NewTicker(interval)
for { for {
select { select {
case item = <-input: //case event = <-input:
log.Debugf("received an event %v on the spammy events channel", item.Op) //log.Debugf("received an event %v on the spammy events channel", event.Op)
case <-time.After(interval):
log.Debug("Runngin parse method") //// Run the job
w.Run() ////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")
} }
} }
} }

View File

@ -1,8 +1,6 @@
package main package main
import ( import (
"time"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
@ -15,13 +13,12 @@ type IWatchable interface {
Watcher() *fsnotify.Watcher // returns linked watcher Watcher() *fsnotify.Watcher // returns linked watcher
GetPath() string // returns watched path GetPath() string // returns watched path
GetDir() string // returns watched dir GetDir() string // returns watched dir
EventsChan() chan fsnotify.Event
} }
// Main thread for watching file changes // Main thread for watching file changes
func WatcherThread(w IWatchable) { func WatcherThread(w IWatchable) {
spammyEventsChannel := make(chan fsnotify.Event)
bookmarkPath := w.GetPath() bookmarkPath := w.GetPath()
log.Infof("watching %s", bookmarkPath) log.Infof("watching %s", bookmarkPath)
@ -46,11 +43,12 @@ func WatcherThread(w IWatchable) {
} }
// Firefox keeps the file open and makes changes on it // Firefox keeps the file open and makes changes on it
if event.Op&fsnotify.Write == fsnotify.Write && // It needs a debouncer
event.Name == bookmarkPath { if event.Name == bookmarkPath {
debugPrint("event: %v | eventName: %v", event.Op, event.Name) debugPrint("event: %v | eventName: %v", event.Op, event.Name)
go debounce(1000*time.Millisecond, spammyEventsChannel, w) //go debounce(1000*time.Millisecond, spammyEventsChannel, w)
spammyEventsChannel <- event ch := w.EventsChan()
ch <- event
//w.Run() //w.Run()
} }
case err := <-watcher.Errors: case err := <-watcher.Errors: