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>
189 lines
5.2 KiB
Go
189 lines
5.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: scan_runs.sql
|
|
|
|
package dbq
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const failUnfinishedScanRuns = `-- name: FailUnfinishedScanRuns :execrows
|
|
UPDATE scan_runs
|
|
SET finished_at = now(),
|
|
error_message = 'The server stopped while the scan was running.'
|
|
WHERE finished_at IS NULL
|
|
`
|
|
|
|
// At startup no scan can be running yet, so a row still unfinished was cut
|
|
// off when the server last stopped. Closing it keeps it from reading as a
|
|
// scan in progress, which would block saving library folders.
|
|
func (q *Queries) FailUnfinishedScanRuns(ctx context.Context) (int64, error) {
|
|
result, err := q.db.Exec(ctx, failUnfinishedScanRuns)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const finishScanRun = `-- name: FinishScanRun :exec
|
|
UPDATE scan_runs
|
|
SET finished_at = now(),
|
|
error_message = NULLIF($2::text, '')
|
|
WHERE id = $1
|
|
`
|
|
|
|
type FinishScanRunParams struct {
|
|
ID pgtype.UUID
|
|
Column2 string
|
|
}
|
|
|
|
// Closes the row. error_message is non-empty only when a fatal error
|
|
// aborted the chain before normal completion.
|
|
func (q *Queries) FinishScanRun(ctx context.Context, arg FinishScanRunParams) error {
|
|
_, err := q.db.Exec(ctx, finishScanRun, arg.ID, arg.Column2)
|
|
return err
|
|
}
|
|
|
|
const getInFlightScanRun = `-- name: GetInFlightScanRun :one
|
|
SELECT id, started_at
|
|
FROM scan_runs
|
|
WHERE finished_at IS NULL
|
|
ORDER BY started_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetInFlightScanRunRow struct {
|
|
ID pgtype.UUID
|
|
StartedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Used by POST /api/admin/scan/run to 409 when a scan is already running.
|
|
// "In flight" = finished_at IS NULL.
|
|
func (q *Queries) GetInFlightScanRun(ctx context.Context) (GetInFlightScanRunRow, error) {
|
|
row := q.db.QueryRow(ctx, getInFlightScanRun)
|
|
var i GetInFlightScanRunRow
|
|
err := row.Scan(&i.ID, &i.StartedAt)
|
|
return i, err
|
|
}
|
|
|
|
const getLatestScanRun = `-- name: GetLatestScanRun :one
|
|
SELECT id, started_at, finished_at, library, mbid_backfill, cover_enrich, artist_art_enrich, error_message
|
|
FROM scan_runs
|
|
ORDER BY started_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetLatestScanRunRow struct {
|
|
ID pgtype.UUID
|
|
StartedAt pgtype.Timestamptz
|
|
FinishedAt pgtype.Timestamptz
|
|
Library []byte
|
|
MbidBackfill []byte
|
|
CoverEnrich []byte
|
|
ArtistArtEnrich []byte
|
|
ErrorMessage *string
|
|
}
|
|
|
|
// Powers GET /api/admin/scan/status. Returns NoRows when no scan has
|
|
// ever run; the API layer maps that to a 200 with empty payload.
|
|
func (q *Queries) GetLatestScanRun(ctx context.Context) (GetLatestScanRunRow, error) {
|
|
row := q.db.QueryRow(ctx, getLatestScanRun)
|
|
var i GetLatestScanRunRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.StartedAt,
|
|
&i.FinishedAt,
|
|
&i.Library,
|
|
&i.MbidBackfill,
|
|
&i.CoverEnrich,
|
|
&i.ArtistArtEnrich,
|
|
&i.ErrorMessage,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertScanRun = `-- name: InsertScanRun :one
|
|
|
|
INSERT INTO scan_runs DEFAULT VALUES RETURNING id, started_at
|
|
`
|
|
|
|
type InsertScanRunRow struct {
|
|
ID pgtype.UUID
|
|
StartedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Scan-run lifecycle queries.
|
|
// Creates a new scan_runs row with started_at=now(). Stages are filled in
|
|
// as each completes via UpdateScanRunStage; finished_at is set by FinishScanRun.
|
|
func (q *Queries) InsertScanRun(ctx context.Context) (InsertScanRunRow, error) {
|
|
row := q.db.QueryRow(ctx, insertScanRun)
|
|
var i InsertScanRunRow
|
|
err := row.Scan(&i.ID, &i.StartedAt)
|
|
return i, err
|
|
}
|
|
|
|
const updateScanRunArtistArtEnrich = `-- name: UpdateScanRunArtistArtEnrich :exec
|
|
UPDATE scan_runs
|
|
SET artist_art_enrich = $2
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateScanRunArtistArtEnrichParams struct {
|
|
ID pgtype.UUID
|
|
ArtistArtEnrich []byte
|
|
}
|
|
|
|
// Persists the artist-art enrich stage tally JSON.
|
|
func (q *Queries) UpdateScanRunArtistArtEnrich(ctx context.Context, arg UpdateScanRunArtistArtEnrichParams) error {
|
|
_, err := q.db.Exec(ctx, updateScanRunArtistArtEnrich, arg.ID, arg.ArtistArtEnrich)
|
|
return err
|
|
}
|
|
|
|
const updateScanRunCoverEnrich = `-- name: UpdateScanRunCoverEnrich :exec
|
|
UPDATE scan_runs SET cover_enrich = $2 WHERE id = $1
|
|
`
|
|
|
|
type UpdateScanRunCoverEnrichParams struct {
|
|
ID pgtype.UUID
|
|
CoverEnrich []byte
|
|
}
|
|
|
|
func (q *Queries) UpdateScanRunCoverEnrich(ctx context.Context, arg UpdateScanRunCoverEnrichParams) error {
|
|
_, err := q.db.Exec(ctx, updateScanRunCoverEnrich, arg.ID, arg.CoverEnrich)
|
|
return err
|
|
}
|
|
|
|
const updateScanRunLibrary = `-- name: UpdateScanRunLibrary :exec
|
|
UPDATE scan_runs SET library = $2 WHERE id = $1
|
|
`
|
|
|
|
type UpdateScanRunLibraryParams struct {
|
|
ID pgtype.UUID
|
|
Library []byte
|
|
}
|
|
|
|
// Persists the file-walk stage's tallies. Idempotent — overwrites prior
|
|
// value if the orchestrator re-runs the stage on retry.
|
|
func (q *Queries) UpdateScanRunLibrary(ctx context.Context, arg UpdateScanRunLibraryParams) error {
|
|
_, err := q.db.Exec(ctx, updateScanRunLibrary, arg.ID, arg.Library)
|
|
return err
|
|
}
|
|
|
|
const updateScanRunMbidBackfill = `-- name: UpdateScanRunMbidBackfill :exec
|
|
UPDATE scan_runs SET mbid_backfill = $2 WHERE id = $1
|
|
`
|
|
|
|
type UpdateScanRunMbidBackfillParams struct {
|
|
ID pgtype.UUID
|
|
MbidBackfill []byte
|
|
}
|
|
|
|
func (q *Queries) UpdateScanRunMbidBackfill(ctx context.Context, arg UpdateScanRunMbidBackfillParams) error {
|
|
_, err := q.db.Exec(ctx, updateScanRunMbidBackfill, arg.ID, arg.MbidBackfill)
|
|
return err
|
|
}
|