mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-05 05:27:37 +03:00
Replace periodic scanner cancellation channel with a context
This commit is contained in:
parent
9520c30c32
commit
c5686c4884
2 changed files with 10 additions and 17 deletions
10
cmd/root.go
10
cmd/root.go
|
@ -1,6 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
@ -79,21 +80,20 @@ func startScanner() (func() error, func(err error)) {
|
||||||
interval := conf.Server.ScanInterval
|
interval := conf.Server.ScanInterval
|
||||||
log.Info("Starting scanner", "interval", interval.String())
|
log.Info("Starting scanner", "interval", interval.String())
|
||||||
scanner := GetScanner()
|
scanner := GetScanner()
|
||||||
done := make(chan struct{})
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
return func() error {
|
return func() error {
|
||||||
if interval != 0 {
|
if interval != 0 {
|
||||||
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
||||||
scanner.Start(interval)
|
scanner.Run(ctx, interval)
|
||||||
} else {
|
} else {
|
||||||
log.Warn("Periodic scan is DISABLED", "interval", interval)
|
log.Warn("Periodic scan is DISABLED", "interval", interval)
|
||||||
|
<-ctx.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
<-done
|
|
||||||
return nil
|
return nil
|
||||||
}, func(err error) {
|
}, func(err error) {
|
||||||
scanner.Stop()
|
cancel()
|
||||||
done <- struct{}{}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Shutting down Scanner due to error", err)
|
log.Error("Shutting down Scanner due to error", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -16,8 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Scanner interface {
|
type Scanner interface {
|
||||||
Start(interval time.Duration)
|
Run(ctx context.Context, interval time.Duration)
|
||||||
Stop()
|
|
||||||
RescanAll(ctx context.Context, fullRescan bool) error
|
RescanAll(ctx context.Context, fullRescan bool) error
|
||||||
Status(mediaFolder string) (*StatusInfo, error)
|
Status(mediaFolder string) (*StatusInfo, error)
|
||||||
Scanning() bool
|
Scanning() bool
|
||||||
|
@ -49,7 +48,6 @@ type scanner struct {
|
||||||
ds model.DataStore
|
ds model.DataStore
|
||||||
cacheWarmer core.CacheWarmer
|
cacheWarmer core.CacheWarmer
|
||||||
broker events.Broker
|
broker events.Broker
|
||||||
done chan bool
|
|
||||||
scan chan bool
|
scan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,34 +66,29 @@ func New(ds model.DataStore, cacheWarmer core.CacheWarmer, broker events.Broker)
|
||||||
folders: map[string]FolderScanner{},
|
folders: map[string]FolderScanner{},
|
||||||
status: map[string]*scanStatus{},
|
status: map[string]*scanStatus{},
|
||||||
lock: &sync.RWMutex{},
|
lock: &sync.RWMutex{},
|
||||||
done: make(chan bool),
|
|
||||||
scan: make(chan bool),
|
scan: make(chan bool),
|
||||||
}
|
}
|
||||||
s.loadFolders()
|
s.loadFolders()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scanner) Start(interval time.Duration) {
|
func (s *scanner) Run(ctx context.Context, interval time.Duration) {
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
for {
|
for {
|
||||||
err := s.RescanAll(context.Background(), false)
|
err := s.RescanAll(ctx, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
continue
|
continue
|
||||||
case <-s.done:
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scanner) Stop() {
|
|
||||||
s.done <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *scanner) rescan(ctx context.Context, mediaFolder string, fullRescan bool) error {
|
func (s *scanner) rescan(ctx context.Context, mediaFolder string, fullRescan bool) error {
|
||||||
folderScanner := s.folders[mediaFolder]
|
folderScanner := s.folders[mediaFolder]
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
@ -163,7 +156,7 @@ func (s *scanner) RescanAll(ctx context.Context, fullRescan bool) error {
|
||||||
isScanning.Set(true)
|
isScanning.Set(true)
|
||||||
defer isScanning.Set(false)
|
defer isScanning.Set(false)
|
||||||
|
|
||||||
defer s.cacheWarmer.Flush(context.Background())
|
defer s.cacheWarmer.Flush(ctx)
|
||||||
var hasError bool
|
var hasError bool
|
||||||
for folder := range s.folders {
|
for folder := range s.folders {
|
||||||
err := s.rescan(ctx, folder, fullRescan)
|
err := s.rescan(ctx, folder, fullRescan)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue