Answer a repeated ntfy message with its earlier job

Once queued jobs survive a restart, a crash in the second between
queueing an ntfy message and saving the subscription's state would
print the message twice: the replay offers it again. A card is now
submitted as unique by its message's ID. If the queue already holds
that source and reference, Submit queues nothing and returns the
earlier job, or its ID and source once its status is gone, and logs
the repeat. The queue keeps the last 512 references of each source
apart from the statuses, so queue.history = 0 cannot defeat the check,
and they come back on restore.

A replayed message that quiet hours or the paper budget would hold for
a digest never reaches Submit, so the check could not answer it, and
it would print again as a digest row. Before it holds a message, the
subscription now asks the queue through its handler whether it holds
that message's card; if so, the message counts as printed and is not
held.

Only unique jobs take part. On the HTTP routes the reference is a
request ID, which a client may set, so a check on it could answer a
new request with an old job. A unique insert is stored with a sync,
since the subscription treats the message as dealt with once it is
queued.

Part of stage 1c of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 13:50:09 -06:00
1 parent 792ee26af5
commit 8b8b7da0c6
8 files changed
+336 -17

No files matched your search

+7
View File
@@ -345,6 +345,9 @@ type recorder struct {
cards []card
digests []Digest
fail int
// queued, if set, answers Queued for a message's ID.
queued func(id string) bool
}
// card is a message the recorder printed.
@@ -377,6 +380,10 @@ func (h *recorder) Digest(_ context.Context, d Digest) error {
return nil
}
func (h *recorder) Queued(m Message) bool {
return h.queued != nil && h.queued(m.ID)
}
func (h *recorder) failNext(n int) {
h.mu.Lock()
defer h.mu.Unlock()
+50
View File
@@ -3,6 +3,7 @@ package ntfy
import (
"context"
"fmt"
"os"
"slices"
"testing"
"testing/synctest"
@@ -379,3 +380,52 @@ func TestHeldPastTheRowsIsNotPrinted(t *testing.T) {
}
})
}
// A card queued just before a crash that lost the save recording it is
// offered again by the replay. Held by quiet hours, it would never reach
// the queue's own check and would print again on the morning digest, so
// the subscription asks the queue first: a card the queue holds counts as
// printed and is not held.
func TestReplayedCardTheQueueHoldsIsNotHeld(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
loc := london(t)
sleepUntil(time.Date(2026, 11, 30, 21, 0, 0, 0, loc))
f := newFakeServer()
o := testOptions(f, t.TempDir())
o.Quiet = nightly(loc)
o.MaxBacklog = 12 * time.Hour
h := &recorder{}
_, stop := start(t, o, h)
synctest.Wait()
before, err := os.ReadFile(o.StateFile)
if err != nil {
t.Fatal(err)
}
sleepUntil(time.Date(2026, 11, 30, 21, 30, 0, 0, loc))
card := f.publish(Message{Title: "backup done", RawPriority: 3})
synctest.Wait()
if got := h.printed(); !slices.Equal(got, []string{card.ID}) {
t.Fatalf("printed %v, want the card", got)
}
stop()
if err := os.WriteFile(o.StateFile, before, 0o600); err != nil {
t.Fatal(err)
}
sleepUntil(time.Date(2026, 11, 30, 23, 0, 0, 0, loc))
h2 := &recorder{queued: func(id string) bool { return id == card.ID }}
s, stop2 := start(t, o, h2)
defer stop2()
synctest.Wait()
if st := s.Status(); st.Held != 0 {
t.Errorf("status %+v, want the replayed card not held", st)
}
sleepUntil(time.Date(2026, 12, 1, 7, 0, 1, 0, loc))
if ds := h2.digestList(); len(ds) != 0 {
t.Errorf("digests %+v, want none: the queue holds the card", ds)
}
if got := h2.printed(); len(got) != 0 {
t.Errorf("printed %v after the restart, want nothing", got)
}
})
}
+25 -2
View File
@@ -163,6 +163,12 @@ type Handler interface {
// Digest prints the messages held back on one receipt. An error is
// retried as for Message, while the subscription goes on reading.
Digest(ctx context.Context, d Digest) error
// Queued reports whether the print queue already holds the card of m:
// one queued before a restart that lost the subscription's record of
// it. Such a message is not held for a digest, where it would print
// again.
Queued(m Message) bool
}
// Subscriber reads the messages of an ntfy server's topics as they are
@@ -520,11 +526,15 @@ func (r *run) deliver(ctx context.Context, m Message, now time.Time) bool {
r.log.Debug("ntfy message dropped in quiet hours", "ntfy_id", m.ID)
return true
}
r.hold(m, updated, true, now)
if !r.queued(m) {
r.hold(m, updated, true, now)
}
return true
}
if priority < r.o.BypassPriority && (!r.budget.room(now) || (r.held.Count > 0 && !quiet)) {
r.hold(m, updated, false, now)
if !r.queued(m) {
r.hold(m, updated, false, now)
}
return true
}
if err := r.h.Message(ctx, m, updated); err != nil {
@@ -541,6 +551,19 @@ func (r *run) deliver(ctx context.Context, m Message, now time.Time) bool {
return true
}
// queued reports whether the print queue already holds m's card, queued
// before a restart that lost the save of m. m then counts as printed and
// is not held, since a digest row would print it again; a message that is
// not held reaches the queue, whose own check answers it.
func (r *run) queued(m Message) bool {
if m.ID == "" || !r.h.Queued(m) {
return false
}
r.printed.add(m.Sequence())
r.log.Debug("ntfy message already queued", "ntfy_id", m.ID)
return true
}
// hold keeps m for the next digest. Only a message that gets a row counts
// as printed; one past the rows is only counted on the digest.
func (r *run) hold(m Message, updated, overnight bool, now time.Time) {