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:
@@ -598,15 +598,15 @@ func (w *DuplicateResolveWorker) tickOnce(ctx context.Context) (waitForScan bool
|
||||
// A scan in flight has listed the files it will reconcile: a copy removed
|
||||
// now is marked present again when it does. A run past StuckScanThreshold
|
||||
// is a dead one.
|
||||
run, err := q.GetInFlightScanRun(ctx)
|
||||
running, err := scanRunning(ctx, q)
|
||||
switch {
|
||||
case err == nil && time.Since(run.StartedAt.Time) <= StuckScanThreshold:
|
||||
return true
|
||||
case err != nil && !errors.Is(err, pgx.ErrNoRows):
|
||||
case err != nil:
|
||||
if ctx.Err() == nil {
|
||||
w.logger.Warn("duplicate resolve: scan check failed", "err", err)
|
||||
}
|
||||
return false
|
||||
case running:
|
||||
return true
|
||||
}
|
||||
res, err := ResolveDuplicatesWith(ctx, w.pool, w.logger, w.dataDir, w.settings.Get().AutoResolve, w.src)
|
||||
if err != nil && ctx.Err() == nil {
|
||||
|
||||
@@ -80,13 +80,13 @@ func (s *Scanner) SetRoots(ctx context.Context, changes []RootChange) (RerootRes
|
||||
q := dbq.New(s.pool)
|
||||
// A scan walking the old folders would put their paths back, or mark the
|
||||
// moved rows missing when it reconciles.
|
||||
_, err = q.GetInFlightScanRun(ctx)
|
||||
if err == nil {
|
||||
return RerootResult{}, badRoots("A library scan is running; save again once it finishes.")
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
running, err := scanRunning(ctx, q)
|
||||
if err != nil {
|
||||
return RerootResult{}, fmt.Errorf("in-flight scan check: %w", err)
|
||||
}
|
||||
if running {
|
||||
return RerootResult{}, badRoots("A library scan is running; save again once it finishes.")
|
||||
}
|
||||
for _, m := range moves {
|
||||
if err := checkMove(ctx, q, m); err != nil {
|
||||
return RerootResult{}, err
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
// A scan the last shutdown cut off is closed at startup with the reason, so
|
||||
// it no longer reads as a scan in progress.
|
||||
func TestRecoverScanRuns_ClosesARunTheLastShutdownCutOff_Integration(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
ctx := context.Background()
|
||||
var id pgtype.UUID
|
||||
if err := pool.QueryRow(ctx,
|
||||
`INSERT INTO scan_runs (started_at) VALUES (now() - interval '5 minutes') RETURNING id`).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
RecoverScanRuns(ctx, pool, quietLogger())
|
||||
|
||||
var finished bool
|
||||
var reason *string
|
||||
if err := pool.QueryRow(ctx,
|
||||
`SELECT finished_at IS NOT NULL, error_message FROM scan_runs WHERE id = $1`, id).Scan(&finished, &reason); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !finished || reason == nil || *reason == "" {
|
||||
t.Errorf("finished %v, reason %v; want the run closed with why", finished, reason)
|
||||
}
|
||||
}
|
||||
|
||||
// Saving library folders waits for a scan that is running, but not for an
|
||||
// unfinished run older than StuckScanThreshold: that one is dead.
|
||||
func TestSetRoots_OnlyALiveScanHoldsUpSavingFolders_Integration(t *testing.T) {
|
||||
pool := triggerTestPool(t)
|
||||
clearSavedRoots(t, pool)
|
||||
ctx := context.Background()
|
||||
s := New(pool, quietLogger(), nil, nil)
|
||||
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now() - interval '2 hours')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); err != nil {
|
||||
t.Errorf("a dead scan blocked saving folders: %v", err)
|
||||
}
|
||||
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now())`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var re *RootsError
|
||||
if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); !errors.As(err, &re) {
|
||||
t.Errorf("err %v; want a refusal while a scan is running", err)
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user