Skip to content

Commit

Permalink
Better workaround for Go 1.20 missing context.WithoutCancel
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 16, 2023
1 parent d3f6b46 commit 0e3175e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
26 changes: 2 additions & 24 deletions scanner/rescanall.go
Expand Up @@ -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)
}
25 changes: 2 additions & 23 deletions scanner/rescanall_go1.20.go
Expand Up @@ -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()
}
21 changes: 21 additions & 0 deletions scanner/scanner.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 0e3175e

Please sign in to comment.