Skip to content

Commit

Permalink
Merge pull request #251 from davidnewhall/dn2_folder_crash
Browse files Browse the repository at this point in the history
fix crash when folder watcher fails
  • Loading branch information
davidnewhall authored Jan 14, 2023
2 parents 3020c79 + 9a8e7dc commit 450b974
Showing 1 changed file with 24 additions and 18 deletions.
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

0 comments on commit 450b974

Please sign in to comment.