tube/app/watcher.go

71 lines
1.9 KiB
Go
Raw Normal View History

2019-07-03 22:44:31 +03:00
package app
import (
"path/filepath"
"strings"
2019-07-03 22:44:31 +03:00
"time"
fs "github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
2019-07-03 22:44:31 +03:00
)
// This is the amount of time to wait after changes before reacting to them.
// Debounce is done because moving files into the watched directories causes
// many rapid "Write" events to fire which would cause excessive Remove/Add
// method calls on the Library. To avoid this we accumulate the changes and
// only perform them once the events have stopped for this amount of time.
const debounceTimeout = time.Second * 5
// create, write, and chmod all require an add event
const addFlags = fs.Create | fs.Write | fs.Chmod
// remove, rename, write, and chmod all require a remove event
const removeFlags = fs.Remove | fs.Rename | fs.Write | fs.Chmod
// watch library paths and update Library with changes.
func startWatcher(a *App) {
timer := time.NewTimer(debounceTimeout)
addEvents := make(map[string]struct{})
removeEvents := make(map[string]struct{})
for {
select {
case e := <-a.Watcher.Events:
if strings.ContainsAny(e.Name, "#") || filepath.Ext(e.Name) != ".mp4" {
continue
}
log.Debugf("fsnotify event: %s", e)
2019-08-09 00:13:55 +03:00
if e.Op&removeFlags != 0 {
2019-07-03 22:44:31 +03:00
removeEvents[e.Name] = struct{}{}
}
2019-08-09 00:13:55 +03:00
if e.Op&addFlags != 0 {
2019-07-03 22:44:31 +03:00
addEvents[e.Name] = struct{}{}
}
// reset timer
timer.Reset(debounceTimeout)
case <-timer.C:
2019-08-08 14:04:39 +03:00
eventCount := len(removeEvents) + len(addEvents)
2019-07-03 22:44:31 +03:00
// handle remove events first
if len(removeEvents) > 0 {
for p := range removeEvents {
a.Library.Remove(p)
}
// clear map
removeEvents = make(map[string]struct{})
}
// then handle add events
if len(addEvents) > 0 {
for p := range addEvents {
a.Library.Add(p)
}
// clear map
addEvents = make(map[string]struct{})
}
2019-08-08 14:04:39 +03:00
if eventCount > 0 {
buildFeed(a)
}
2019-07-03 22:44:31 +03:00
// reset timer
timer.Reset(debounceTimeout)
}
}
}