Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/watcher/watchManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package watcher
import (
"fmt"
"log"
"os"
"sync"
"time"

"github.com/fsnotify/fsnotify"
"github.com/microcks/microcks-cli/pkg/config"
Expand Down Expand Up @@ -81,7 +83,7 @@ func (wm *WatchManager) Run() {
for {
select {
case event := <-wm.fileWatcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
if event.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Rename|fsnotify.Remove) != 0 {
if event.Name == wm.configPath {
fmt.Println("[INFO] Reloading config...")
wm.lock.Lock()
Expand All @@ -95,7 +97,21 @@ func (wm *WatchManager) Run() {
entry, exists := wm.watchEntries[event.Name]
wm.lock.Unlock()
if exists {
go TriggerImport(entry)
// Re-establish watch if the file was replaced atomically (rename or remove).
if event.Op&(fsnotify.Rename|fsnotify.Remove) != 0 {
_ = wm.fileWatcher.Remove(event.Name)
time.Sleep(50 * time.Millisecond) // Let the OS complete the rename/replace operation
if _, err := os.Stat(event.Name); err == nil {
if err := wm.fileWatcher.Add(event.Name); err != nil {
time.Sleep(100 * time.Millisecond) // Retry once on slow file systems
_ = wm.fileWatcher.Add(event.Name)
}
}
}

if event.Op&fsnotify.Remove == 0 {
go TriggerImport(entry)
}
}
}
}
Expand Down
Loading