fix(library): a file back at a waiting track's path brings it back at once
release / go (push) Successful in 1m40s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m50s
release / android (push) Successful in 4m29s
release / Build signed APK (push) Successful in 4m45s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 42s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 1m40s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m50s
release / android (push) Successful in 4m29s
release / Build signed APK (push) Successful in 4m45s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 42s
release / Verify release artifacts (tag releases only) (push) Skipped
The watcher scans single files and never reconciles, and a file restored with its old mtime takes the unchanged skip, so a track whose file came back at the same path waited in Needs a file for the next full scan. The scan now clears the mark when the path it reads belongs to a waiting track, and tells clients it plays again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -257,6 +257,22 @@ func (s *Scanner) enumerate(
|
|||||||
return paths, errs
|
return paths, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clearReturned takes the missing mark (and any removal reason) off a track
|
||||||
|
// whose file is back at its path, and tells clients it plays again. Counted
|
||||||
|
// as Restored, like the reconcile pass's clears.
|
||||||
|
func (s *Scanner) clearReturned(ctx context.Context, q *dbq.Queries, id pgtype.UUID, stats *Stats) error {
|
||||||
|
ids := []pgtype.UUID{id}
|
||||||
|
if err := logTrackChanges(ctx, q, ids); err != nil {
|
||||||
|
return fmt.Errorf("log returned change: %w", err)
|
||||||
|
}
|
||||||
|
n, err := q.ClearTracksMissing(ctx, ids)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("clear missing mark: %w", err)
|
||||||
|
}
|
||||||
|
stats.Restored += int(n)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// scanFile upserts a single audio file. Returns the album ID the track
|
// scanFile upserts a single audio file. Returns the album ID the track
|
||||||
// belongs to and whether the file was added/updated (false = skipped as
|
// belongs to and whether the file was added/updated (false = skipped as
|
||||||
// unchanged), so watcher-driven callers can enrich just the changed albums.
|
// unchanged), so watcher-driven callers can enrich just the changed albums.
|
||||||
@@ -276,6 +292,16 @@ func (s *Scanner) scanFile(
|
|||||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||||
return pgtype.UUID{}, false, fmt.Errorf("lookup: %w", err)
|
return pgtype.UUID{}, false, fmt.Errorf("lookup: %w", err)
|
||||||
}
|
}
|
||||||
|
// A file at the path of a track waiting for one is that track's file
|
||||||
|
// back. The full scan's reconcile clears these too, but the watcher scans
|
||||||
|
// single files and never reconciles, and a file restored with its old
|
||||||
|
// mtime takes the skip below, so without this the track would wait for the
|
||||||
|
// next full scan.
|
||||||
|
if knownTrack && existing.MissingSince.Valid {
|
||||||
|
if err := s.clearReturned(ctx, q, existing.ID, stats); err != nil {
|
||||||
|
return pgtype.UUID{}, false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
// Incremental skip: only when the file hasn't changed AND we already have a
|
// Incremental skip: only when the file hasn't changed AND we already have a
|
||||||
// real duration AND the row's tag-derived columns were written by the
|
// real duration AND the row's tag-derived columns were written by the
|
||||||
// current extraction logic. The duration clause backfills rows that
|
// current extraction logic. The duration clause backfills rows that
|
||||||
@@ -497,10 +523,11 @@ func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if stats.Added > 0 || stats.Updated > 0 {
|
if stats.Added > 0 || stats.Updated > 0 || stats.Restored > 0 {
|
||||||
s.logger.Info("library watch: scan batch",
|
s.logger.Info("library watch: scan batch",
|
||||||
"added", stats.Added,
|
"added", stats.Added,
|
||||||
"updated", stats.Updated,
|
"updated", stats.Updated,
|
||||||
|
"restored", stats.Restored,
|
||||||
"skipped", stats.Skipped,
|
"skipped", stats.Skipped,
|
||||||
"errored", stats.Errored,
|
"errored", stats.Errored,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||||
@@ -485,3 +486,65 @@ func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing.
|
|||||||
t.Errorf("after the re-read, albums by group = %v, want the green track on its own album", got)
|
t.Errorf("after the re-read, albums by group = %v, want the green track on its own album", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration: a file
|
||||||
|
// put back at the path of a track waiting for one (an admin removed it, or it
|
||||||
|
// went missing) brings the track back as soon as the watcher scans it, even
|
||||||
|
// with its old mtime, which takes the unchanged-file skip. The track keeps its
|
||||||
|
// id, and clients are told it plays again.
|
||||||
|
func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) {
|
||||||
|
pool := newPool(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||||
|
|
||||||
|
root := t.TempDir()
|
||||||
|
path := filepath.Join(root, "Artist R/Album R/03 - Returning.mp3")
|
||||||
|
writeTestMP3(t, path, map[string]string{
|
||||||
|
"TIT2": "Returning", "TPE1": "Artist R", "TALB": "Album R", "TRCK": "3",
|
||||||
|
})
|
||||||
|
scanner := New(pool, logger, []string{root}, nil)
|
||||||
|
if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil {
|
||||||
|
t.Fatalf("first scan: %v", err)
|
||||||
|
}
|
||||||
|
q := dbq.New(pool)
|
||||||
|
before, err := q.GetTrackByPath(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("track not indexed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removed from Reports: the row waits, and the file is back unchanged.
|
||||||
|
if _, err := q.MarkTracksRemoved(ctx, dbq.MarkTracksRemovedParams{
|
||||||
|
Reason: string(RemovedForReport), Ids: []pgtype.UUID{before.ID},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("mark removed: %v", err)
|
||||||
|
}
|
||||||
|
var changesBefore int
|
||||||
|
if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesBefore); err != nil {
|
||||||
|
t.Fatalf("count changes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil {
|
||||||
|
t.Fatalf("watcher scan: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
after, err := q.GetTrackByID(ctx, before.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("track gone: %v", err)
|
||||||
|
}
|
||||||
|
if after.MissingSince.Valid {
|
||||||
|
t.Errorf("track still waits for a file it has: missing_since=%v", after.MissingSince.Time)
|
||||||
|
}
|
||||||
|
if after.RemovedReason != nil {
|
||||||
|
t.Errorf("removed_reason = %q, want none once the file is back", *after.RemovedReason)
|
||||||
|
}
|
||||||
|
if after.FilePath != path {
|
||||||
|
t.Errorf("file_path = %q, want %q", after.FilePath, path)
|
||||||
|
}
|
||||||
|
var changesAfter int
|
||||||
|
if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesAfter); err != nil {
|
||||||
|
t.Fatalf("count changes: %v", err)
|
||||||
|
}
|
||||||
|
if changesAfter <= changesBefore {
|
||||||
|
t.Error("no library change logged, so clients would still show the track as unplayable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user