Temporary fix for scan context cancellation for Go 1.20

This commit is contained in:
Deluan 2023-12-15 07:59:34 -05:00
parent 70effa09e8
commit d3f6b4692d
3 changed files with 66 additions and 22 deletions

33
scanner/rescanall.go Normal file
View File

@ -0,0 +1,33 @@
//go:build go1.21
package scanner
import (
"context"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/log"
)
// TODO: Move this to scanner/scanner.go when we drop support for go 1.20
func (s *scanner) RescanAll(ctx context.Context, fullRescan bool) error {
ctx = context.WithoutCancel(ctx)
if !isScanning.TryLock() {
log.Debug("Scanner already running, ignoring request for rescan.")
return ErrAlreadyScanning
}
defer isScanning.Unlock()
var hasError bool
for folder := range s.folders {
err := s.rescan(ctx, folder, fullRescan)
hasError = hasError || err != nil
}
if hasError {
log.Error("Errors while scanning media. Please check the logs")
core.WriteAfterScanMetrics(ctx, s.ds, false)
return ErrScanError
}
core.WriteAfterScanMetrics(ctx, s.ds, true)
return nil
}

View File

@ -0,0 +1,33 @@
//go:build !go1.21
package scanner
import (
"context"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/log"
)
// TODO Remove this file when we drop support for go 1.20
func (s *scanner) RescanAll(ctx context.Context, fullRescan bool) error {
ctx = context.TODO()
if !isScanning.TryLock() {
log.Debug("Scanner already running, ignoring request for rescan.")
return ErrAlreadyScanning
}
defer isScanning.Unlock()
var hasError bool
for folder := range s.folders {
err := s.rescan(ctx, folder, fullRescan)
hasError = hasError || err != nil
}
if hasError {
log.Error("Errors while scanning media. Please check the logs")
core.WriteAfterScanMetrics(ctx, s.ds, false)
return ErrScanError
}
core.WriteAfterScanMetrics(ctx, s.ds, true)
return nil
}

View File

@ -139,28 +139,6 @@ func (s *scanner) startProgressTracker(mediaFolder string) (chan uint32, context
return progress, cancel
}
func (s *scanner) RescanAll(ctx context.Context, fullRescan bool) error {
ctx = context.WithoutCancel(ctx)
if !isScanning.TryLock() {
log.Debug("Scanner already running, ignoring request for rescan.")
return ErrAlreadyScanning
}
defer isScanning.Unlock()
var hasError bool
for folder := range s.folders {
err := s.rescan(ctx, folder, fullRescan)
hasError = hasError || err != nil
}
if hasError {
log.Error("Errors while scanning media. Please check the logs")
core.WriteAfterScanMetrics(ctx, s.ds, false)
return ErrScanError
}
core.WriteAfterScanMetrics(ctx, s.ds, true)
return nil
}
func (s *scanner) getStatus(folder string) (scanStatus, bool) {
s.lock.RLock()
defer s.lock.RUnlock()