Skip to content

Commit

Permalink
scandir.WatchFile
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Nov 4, 2017
1 parent dbc8af4 commit 62b6e39
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions scandir.go
Expand Up @@ -115,20 +115,7 @@ func (s *ScanDir) Start(ctl Control) {
log.Printf("%s\n", out)
}
}
go func() {
if err := WatchFile(watch, s.watch); err != nil {
log.Printf("WatchFile error: %s", err)
// try 3 times sleeping i*100ms between retries
for i := int32(100); i <= 300; i += 100 {
time.Sleep(time.Duration(rand.Int31n(i)) * time.Millisecond)
err := WatchFile(watch, s.watch)
if err == nil {
return
}
}
log.Printf("Could not watch file %q error: %s", watch, err)
}
}()
go s.WatchFile(watch)
} else {
// remove service
s.services.Delete(serviceName)
Expand Down Expand Up @@ -162,19 +149,7 @@ func (s *ScanDir) Scandir(ctl Control) error {
} else {
log.Printf("%s\n", out)
}
go func() {
if err := WatchFile(path, s.watch); err != nil {
// try 3 times sleeping i*100ms between retries
for i := int32(100); i <= 300; i += 100 {
time.Sleep(time.Duration(rand.Int31n(i)) * time.Millisecond)
err := WatchFile(path, s.watch)
if err == nil {
return
}
}
log.Printf("Could not watch file %q error: %s", path, err)
}
}()
go s.WatchFile(path)
}
}
}
Expand All @@ -186,3 +161,18 @@ func (s *ScanDir) Scandir(ctl Control) error {
}
return filepath.Walk(s.scandir, find)
}

// WatchFile - react on file changes
func (s *ScanDir) WatchFile(path string) {
if err := WatchFile(path, s.watch); err != nil {
// try 3 times sleeping i*100ms between retries
for i := int32(100); i <= 300; i += 100 {
time.Sleep(time.Duration(rand.Int31n(i)) * time.Millisecond)
err := WatchFile(path, s.watch)
if err == nil {
return
}
}
log.Printf("Could not watch file %q error: %s", path, err)
}
}

0 comments on commit 62b6e39

Please sign in to comment.