diff --git a/internal/scrobble/worker.go b/internal/scrobble/worker.go index 82e69f10..20fa08c6 100644 --- a/internal/scrobble/worker.go +++ b/internal/scrobble/worker.go @@ -85,9 +85,20 @@ func (w *Worker) tickOnce(ctx context.Context) error { userID pgtype.UUID } batches := map[string]*userBatch{} + // A row that can't be sent is marked failed rather than left pending. + // The batch is the oldest pending rows, so rows left behind would come + // back first on every tick, and a full batch of them would stop + // ListenBrainz scrobbling for every account. + unsendable := func(id pgtype.UUID, why string) { + if err := q.MarkScrobbleFailed(ctx, dbq.MarkScrobbleFailedParams{ID: id, LastError: &why}); err != nil { + w.logger.Error("scrobble: MarkScrobbleFailed", "queue_id", id, "err", err) + } + } for _, r := range rows { if r.LbToken == nil || *r.LbToken == "" || !r.LbEnabled { - // Defensive: row got into queue but user since disabled. Skip. + // ListenBrainz was switched off, or its token removed, after the + // play was queued. + unsendable(r.QueueID, "ListenBrainz is off for this account") continue } key := r.UserID.String() @@ -95,6 +106,7 @@ func (w *Worker) tickOnce(ctx context.Context) error { if ub == nil { token := secret.Reveal(secret.ListenBrainzToken.Purpose(), key, *r.LbToken) if token == "" { + unsendable(r.QueueID, "the saved ListenBrainz token can't be read; save it again in Settings") continue } ub = &userBatch{token: token, userID: r.UserID} diff --git a/internal/scrobble/worker_integration_test.go b/internal/scrobble/worker_integration_test.go index b842863f..56d28159 100644 --- a/internal/scrobble/worker_integration_test.go +++ b/internal/scrobble/worker_integration_test.go @@ -222,3 +222,26 @@ func TestWorker_BatchOfTen_OnePost(t *testing.T) { t.Errorf("LB POSTs = %d, want 1 (batch)", posts.Load()) } } + +// A queued play whose account has since switched ListenBrainz off is marked +// failed, not left pending: left pending it heads the queue on every tick. +func TestWorker_ListenBrainzSwitchedOff_MarksFailedInsteadOfBlockingTheQueue(t *testing.T) { + s := seed(t, seedOpts{lbToken: "tk", lbEnabled: false, durationMs: 250_000, ratio: 0.83}) + helperEnqueue(t, s) + var posts atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + posts.Add(1) + w.WriteHeader(http.StatusOK) + })) + defer srv.Close() + lb := &listenbrainz.Client{BaseURL: srv.URL, HTTP: srv.Client()} + if err := newTestWorker(s, lb).tickOnce(context.Background()); err != nil { + t.Fatalf("tickOnce: %v", err) + } + if helperPendingCount(t, s) != 0 || helperFailedCount(t, s) != 1 { + t.Fatalf("pending %d failed %d, want the row marked failed", helperPendingCount(t, s), helperFailedCount(t, s)) + } + if posts.Load() != 0 { + t.Error("a play was sent for an account with ListenBrainz off") + } +}