mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 21:17:37 +03:00
Add scanner status
This commit is contained in:
parent
c0ec0b28b9
commit
af7eaa2b7a
5 changed files with 170 additions and 52 deletions
30
cmd/root.go
30
cmd/root.go
|
@ -53,11 +53,8 @@ func runNavidrome() {
|
||||||
|
|
||||||
var g run.Group
|
var g run.Group
|
||||||
g.Add(startServer())
|
g.Add(startServer())
|
||||||
if conf.Server.ScanInterval != 0 {
|
g.Add(startScanner())
|
||||||
g.Add(startScanner())
|
|
||||||
} else {
|
|
||||||
log.Warn("Scanner is disabled", "interval", conf.Server.ScanInterval)
|
|
||||||
}
|
|
||||||
if err := g.Run(); err != nil {
|
if err := g.Run(); err != nil {
|
||||||
log.Error("Fatal error in Navidrome. Aborting", err)
|
log.Error("Fatal error in Navidrome. Aborting", err)
|
||||||
}
|
}
|
||||||
|
@ -83,25 +80,16 @@ func startScanner() (func() error, func(err error)) {
|
||||||
log.Info("Starting scanner", "interval", interval.String())
|
log.Info("Starting scanner", "interval", interval.String())
|
||||||
scanner := CreateScanner(conf.Server.MusicFolder)
|
scanner := CreateScanner(conf.Server.MusicFolder)
|
||||||
|
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
done := make(chan bool)
|
|
||||||
|
|
||||||
return func() error {
|
return func() error {
|
||||||
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
if interval != 0 {
|
||||||
for {
|
go func() {
|
||||||
if err := scanner.RescanAll(false); err != nil {
|
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
||||||
log.Error("Error scanning media folder", "folder", conf.Server.MusicFolder, err)
|
scanner.RescanAll(false)
|
||||||
}
|
}()
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
continue
|
|
||||||
case <-done:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return scanner.Start(interval)
|
||||||
}, func(err error) {
|
}, func(err error) {
|
||||||
ticker.Stop()
|
scanner.Stop()
|
||||||
done <- true
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fatal error executing Scanner", err)
|
log.Error("Fatal error executing Scanner", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
22
cmd/scan.go
22
cmd/scan.go
|
@ -1,8 +1,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/conf"
|
"github.com/deluan/navidrome/conf"
|
||||||
"github.com/deluan/navidrome/log"
|
"github.com/deluan/navidrome/log"
|
||||||
|
"github.com/deluan/navidrome/scanner"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,12 +25,23 @@ var scanCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func waitScanToFinish(scanner scanner.Scanner) {
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
ticker := time.Tick(100 * time.Millisecond)
|
||||||
|
for {
|
||||||
|
if !scanner.Scanning() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<-ticker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runScanner() {
|
func runScanner() {
|
||||||
scanner := CreateScanner(conf.Server.MusicFolder)
|
scanner := CreateScanner(conf.Server.MusicFolder)
|
||||||
err := scanner.RescanAll(fullRescan)
|
go func() { _ = scanner.Start(0) }()
|
||||||
if err != nil {
|
scanner.RescanAll(fullRescan)
|
||||||
log.Error("Error scanning media folder", "folder", conf.Server.MusicFolder, err)
|
waitScanToFinish(scanner)
|
||||||
}
|
scanner.Stop()
|
||||||
if fullRescan {
|
if fullRescan {
|
||||||
log.Info("Finished full rescan")
|
log.Info("Finished full rescan")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -25,7 +25,7 @@ func CreateServer(musicFolder string) *server.Server {
|
||||||
return serverServer
|
return serverServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateScanner(musicFolder string) *scanner.Scanner {
|
func CreateScanner(musicFolder string) scanner.Scanner {
|
||||||
dataStore := persistence.New()
|
dataStore := persistence.New()
|
||||||
scannerScanner := scanner.New(dataStore)
|
scannerScanner := scanner.New(dataStore)
|
||||||
return scannerScanner
|
return scannerScanner
|
||||||
|
|
|
@ -29,7 +29,7 @@ func CreateServer(musicFolder string) *server.Server {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateScanner(musicFolder string) *scanner.Scanner {
|
func CreateScanner(musicFolder string) scanner.Scanner {
|
||||||
panic(wire.Build(
|
panic(wire.Build(
|
||||||
allProviders,
|
allProviders,
|
||||||
))
|
))
|
||||||
|
|
|
@ -5,24 +5,88 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/log"
|
"github.com/deluan/navidrome/log"
|
||||||
"github.com/deluan/navidrome/model"
|
"github.com/deluan/navidrome/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scanner struct {
|
type Scanner interface {
|
||||||
folders map[string]FolderScanner
|
Start(interval time.Duration) error
|
||||||
ds model.DataStore
|
Stop()
|
||||||
|
RescanAll(fullRescan bool)
|
||||||
|
Status(mediaFolder string) (*StatusInfo, error)
|
||||||
|
Scanning() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ds model.DataStore) *Scanner {
|
type StatusInfo struct {
|
||||||
s := &Scanner{ds: ds, folders: map[string]FolderScanner{}}
|
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()
|
s.loadFolders()
|
||||||
return s
|
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]
|
folderScanner := s.folders[mediaFolder]
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
@ -34,6 +98,9 @@ func (s *Scanner) Rescan(mediaFolder string, fullRescan bool) error {
|
||||||
log.Debug("Scanning folder (full scan)", "folder", mediaFolder)
|
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)
|
err := folderScanner.Scan(log.NewContext(context.TODO()), lastModifiedSince)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error importing MediaFolder", "folder", mediaFolder, err)
|
log.Error("Error importing MediaFolder", "folder", mediaFolder, err)
|
||||||
|
@ -43,22 +110,77 @@ func (s *Scanner) Rescan(mediaFolder string, fullRescan bool) error {
|
||||||
return err
|
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
|
var hasError bool
|
||||||
for folder := range s.folders {
|
for folder := range s.folders {
|
||||||
err := s.Rescan(folder, fullRescan)
|
err := s.rescan(folder, fullRescan)
|
||||||
hasError = hasError || err != nil
|
hasError = hasError || err != nil
|
||||||
}
|
}
|
||||||
if hasError {
|
if hasError {
|
||||||
log.Error("Errors while scanning media. Please check the logs")
|
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
|
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)
|
ms, err := s.ds.Property(context.TODO()).Get(model.PropLastScan + "-" + folder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}
|
return time.Time{}
|
||||||
|
@ -70,32 +192,26 @@ func (s *Scanner) getLastModifiedSince(folder string) time.Time {
|
||||||
return time.Unix(0, i*int64(time.Millisecond))
|
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)
|
millis := t.UnixNano() / int64(time.Millisecond)
|
||||||
if err := s.ds.Property(context.TODO()).Put(model.PropLastScan+"-"+folder, fmt.Sprint(millis)); err != nil {
|
if err := s.ds.Property(context.TODO()).Put(model.PropLastScan+"-"+folder, fmt.Sprint(millis)); err != nil {
|
||||||
log.Error("Error updating DB after scan", err)
|
log.Error("Error updating DB after scan", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) loadFolders() {
|
func (s *scanner) loadFolders() {
|
||||||
fs, _ := s.ds.MediaFolder(context.TODO()).GetAll()
|
fs, _ := s.ds.MediaFolder(context.TODO()).GetAll()
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
log.Info("Configuring Media Folder", "name", f.Name, "path", f.Path)
|
log.Info("Configuring Media Folder", "name", f.Name, "path", f.Path)
|
||||||
s.folders[f.Path] = s.newScanner(f)
|
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)
|
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