release / go (push) Successful in 2m6s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m18s
release / android (push) Successful in 4m19s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 30s
release / Verify release artifacts (tag releases only) (push) Skipped
Background jobs run in their own goroutines, outside the router's panic recovery, and only five of them recovered. A panic in the scrobble, similarity, co-play, gc, tags, compactor, notification, backup or safety-net loop, or in a scan or rebuild started off a request, ended the process and every stream in it. internal/periodic now runs each job: a pass at start (or after one interval, Delayed), then one per interval and on any WakeOn channel, each pass under recover with the stack logged. Detached one-shot work (scans, the lazy system-playlist build, bulk cover refetch, leveled prerender, the backup run, the playlist catch-up) runs under periodic.Safe. The duplicate resolver keeps its own loop for its retry-soon timer, and its pass already recovers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.4 KiB
Go
94 lines
3.4 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/periodic"
|
|
)
|
|
|
|
// StuckScanThreshold is the age past which an in-flight scan_runs row is
|
|
// reaped on the next TryStartScan attempt. Real scans on realistic libraries
|
|
// finish well within this; the threshold is set far above legitimate scan
|
|
// 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.
|
|
//
|
|
// Behavior:
|
|
// - No in-flight row → starts RunScan in a detached goroutine; returns (true, nil, nil).
|
|
// - In-flight row, started_at > StuckScanThreshold ago → reaps via FinishScanRun
|
|
// with error_message="reaped (stale)", then starts new scan; returns (true, nil, nil).
|
|
// - In-flight row, started_at within threshold → returns (false, &row, nil); caller
|
|
// decides what to do (HTTP handler returns 409; scheduler logs skip).
|
|
// - DB error during the in-flight check or reap → returns (false, nil, err).
|
|
func TryStartScan(ctx context.Context, pool *pgxpool.Pool,
|
|
scanner *Scanner, enricher *coverart.Enricher,
|
|
logger *slog.Logger, cfg RunScanConfig,
|
|
) (started bool, existing *dbq.GetInFlightScanRunRow, err error) {
|
|
q := dbq.New(pool)
|
|
row, qerr := q.GetInFlightScanRun(ctx)
|
|
if qerr == nil {
|
|
// Found an in-flight row. Reap if stale, else skip.
|
|
age := time.Since(row.StartedAt.Time)
|
|
if age <= StuckScanThreshold {
|
|
return false, &row, nil
|
|
}
|
|
logger.Warn("reaping stale in-flight scan",
|
|
"id", row.ID, "started_at", row.StartedAt.Time, "age", age)
|
|
if rerr := q.FinishScanRun(ctx, dbq.FinishScanRunParams{
|
|
ID: row.ID,
|
|
Column2: "reaped (stale)",
|
|
}); rerr != nil {
|
|
return false, nil, fmt.Errorf("reap stale scan: %w", rerr)
|
|
}
|
|
// Fall through to start a new scan.
|
|
} else if !errors.Is(qerr, pgx.ErrNoRows) {
|
|
return false, nil, fmt.Errorf("in-flight check: %w", qerr)
|
|
}
|
|
|
|
// No in-flight (or just reaped). Start RunScan detached.
|
|
go periodic.Safe(ctx, logger, "scan run", func(ctx context.Context) {
|
|
if _, runErr := RunScan(ctx, pool, scanner, enricher, logger, cfg); runErr != nil {
|
|
logger.Warn("scan run failed", "err", runErr)
|
|
}
|
|
})
|
|
return true, nil, nil
|
|
}
|