Skip to content

Commit

Permalink
Temporary fix for scan context cancellation for Go 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 15, 2023
1 parent 70effa0 commit d3f6b46
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
33 changes: 33 additions & 0 deletions scanner/rescanall.go
@@ -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
}
33 changes: 33 additions & 0 deletions scanner/rescanall_go1.20.go
@@ -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
}
22 changes: 0 additions & 22 deletions scanner/scanner.go
Expand Up @@ -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()
Expand Down

0 comments on commit d3f6b46

Please sign in to comment.