Files
baa33768d6 fix(scanner): stop reporting an unusable ffprobe as an empty folder (#468)
* fix(scanner): stop reporting an unusable ffprobe as an empty folder

parseAudiobookFolder and parsePodcastShow signalled "this folder holds no
audio files" by wrapping os.ErrNotExist, and their reconcile callers skipped
on that. exec also wraps fs.ErrNotExist when the configured ffprobe binary
cannot be run, so a wrong playback.ffmpeg_path made every candidate folder
look empty: the scan logged processed=N failed=0, indexed nothing, and gave
the operator no clue why the library stayed empty.

Introduce an errFolderHasNoMedia sentinel that deliberately does not wrap
os.ErrNotExist, and skip on that instead. A folder that disappears between
the scan walk and the parse still maps to the sentinel, so a mid-scan rename
or delete stays a quiet skip rather than a scan failure.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(scanner): bound the all-failed scan summary instead of joining every failure

The sentinel change in this PR makes a previously-unreachable path reachable.
Before it, a misconfigured ffprobe made every candidate folder look empty, so
the scan skipped everything and `failed` stayed 0 — the `failedCount ==
processedCount` branch never fired. Now that an unusable ffprobe propagates as
a real failure, that branch is the expected outcome of a first scan with a bad
`playback.ffmpeg_path`, and it joins one wrapped error per failed folder.

On the 240k-folder library the scan code is written for, `errors.Join` over
that slice produces a ~64 MB error string (measured) that is written verbatim
into `scan_runs.error_message` and republished over the Redis events channel
and the admin SSE stream. The `failures` slice itself also grew unbounded for
the whole scan even when the all-failed guard could not fire (any rescan with
`skipped > 0`), holding hundreds of megabytes across a multi-hour scan before
discarding it.

Add a `scanFailures` collector that retains the first 20 failures and counts
the rest, joining them with a trailing "and N more failures (elided)". The
retained sample still names the cause, which is the entire purpose of the
summary. The same 64 MB case now produces 5.4 KB.

The ebook and manga scans have the identical shape and the same exposure via
their own probe failures, so all four call sites share the collector rather
than fixing the two audio paths alone. `failMu` now guards only `cancelErr`
and is renamed `cancelMu` to match.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
2026-07-26 23:35:36 -04:00

74 lines
2.7 KiB
Go

package scanner
import (
"errors"
"fmt"
"sync"
)
// errFolderHasNoMedia signals that a candidate folder contained zero media
// files of the kind its parser looks for. Folder-scoped parsers
// (parseAudiobookFolder, parsePodcastShow) return it so their reconcile
// callers can skip the folder quietly.
//
// It deliberately does NOT wrap os.ErrNotExist. The folder parsers shell out
// to ffprobe, and a missing or misconfigured ffprobe binary surfaces as an
// exec error that wraps fs.ErrNotExist ("fork/exec /path/ffprobe: no such
// file or directory"). Skipping on os.ErrNotExist therefore swallowed a
// server misconfiguration as "this folder has no audio", leaving scans that
// reported processed=N failed=0 while indexing nothing at all.
var errFolderHasNoMedia = errors.New("folder contains no media files")
// maxRetainedScanFailures caps how many per-item errors a scan keeps for its
// "everything failed" summary. A misconfigured ffprobe fails every candidate,
// so on a library with hundreds of thousands of folders an uncapped slice
// accumulates hundreds of megabytes of near-identical strings for the whole
// scan, and joining them produces a single error that is written verbatim into
// scan_runs.error_message and republished over the events channel and the
// admin SSE stream. The first few failures identify the cause; the rest only
// repeat it.
const maxRetainedScanFailures = 20
// scanFailures collects per-item scan errors for the all-failed summary while
// retaining at most maxRetainedScanFailures of them. It is safe for concurrent
// use by the scan worker pools.
type scanFailures struct {
mu sync.Mutex
retained []error
total int
}
// addf records a formatted failure. The error is only formatted while under
// the cap, so the common runaway case — every candidate failing for the same
// reason — does not pay for building strings it will discard.
func (f *scanFailures) addf(format string, args ...any) {
f.mu.Lock()
defer f.mu.Unlock()
f.total++
if len(f.retained) < maxRetainedScanFailures {
f.retained = append(f.retained, fmt.Errorf(format, args...))
}
}
// len reports how many failures were recorded, including elided ones.
func (f *scanFailures) len() int {
f.mu.Lock()
defer f.mu.Unlock()
return f.total
}
// join returns the retained failures as one error, with a trailing note when
// failures were elided. Returns nil when nothing was recorded.
func (f *scanFailures) join() error {
f.mu.Lock()
defer f.mu.Unlock()
if f.total == 0 {
return nil
}
if elided := f.total - len(f.retained); elided > 0 {
return errors.Join(append(append([]error(nil), f.retained...),
fmt.Errorf("and %d more failures (elided)", elided))...)
}
return errors.Join(f.retained...)
}