fix(library): a scan cut off by a restart no longer blocks saving library folders

A restart mid-scan left its scan_runs row unfinished, and saving library
folders refused ("A library scan is running") until a later scan reaped it,
up to 12 hours on. Startup now closes such rows with the reason before the
boot scan, as backups already do. Saving folders and the duplicate sweep
share one check that treats a run past StuckScanThreshold as dead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 22:02:00 -05:00
co-authored by Claude Opus 5.5
parent 1ff4bc8258
commit 785b15ab5f
7 changed files with 123 additions and 10 deletions
+27
View File
@@ -20,6 +20,33 @@ import (
// duration so a normally-running scan is never reaped accidentally.
const StuckScanThreshold = 1 * time.Hour
// RecoverScanRuns closes the scan runs a stopped server left unfinished. It
// runs once at startup, before the first scan: none can be running yet.
func RecoverScanRuns(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) {
n, err := dbq.New(pool).FailUnfinishedScanRuns(ctx)
if err != nil {
logger.Warn("scan: recover unfinished runs", "err", err)
return
}
if n > 0 {
logger.Warn("scan: a run was cut off by the last shutdown", "runs", n)
}
}
// scanRunning reports whether a library scan is running now: an unfinished
// run that started within StuckScanThreshold. An older unfinished run is a
// dead one, and doesn't hold anything up.
func scanRunning(ctx context.Context, q *dbq.Queries) (bool, error) {
run, err := q.GetInFlightScanRun(ctx)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
if err != nil {
return false, err
}
return time.Since(run.StartedAt.Time) <= StuckScanThreshold, nil
}
// TryStartScan attempts to start a new scan, with built-in handling for
// stuck/stale in-flight rows.
//