Skip to content

Commit

Permalink
Fix poll interval not been used properly in the watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcnunes committed Jun 19, 2018
1 parent 8e02bc3 commit 28cf317
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion main.go
Expand Up @@ -162,7 +162,7 @@ func runGaper(cfg *Config) error {
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()
for {
Expand All @@ -181,6 +181,7 @@ func runGaper(cfg *Config) error {
case err := <-watcher.Errors:
return fmt.Errorf("error on watching files: %v", err)
default:
logger.Debug("Waiting watch event")
time.Sleep(time.Duration(cfg.PollInterval) * time.Millisecond)
}
}
Expand Down
6 changes: 4 additions & 2 deletions watcher.go
Expand Up @@ -9,6 +9,7 @@ import (

// Watcher ...
type Watcher struct {
PollInterval int
WatchItems []string
IgnoreItems []string
AllowedExtensions map[string]bool
Expand All @@ -17,7 +18,7 @@ type Watcher struct {
}

// 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)
for _, ext := range extensions {
allowedExts["."+ext] = true
Expand All @@ -26,6 +27,7 @@ func NewWatcher(watchItems []string, ignoreItems []string, extensions []string)
return &Watcher{
Events: make(chan string),
Errors: make(chan error),
PollInterval: pollInterval,
WatchItems: watchItems,
IgnoreItems: ignoreItems,
AllowedExtensions: allowedExts,
Expand Down Expand Up @@ -53,6 +55,7 @@ func (w *Watcher) Watch() { // nolint: gocyclo
if filepath.Base(path)[0] == '.' {
return nil
}
time.Sleep(time.Duration(w.PollInterval) * time.Millisecond)

ext := filepath.Ext(path)
if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) {
Expand All @@ -68,6 +71,5 @@ func (w *Watcher) Watch() { // nolint: gocyclo
w.Errors <- err
}

time.Sleep(500 * time.Millisecond)
}
}

0 comments on commit 28cf317

Please sign in to comment.