Fix poll interval not been used properly in the watcher

This commit is contained in:
Max Claus Nunes 2018-06-18 23:21:43 -03:00
parent 8e02bc3348
commit 28cf3176d3
2 changed files with 6 additions and 3 deletions

View File

@ -162,7 +162,7 @@ func runGaper(cfg *Config) error {
return fmt.Errorf("run error: %v", err) return fmt.Errorf("run error: %v", err)
} }
watcher := NewWatcher(cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions) watcher := NewWatcher(cfg.PollInterval, cfg.WatchItems, cfg.IgnoreItems, cfg.Extensions)
go watcher.Watch() go watcher.Watch()
for { for {
@ -181,6 +181,7 @@ func runGaper(cfg *Config) error {
case err := <-watcher.Errors: case err := <-watcher.Errors:
return fmt.Errorf("error on watching files: %v", err) return fmt.Errorf("error on watching files: %v", err)
default: default:
logger.Debug("Waiting watch event")
time.Sleep(time.Duration(cfg.PollInterval) * time.Millisecond) time.Sleep(time.Duration(cfg.PollInterval) * time.Millisecond)
} }
} }

View File

@ -9,6 +9,7 @@ import (
// Watcher ... // Watcher ...
type Watcher struct { type Watcher struct {
PollInterval int
WatchItems []string WatchItems []string
IgnoreItems []string IgnoreItems []string
AllowedExtensions map[string]bool AllowedExtensions map[string]bool
@ -17,7 +18,7 @@ type Watcher struct {
} }
// NewWatcher ... // NewWatcher ...
func NewWatcher(watchItems []string, ignoreItems []string, extensions []string) *Watcher { func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) *Watcher {
allowedExts := make(map[string]bool) allowedExts := make(map[string]bool)
for _, ext := range extensions { for _, ext := range extensions {
allowedExts["."+ext] = true allowedExts["."+ext] = true
@ -26,6 +27,7 @@ func NewWatcher(watchItems []string, ignoreItems []string, extensions []string)
return &Watcher{ return &Watcher{
Events: make(chan string), Events: make(chan string),
Errors: make(chan error), Errors: make(chan error),
PollInterval: pollInterval,
WatchItems: watchItems, WatchItems: watchItems,
IgnoreItems: ignoreItems, IgnoreItems: ignoreItems,
AllowedExtensions: allowedExts, AllowedExtensions: allowedExts,
@ -53,6 +55,7 @@ func (w *Watcher) Watch() { // nolint: gocyclo
if filepath.Base(path)[0] == '.' { if filepath.Base(path)[0] == '.' {
return nil return nil
} }
time.Sleep(time.Duration(w.PollInterval) * time.Millisecond)
ext := filepath.Ext(path) ext := filepath.Ext(path)
if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) { if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) {
@ -68,6 +71,5 @@ func (w *Watcher) Watch() { // nolint: gocyclo
w.Errors <- err w.Errors <- err
} }
time.Sleep(500 * time.Millisecond)
} }
} }