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
)
// 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
}

View File

@ -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
}

View File

@ -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")
}
}
}

View File

@ -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: