More config watcher fixes

This commit is contained in:
Svilen Markov 2024-11-30 12:37:56 +00:00
parent 98b4b7330e
commit 1785af4749

View File

@ -159,6 +159,14 @@ func configFilesWatcher(
onChange func(newContents []byte), onChange func(newContents []byte),
onErr func(error), onErr func(error),
) (func() error, error) { ) (func() error, error) {
mainFileAbsPath, err := filepath.Abs(mainFilePath)
if err != nil {
return nil, fmt.Errorf("getting absolute path of main file: %w", err)
}
// TODO: refactor, flaky
lastIncludes[mainFileAbsPath] = struct{}{}
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
return nil, fmt.Errorf("creating watcher: %w", err) return nil, fmt.Errorf("creating watcher: %w", err)
@ -169,26 +177,26 @@ func configFilesWatcher(
return nil, fmt.Errorf("adding main file to watcher: %w", err) return nil, fmt.Errorf("adding main file to watcher: %w", err)
} }
updateWatchedIncludes := func(previousIncludes map[string]struct{}, newIncludes map[string]struct{}) { updateWatchedFiles := func(previousWatched map[string]struct{}, newWatched map[string]struct{}) {
for includePath := range previousIncludes { for filePath := range previousWatched {
if _, ok := newIncludes[includePath]; !ok { if _, ok := newWatched[filePath]; !ok {
watcher.Remove(includePath) watcher.Remove(filePath)
} }
} }
for includePath := range newIncludes { for filePath := range newWatched {
if _, ok := previousIncludes[includePath]; !ok { if _, ok := previousWatched[filePath]; !ok {
if err := watcher.Add(includePath); err != nil { if err := watcher.Add(filePath); err != nil {
log.Printf( log.Printf(
"Could not add included config file to watcher, changes to this file will not trigger a reload. path: %s, error: %v", "Could not add file to watcher, changes to this file will not trigger a reload. path: %s, error: %v",
includePath, err, filePath, err,
) )
} }
} }
} }
} }
updateWatchedIncludes(nil, lastIncludes) updateWatchedFiles(nil, lastIncludes)
// needed for lastContents and lastIncludes because they get updated in multiple goroutines // needed for lastContents and lastIncludes because they get updated in multiple goroutines
mu := sync.Mutex{} mu := sync.Mutex{}
@ -204,7 +212,9 @@ func configFilesWatcher(
defer mu.Unlock() defer mu.Unlock()
if !maps.Equal(currentIncludes, lastIncludes) { if !maps.Equal(currentIncludes, lastIncludes) {
updateWatchedIncludes(lastIncludes, currentIncludes) // TODO: refactor, flaky
currentIncludes[mainFileAbsPath] = struct{}{}
updateWatchedFiles(lastIncludes, currentIncludes)
lastIncludes = currentIncludes lastIncludes = currentIncludes
} }