diff --git a/main.go b/main.go index 4592b34..fdde3db 100644 --- a/main.go +++ b/main.go @@ -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 { @@ -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) } } diff --git a/watcher.go b/watcher.go index 33bc646..46e2334 100644 --- a/watcher.go +++ b/watcher.go @@ -9,6 +9,7 @@ import ( // Watcher ... type Watcher struct { + PollInterval int WatchItems []string IgnoreItems []string AllowedExtensions map[string]bool @@ -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 @@ -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, @@ -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) { @@ -68,6 +71,5 @@ func (w *Watcher) Watch() { // nolint: gocyclo w.Errors <- err } - time.Sleep(500 * time.Millisecond) } }