Skip to content

Commit b70935d

Browse files
HurSungYungithubnemo
authored andcommitted
make polling interval a parameter
1 parent 6270e28 commit b70935d

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

daemon.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ import (
8484
// Milliseconds to wait for the next job to begin after a file change
8585
const WorkDelay = 900
8686

87-
// Milliseconds of interval between polling file changes when polling option is selected
88-
const PollingInterval = 100
89-
9087
// Default pattern to match files which trigger a build
9188
const FilePattern = `(.+\.go|.+\.c)$`
9289

@@ -124,6 +121,7 @@ var (
124121
flagGracefulTimeout = flag.Uint("graceful-timeout", 3, "Duration (in seconds) to wait for graceful kill to complete")
125122
flagVerbose = flag.Bool("verbose", false, "Be verbose about which directories are watched.")
126123
flagPolling = flag.Bool("polling", false, "Use polling method to watch file change instead of fsnotify")
124+
flagPollingInterval = flag.Int("polling-interval", 100, "Milliseconds of interval between polling file changes when polling option is selected")
127125

128126
// initialized in main() due to custom type.
129127
flagDirectories globList
@@ -394,14 +392,15 @@ func main() {
394392
pattern := regexp.MustCompile(*flagPattern)
395393

396394
cfg := &WatcherConfig{
397-
flagVerbose: *flagVerbose,
398-
flagRecursive: *flagRecursive,
399-
flagPolling: *flagPolling,
400-
flagDirectories: flagDirectories,
401-
flagExcludedDirs: flagExcludedDirs,
402-
flagExcludedFiles: flagExcludedFiles,
403-
flagIncludedFiles: flagIncludedFiles,
404-
pattern: pattern,
395+
flagVerbose: *flagVerbose,
396+
flagRecursive: *flagRecursive,
397+
flagPolling: *flagPolling,
398+
flagPollingInterval: *flagPollingInterval,
399+
flagDirectories: flagDirectories,
400+
flagExcludedDirs: flagExcludedDirs,
401+
flagExcludedFiles: flagExcludedFiles,
402+
flagIncludedFiles: flagIncludedFiles,
403+
pattern: pattern,
405404
}
406405
watcher, err := NewWatcher(cfg)
407406

watcher.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ import (
1313
"time"
1414
)
1515

16-
func pathMatches(cfg *WatcherConfig, pathName string) bool {
17-
base := filepath.Base(pathName)
18-
return (cfg.flagIncludedFiles.Matches(base) || matchesPattern(cfg.pattern, pathName)) &&
16+
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
17+
return cfg.flagRecursive == true && !cfg.flagExcludedDirs.Matches(path)
18+
}
19+
20+
func pathMatches(cfg *WatcherConfig, path string) bool {
21+
base := filepath.Base(path)
22+
return (cfg.flagIncludedFiles.Matches(base) || matchesPattern(cfg.pattern, path)) &&
1923
!cfg.flagExcludedFiles.Matches(base)
2024
}
2125

2226
type WatcherConfig struct {
23-
flagVerbose bool
24-
flagPolling bool
25-
flagRecursive bool
26-
flagDirectories globList
27-
flagExcludedDirs globList
28-
flagExcludedFiles globList
29-
flagIncludedFiles globList
30-
pattern *regexp.Regexp
27+
flagVerbose bool
28+
flagPolling bool
29+
flagRecursive bool
30+
flagPollingInterval int
31+
flagDirectories globList
32+
flagExcludedDirs globList
33+
flagExcludedFiles globList
34+
flagIncludedFiles globList
35+
pattern *regexp.Regexp
3136
}
3237

3338
type FileWatcher interface {
@@ -57,7 +62,7 @@ func (n NotifyWatcher) Watch(jobs chan<- string) {
5762
case ev := <-n.watcher.Events:
5863
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create {
5964
// Assume it is a directory and track it.
60-
if n.cfg.flagRecursive == true && !n.cfg.flagExcludedDirs.Matches(ev.Name) {
65+
if directoryShouldBeTracked(n.cfg, ev.Name) {
6166
n.watcher.Add(ev.Name)
6267
}
6368
if pathMatches(n.cfg, ev.Name) {
@@ -104,7 +109,7 @@ func (p PollingWatcher) AddFiles() error {
104109
func (p PollingWatcher) Watch(jobs chan<- string) {
105110
// Start the watching process.
106111
go func() {
107-
if err := p.watcher.Start(PollingInterval * time.Millisecond); err != nil {
112+
if err := p.watcher.Start(time.Duration(p.cfg.flagPollingInterval) * time.Millisecond); err != nil {
108113
log.Fatalln(err)
109114
}
110115
}()

0 commit comments

Comments
 (0)