Migration 0049 adds suggestion_snoozes(user_id, candidate_mbid, candidate_name, snoozed_until), and SuggestArtistsForUser excludes rows whose snooze hasn't expired. This is NOT a dislike. Rule #101 forbids a "Not for me" / thumbs-down UI; a snooze is the approved shape instead because it records no verdict on the music, expires on its own (~90d), and never reaches the taste profile. It's acquisition triage — "not right now" — so the filter sits at the candidate stage rather than in the score, where it would become a ranking signal by the back door. Per-user throughout (rule #47): one household member parking a candidate leaves everyone else's deck untouched. candidate_name is denormalized because suggestions are out-of-library by definition — there is no artists row to resolve a display name from, and the un-snooze list has to show something. That list is why GET /discover/snoozes exists at all: a parked candidate is by definition absent from the deck, so without it the DELETE would be unreachable. Also fixes a hole in the codegen check from #2380: `git diff` ignores untracked paths, so a brand-new generated file would have passed it silently. `git add -N` first. This commit is the first to add one. Endpoints: POST /api/discover/suggestions/{mbid}/snooze (body: name, days) DELETE /api/discover/suggestions/{mbid}/snooze GET /api/discover/snoozes UI lands in slice 4 (#2375) before any of this merges — rule #27. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
106 lines
4.0 KiB
Go
106 lines
4.0 KiB
Go
// Package gc runs periodic garbage-collection / lifecycle sweeps
|
|
// against tables that have NO writer-side close path or NO retention
|
|
// policy. Each sweep addresses a drift finding from the 2026-06-02
|
|
// audit (Scribe parent #552) and is idempotent — re-running it on
|
|
// already-clean rows is a no-op.
|
|
//
|
|
// One Worker handles all sweeps so a single long-tick goroutine
|
|
// amortises the per-tick fixed cost. Each individual sweep is small
|
|
// (single UPDATE / DELETE with a time-bounded WHERE) and emits a
|
|
// log line with the affected-row count so the sweep cadence is
|
|
// visible in the application log without an explicit metrics layer.
|
|
//
|
|
// Sweeps:
|
|
// - GcCloseStalePlayEvents (#566)
|
|
// - GcClosePlaySessionsWithNoRecentEvents (#565)
|
|
// - GcExpireScrobbleQueueFailedRows (#567)
|
|
// - GcResetStuckSystemPlaylistRuns (#574)
|
|
// - GcDeleteExpiredPasswordResets (#575)
|
|
// - GcPruneDiagnostics (M9 — diagnostics 30d retention)
|
|
// - GcDeleteExpiredSuggestionSnoozes (#2374 — snoozes expire, then go)
|
|
package gc
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// defaultTick is the production sweep cadence. 1 hour is generous
|
|
// since each sweep's WHERE clause uses a multi-hour staleness
|
|
// threshold; the worst-case delay between a row becoming sweepable
|
|
// and the worker noticing is bounded by tick + threshold.
|
|
const defaultTick = 1 * time.Hour
|
|
|
|
// Worker holds the pool + logger + tick interval. Construct with
|
|
// [NewWorker]; pass the returned Worker to a goroutine that calls
|
|
// [Worker.Run] with a context that's cancelled on shutdown.
|
|
type Worker struct {
|
|
pool *pgxpool.Pool
|
|
logger *slog.Logger
|
|
tick time.Duration
|
|
}
|
|
|
|
// NewWorker builds a Worker with the production tick (1h). Tests can
|
|
// reach into the Worker after construction to override `tick` for
|
|
// faster iteration.
|
|
func NewWorker(pool *pgxpool.Pool, logger *slog.Logger) *Worker {
|
|
return &Worker{pool: pool, logger: logger, tick: defaultTick}
|
|
}
|
|
|
|
// Run blocks until ctx is cancelled, running every sweep on each
|
|
// tick. Sweeps fire in fixed order; an error in one does NOT abort
|
|
// the rest (the panic-vs-just-failed distinction matters here — a
|
|
// pgx transient error from one query shouldn't prevent the others
|
|
// from running).
|
|
func (w *Worker) Run(ctx context.Context) {
|
|
// Fire once at start so a freshly-deployed server doesn't wait a
|
|
// full tick before doing the initial sweep. Matches the scrobble
|
|
// + similarity workers' "sweep then tick" pattern.
|
|
w.tickOnce(ctx)
|
|
t := time.NewTicker(w.tick)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
w.tickOnce(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
// tickOnce runs each sweep once, logging the affected-row count.
|
|
// Errors are logged per-sweep but do NOT abort the remaining ones —
|
|
// each sweep is independent.
|
|
func (w *Worker) tickOnce(ctx context.Context) {
|
|
q := dbq.New(w.pool)
|
|
w.runSweep(ctx, "close_stale_play_events", q.GcCloseStalePlayEvents)
|
|
w.runSweep(ctx, "close_play_sessions", q.GcClosePlaySessionsWithNoRecentEvents)
|
|
w.runSweep(ctx, "expire_scrobble_failed", q.GcExpireScrobbleQueueFailedRows)
|
|
w.runSweep(ctx, "reset_stuck_system_runs", q.GcResetStuckSystemPlaylistRuns)
|
|
w.runSweep(ctx, "delete_expired_password_resets", q.GcDeleteExpiredPasswordResets)
|
|
w.runSweep(ctx, "prune_diagnostics", q.GcPruneDiagnostics)
|
|
w.runSweep(ctx, "delete_expired_suggestion_snoozes", q.GcDeleteExpiredSuggestionSnoozes)
|
|
}
|
|
|
|
// runSweep is a small adapter so each sweep call site is a one-liner
|
|
// in tickOnce. Logs at info on rows>0 and debug on rows=0 to keep
|
|
// the normal-case (nothing-to-do) noise out of operator logs.
|
|
func (w *Worker) runSweep(ctx context.Context, name string, fn func(context.Context) (int64, error)) {
|
|
rows, err := fn(ctx)
|
|
if err != nil {
|
|
w.logger.Error("gc sweep failed", "sweep", name, "err", err)
|
|
return
|
|
}
|
|
if rows > 0 {
|
|
w.logger.Info("gc sweep", "sweep", name, "rows_affected", rows)
|
|
} else {
|
|
w.logger.Debug("gc sweep", "sweep", name, "rows_affected", 0)
|
|
}
|
|
}
|