mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Add scanner status
This commit is contained in:
parent
c0ec0b28b9
commit
af7eaa2b7a
5 changed files with 170 additions and 52 deletions
|
@ -5,24 +5,88 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/deluan/navidrome/log"
|
||||
"github.com/deluan/navidrome/model"
|
||||
)
|
||||
|
||||
type Scanner struct {
|
||||
folders map[string]FolderScanner
|
||||
ds model.DataStore
|
||||
type Scanner interface {
|
||||
Start(interval time.Duration) error
|
||||
Stop()
|
||||
RescanAll(fullRescan bool)
|
||||
Status(mediaFolder string) (*StatusInfo, error)
|
||||
Scanning() bool
|
||||
}
|
||||
|
||||
func New(ds model.DataStore) *Scanner {
|
||||
s := &Scanner{ds: ds, folders: map[string]FolderScanner{}}
|
||||
type StatusInfo struct {
|
||||
MediaFolder string
|
||||
Scanning bool
|
||||
LastScan time.Time
|
||||
Count int
|
||||
}
|
||||
|
||||
type FolderScanner interface {
|
||||
Scan(ctx context.Context, lastModifiedSince time.Time) error
|
||||
}
|
||||
|
||||
type scanner struct {
|
||||
folders map[string]FolderScanner
|
||||
status map[string]*scanStatus
|
||||
lock *sync.RWMutex
|
||||
ds model.DataStore
|
||||
done chan bool
|
||||
scan chan bool
|
||||
}
|
||||
|
||||
type scanStatus struct {
|
||||
active bool
|
||||
count int
|
||||
lastUpdate time.Time
|
||||
}
|
||||
|
||||
func New(ds model.DataStore) Scanner {
|
||||
s := &scanner{
|
||||
ds: ds,
|
||||
folders: map[string]FolderScanner{},
|
||||
status: map[string]*scanStatus{},
|
||||
lock: &sync.RWMutex{},
|
||||
done: make(chan bool),
|
||||
scan: make(chan bool),
|
||||
}
|
||||
s.loadFolders()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Scanner) Rescan(mediaFolder string, fullRescan bool) error {
|
||||
func (s *scanner) Start(interval time.Duration) error {
|
||||
if interval == 0 {
|
||||
log.Warn("Periodic scan is DISABLED", "interval", interval)
|
||||
}
|
||||
var sleep <-chan time.Time
|
||||
for {
|
||||
if interval == 0 {
|
||||
sleep = make(chan time.Time)
|
||||
} else {
|
||||
sleep = time.After(interval)
|
||||
}
|
||||
select {
|
||||
case full := <-s.scan:
|
||||
s.rescanAll(full)
|
||||
case <-sleep:
|
||||
s.rescanAll(false)
|
||||
continue
|
||||
case <-s.done:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) Stop() {
|
||||
s.done <- true
|
||||
}
|
||||
|
||||
func (s *scanner) rescan(mediaFolder string, fullRescan bool) error {
|
||||
folderScanner := s.folders[mediaFolder]
|
||||
start := time.Now()
|
||||
|
||||
|
@ -34,6 +98,9 @@ func (s *Scanner) Rescan(mediaFolder string, fullRescan bool) error {
|
|||
log.Debug("Scanning folder (full scan)", "folder", mediaFolder)
|
||||
}
|
||||
|
||||
s.setStatusActive(mediaFolder, true)
|
||||
defer s.setStatus(mediaFolder, false, 0, start)
|
||||
|
||||
err := folderScanner.Scan(log.NewContext(context.TODO()), lastModifiedSince)
|
||||
if err != nil {
|
||||
log.Error("Error importing MediaFolder", "folder", mediaFolder, err)
|
||||
|
@ -43,22 +110,77 @@ func (s *Scanner) Rescan(mediaFolder string, fullRescan bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *Scanner) RescanAll(fullRescan bool) error {
|
||||
func (s *scanner) RescanAll(fullRescan bool) {
|
||||
if s.Scanning() {
|
||||
log.Debug("Scanner already running, ignoring request for rescan.")
|
||||
return
|
||||
}
|
||||
s.scan <- fullRescan
|
||||
}
|
||||
|
||||
func (s *scanner) rescanAll(fullRescan bool) {
|
||||
var hasError bool
|
||||
for folder := range s.folders {
|
||||
err := s.Rescan(folder, fullRescan)
|
||||
err := s.rescan(folder, fullRescan)
|
||||
hasError = hasError || err != nil
|
||||
}
|
||||
if hasError {
|
||||
log.Error("Errors while scanning media. Please check the logs")
|
||||
return errors.New("errors while scanning media")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) getStatus(folder string) *scanStatus {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
if status, ok := s.status[folder]; ok {
|
||||
return status
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scanner) Status() []StatusInfo { return nil }
|
||||
func (s *scanner) setStatus(folder string, active bool, count int, lastUpdate time.Time) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
if status, ok := s.status[folder]; ok {
|
||||
status.active = active
|
||||
status.count = count
|
||||
status.lastUpdate = lastUpdate
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scanner) getLastModifiedSince(folder string) time.Time {
|
||||
func (s *scanner) setStatusActive(folder string, active bool) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
if status, ok := s.status[folder]; ok {
|
||||
status.active = active
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) Scanning() bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
for _, status := range s.status {
|
||||
if status.active {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *scanner) Status(mediaFolder string) (*StatusInfo, error) {
|
||||
status := s.getStatus(mediaFolder)
|
||||
if status == nil {
|
||||
return nil, errors.New("mediaFolder not found")
|
||||
}
|
||||
return &StatusInfo{
|
||||
MediaFolder: mediaFolder,
|
||||
Scanning: status.active,
|
||||
LastScan: status.lastUpdate,
|
||||
Count: status.count,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *scanner) getLastModifiedSince(folder string) time.Time {
|
||||
ms, err := s.ds.Property(context.TODO()).Get(model.PropLastScan + "-" + folder)
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
|
@ -70,32 +192,26 @@ func (s *Scanner) getLastModifiedSince(folder string) time.Time {
|
|||
return time.Unix(0, i*int64(time.Millisecond))
|
||||
}
|
||||
|
||||
func (s *Scanner) updateLastModifiedSince(folder string, t time.Time) {
|
||||
func (s *scanner) updateLastModifiedSince(folder string, t time.Time) {
|
||||
millis := t.UnixNano() / int64(time.Millisecond)
|
||||
if err := s.ds.Property(context.TODO()).Put(model.PropLastScan+"-"+folder, fmt.Sprint(millis)); err != nil {
|
||||
log.Error("Error updating DB after scan", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scanner) loadFolders() {
|
||||
func (s *scanner) loadFolders() {
|
||||
fs, _ := s.ds.MediaFolder(context.TODO()).GetAll()
|
||||
for _, f := range fs {
|
||||
log.Info("Configuring Media Folder", "name", f.Name, "path", f.Path)
|
||||
s.folders[f.Path] = s.newScanner(f)
|
||||
s.status[f.Path] = &scanStatus{
|
||||
active: false,
|
||||
count: 0,
|
||||
lastUpdate: s.getLastModifiedSince(f.Path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scanner) newScanner(f model.MediaFolder) FolderScanner {
|
||||
func (s *scanner) newScanner(f model.MediaFolder) FolderScanner {
|
||||
return NewTagScanner(f.Path, s.ds)
|
||||
}
|
||||
|
||||
type Status int
|
||||
|
||||
type StatusInfo struct {
|
||||
MediaFolder string
|
||||
Status Status
|
||||
}
|
||||
|
||||
type FolderScanner interface {
|
||||
Scan(ctx context.Context, lastModifiedSince time.Time) error
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue