feat(playlists): wire Refresh into PUT /api/me/timezone + registration

Completes the server side of #392 Half B.

PUT /api/me/timezone now calls scheduler.Refresh(ctx, userID) after
the DB write so the rescheduled daily-at-03:00-local job takes
effect synchronously. Failure to refresh is logged but doesn't
undo the DB write — the hourly reconciliation pass would pick it
up within an hour regardless.

POST /api/auth/register calls Refresh after successful user
insert so brand-new users get scheduled immediately rather than
waiting for the hourly pass to discover them.

system_cron.go deleted: the new scheduler subsumes its
responsibilities. The StartSystemPlaylistCron call in main.go is
also removed. Server restart now runs the new scheduler's startup
recovery + catch-up instead.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-13 11:58:24 -04:00
parent 46c8edfa82
commit 7fac264c73
4 changed files with 21 additions and 62 deletions
-61
View File
@@ -1,61 +0,0 @@
package playlists
import (
"context"
"log/slog"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// StartSystemPlaylistCron launches the in-process daily build loop in a
// goroutine. Caller passes a context that's cancelled on graceful shutdown.
//
// Lifecycle:
// 1. Clear any stale in_flight=true rows from a previously crashed process
// (safe at startup: by definition, nothing is running yet).
// 2. Run once at startup so users catch up after a server restart without
// waiting up to 24h.
// 3. Tick every 24h; runOnce on each tick.
// 4. Exit on context cancellation.
//
// Per-user errors are logged but don't halt the loop. Per-cycle errors (e.g.,
// "list active users" failed) are logged but the next tick still fires.
func StartSystemPlaylistCron(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, dataDir string) {
go func() {
q := dbq.New(pool)
if err := q.ClearStaleSystemPlaylistInFlight(ctx); err != nil {
logger.Warn("system playlist cron: startup recovery failed", "err", err)
}
runOnce(ctx, pool, logger, dataDir)
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
runOnce(ctx, pool, logger, dataDir)
}
}
}()
}
func runOnce(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, dataDir string) {
q := dbq.New(pool)
users, err := q.ListActiveUsersForSystemPlaylists(ctx)
if err != nil {
logger.Error("system playlist cron: list active users failed", "err", err)
return
}
for _, userID := range users {
if err := BuildSystemPlaylists(ctx, pool, logger, userID, time.Now(), dataDir); err != nil {
logger.Warn("system playlist build failed in cron",
"user_id", uuidStringPL(userID), "err", err)
}
}
}