Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash when folder watcher fails #251

Merged
merged 1 commit into from
Jan 14, 2023
Merged
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
42 changes: 24 additions & 18 deletions pkg/unpackerr/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (u *Unpackerr) PollFolders() {

u.Folders, flist = u.checkFolders()

if u.folders, err = u.newFolderWatcher(); err != nil {
if err = u.newFolderWatcher(); err != nil {
u.Print("[ERROR] Watching Folders:", err)
return
}
Expand All @@ -126,18 +126,34 @@ func (u *Unpackerr) PollFolders() {

// newFolderWatcher returns a new folder watcher.
// You must call folders.FSNotify.Close() when you're done with it.
func (u *Unpackerr) newFolderWatcher() (*Folders, error) {
w := watcher.New()
w.FilterOps(watcher.Rename, watcher.Move, watcher.Write, watcher.Create)
w.IgnoreHiddenFiles(true)
func (u *Unpackerr) newFolderWatcher() error {
u.folders = &Folders{
Interval: u.Folder.Interval.Duration,
Config: u.Folders,
Folders: make(map[string]*Folder),
Events: make(chan *eventData, u.Config.Buffer),
Updates: make(chan *xtractr.Response, updateChanBuf),
Debugf: u.Debugf,
Printf: u.Printf,
}

if len(u.Folders) == 0 {
return nil // do not initialize watcher
}

u.folders.Watcher = watcher.New()
u.folders.Watcher.FilterOps(watcher.Rename, watcher.Move, watcher.Write, watcher.Create)
u.folders.Watcher.IgnoreHiddenFiles(true)

fsn, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("fsnotify.NewWatcher: %w", err)
return fmt.Errorf("fsnotify.NewWatcher: %w", err)
}

u.folders.FSNotify = fsn

for _, folder := range u.Folders {
if err := w.Add(folder.Path); err != nil {
if err := u.folders.Watcher.Add(folder.Path); err != nil {
u.Printf("[ERROR] Folder '%s' (cannot poll): %v", folder.Path, err)
}

Expand All @@ -146,17 +162,7 @@ func (u *Unpackerr) newFolderWatcher() (*Folders, error) {
}
}

return &Folders{
Interval: u.Folder.Interval.Duration,
Config: u.Folders,
Folders: make(map[string]*Folder),
Events: make(chan *eventData, u.Config.Buffer),
Updates: make(chan *xtractr.Response, updateChanBuf),
Debugf: u.Debugf,
Printf: u.Printf,
FSNotify: fsn,
Watcher: w,
}, nil
return nil
}

// Add uses either fsnotify or watcher.
Expand Down