From 0e3175ea17acc160fb0fd7b6744ffb4a2f4f357e Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 16 Dec 2023 13:33:03 -0500 Subject: [PATCH] Better workaround for Go 1.20 missing context.WithoutCancel --- scanner/rescanall.go | 26 ++------------------------ scanner/rescanall_go1.20.go | 25 ++----------------------- scanner/scanner.go | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/scanner/rescanall.go b/scanner/rescanall.go index 4a8bc8d18..a48247e38 100644 --- a/scanner/rescanall.go +++ b/scanner/rescanall.go @@ -4,30 +4,8 @@ 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 +func contextWithoutCancel(ctx context.Context) context.Context { + return context.WithoutCancel(ctx) } diff --git a/scanner/rescanall_go1.20.go b/scanner/rescanall_go1.20.go index d64a8d974..e4b6b2d1a 100644 --- a/scanner/rescanall_go1.20.go +++ b/scanner/rescanall_go1.20.go @@ -4,30 +4,9 @@ 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 +func contextWithoutCancel(ctx context.Context) context.Context { + return context.TODO() } diff --git a/scanner/scanner.go b/scanner/scanner.go index 9d43db209..f3eb48542 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -177,6 +177,27 @@ func (s *scanner) setStatusEnd(folder string, lastUpdate time.Time) { } } +func (s *scanner) RescanAll(ctx context.Context, fullRescan bool) error { + ctx = contextWithoutCancel(ctx) + if !isScanning.TryLock() { + log.Debug(ctx, "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(ctx, "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) Status(mediaFolder string) (*StatusInfo, error) { status, ok := s.getStatus(mediaFolder) if !ok {