Skip to content

🎧 Your Personal Streaming Service

Update count on `getScanStatus`

Deluan8e584ee020b2
diff --git a/scanner/scanner.go b/scanner/scanner.go
index 991a4c457..28bc80605 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"sync"
+ "sync/atomic"
"time"
"github.com/deluan/navidrome/core"
@@ -25,11 +26,11 @@ type StatusInfo struct {
MediaFolder string
Scanning bool
LastScan time.Time
- Count int64
+ Count uint32
}
type FolderScanner interface {
- Scan(ctx context.Context, lastModifiedSince time.Time) error
+ Scan(ctx context.Context, lastModifiedSince time.Time, progress chan uint32) error
}
type scanner struct {
@@ -44,7 +45,7 @@ type scanner struct {
type scanStatus struct {
active bool
- count int64
+ count uint32
lastUpdate time.Time
}
@@ -63,22 +64,22 @@ func New(ds model.DataStore, cacheWarmer core.CacheWarmer) Scanner {
}
func (s *scanner) Start(interval time.Duration) error {
+ var ticker *time.Ticker
if interval == 0 {
log.Warn("Periodic scan is DISABLED", "interval", interval)
+ ticker = time.NewTicker(1 * time.Hour)
+ } else {
+ ticker = time.NewTicker(interval)
}
- var sleep <-chan time.Time
+ defer ticker.Stop()
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 <-ticker.C:
+ if interval != 0 {
+ s.rescanAll(false)
+ }
case <-s.done:
return nil
}
@@ -104,7 +105,19 @@ func (s *scanner) rescan(mediaFolder string, fullRescan bool) error {
s.setStatusActive(mediaFolder, true)
defer s.setStatus(mediaFolder, false, 0, start)
- err := folderScanner.Scan(log.NewContext(context.TODO()), lastModifiedSince)
+ progress := make(chan uint32, 10)
+ go func() {
+ for {
+ count, more := <-progress
+ if !more {
+ break
+ }
+ atomic.AddUint32(&s.status[mediaFolder].count, count)
+ }
+ }()
+
+ err := folderScanner.Scan(log.NewContext(context.TODO()), lastModifiedSince, progress)
+ close(progress)
if err != nil {
log.Error("Error importing MediaFolder", "folder", mediaFolder, err)
}
@@ -142,7 +155,7 @@ func (s *scanner) getStatus(folder string) *scanStatus {
return nil
}
-func (s *scanner) setStatus(folder string, active bool, count int64, lastUpdate time.Time) {
+func (s *scanner) setStatus(folder string, active bool, count uint32, lastUpdate time.Time) {
s.lock.Lock()
defer s.lock.Unlock()
if status, ok := s.status[folder]; ok {
diff --git a/scanner/tag_scanner.go b/scanner/tag_scanner.go
index c5dad1c33..722161d2c 100644
--- a/scanner/tag_scanner.go
+++ b/scanner/tag_scanner.go
@@ -67,7 +67,7 @@ const (
// If the playlist is not in the DB, import it, setting sync = true
// If the playlist is in the DB and sync == true, import it, or else skip it
// Delete all empty albums, delete all empty artists, clean-up playlists
-func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time) error {
+func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, progress chan uint32) error {
ctx = s.withAdminUser(ctx)
start := time.Now()
@@ -86,6 +86,7 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time) erro
if !more {
break
}
+ progress <- folderStats.AudioFilesCount
allFSDirs[folderStats.Path] = folderStats
if s.folderHasChanged(ctx, folderStats, allDBDirs, lastModifiedSince) {
diff --git a/scanner/walk_dir_tree.go b/scanner/walk_dir_tree.go
index 256255f44..bf57fb8d4 100644
--- a/scanner/walk_dir_tree.go
+++ b/scanner/walk_dir_tree.go
@@ -19,7 +19,7 @@ type (
ModTime time.Time
HasImages bool
HasPlaylist bool
- AudioFilesCount int64
+ AudioFilesCount uint32
}
walkResults = chan dirStats
)
diff --git a/server/subsonic/library_scanning.go b/server/subsonic/library_scanning.go
index 6f404985e..b3ea5caf8 100644
--- a/server/subsonic/library_scanning.go
+++ b/server/subsonic/library_scanning.go
@@ -31,7 +31,7 @@ func (c *LibraryScanningController) GetScanStatus(w http.ResponseWriter, r *http
response := newResponse()
response.ScanStatus = &responses.ScanStatus{
Scanning: status.Scanning,
- Count: status.Count,
+ Count: int64(status.Count),
LastScan: &status.LastScan,
}
return response, nil