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:
1 parent
792ee26af5
commit
8b8b7da0c6
8 files changed
+336
-17
No files matched your search
@@ -147,6 +147,7 @@ func quietHours(q config.NtfyQuiet, formatZone string) (*ntfy.Quiet, error) {
|
||||
// ntfySubmitter is the print queue as a subscription uses it.
|
||||
type ntfySubmitter interface {
|
||||
Submit(job receipt.Job, meta printqueue.Meta) (printqueue.Status, error)
|
||||
Holds(source, ref string) bool
|
||||
}
|
||||
|
||||
// ntfyPrinter prints what a subscription delivers: each message as a
|
||||
@@ -163,7 +164,9 @@ type ntfyPrinter struct {
|
||||
const digestRef = "digest"
|
||||
|
||||
// Message prints m as a card, marked "(updated)" when updated. A full
|
||||
// queue is returned, for the subscription to offer m again.
|
||||
// queue is returned, for the subscription to offer m again. The card is
|
||||
// unique by m's ID, so a message offered again after a crash, whose card
|
||||
// the queue already holds, is not queued twice.
|
||||
func (p *ntfyPrinter) Message(_ context.Context, m ntfy.Message, updated bool) error {
|
||||
n, err := modules.FromNtfy(m)
|
||||
if err != nil {
|
||||
@@ -174,7 +177,13 @@ func (p *ntfyPrinter) Message(_ context.Context, m ntfy.Message, updated bool) e
|
||||
env := p.env()
|
||||
job, err := modules.Card(n, env.Card, env)
|
||||
p.reportFallback(m.ID, err)
|
||||
return p.submit(job, printqueue.Meta{Ref: m.ID, Sequence: m.Notification()})
|
||||
return p.submit(job, printqueue.Meta{Ref: m.ID, Sequence: m.Notification(), Unique: true})
|
||||
}
|
||||
|
||||
// Queued reports whether the queue holds the card of m, which a message
|
||||
// offered again after a restart may already have.
|
||||
func (p *ntfyPrinter) Queued(m ntfy.Message) bool {
|
||||
return p.jobs.Holds("ntfy:"+p.name, m.ID)
|
||||
}
|
||||
|
||||
// Digest prints the held messages on one receipt.
|
||||
|
||||
@@ -218,6 +218,16 @@ func (q *fakeQueue) Submit(job receipt.Job, meta printqueue.Meta) (printqueue.St
|
||||
return printqueue.Status{}, nil
|
||||
}
|
||||
|
||||
// Holds reports whether a unique job of source and ref was submitted.
|
||||
func (q *fakeQueue) Holds(source, ref string) bool {
|
||||
for _, m := range q.metas {
|
||||
if m.Unique && m.Source == source && m.Ref == ref {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func testEnv() modules.Env {
|
||||
return modules.Env{Now: time.Date(2026, 11, 29, 21, 4, 5, 0, time.UTC), Location: time.UTC, TimeLayout: "02 Jan 15:04"}
|
||||
}
|
||||
@@ -244,7 +254,7 @@ func TestNtfyPrinter(t *testing.T) {
|
||||
if len(q.jobs) != 2 {
|
||||
t.Fatalf("%d jobs, want the card and the digest", len(q.jobs))
|
||||
}
|
||||
if q.metas[0] != (printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Sequence: "alerts/msg1"}) ||
|
||||
if q.metas[0] != (printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Sequence: "alerts/msg1", Unique: true}) ||
|
||||
q.metas[1] != (printqueue.Meta{Source: "ntfy:home", Ref: "digest"}) {
|
||||
t.Errorf("metas %+v", q.metas)
|
||||
}
|
||||
@@ -257,6 +267,10 @@ func TestNtfyPrinter(t *testing.T) {
|
||||
t.Errorf("metas %+v, %v; want a card of a named sequence to carry its notification", q.metas, err)
|
||||
}
|
||||
|
||||
if !p.Queued(m) || p.Queued(ntfy.Message{ID: "other"}) {
|
||||
t.Error("Queued does not answer by the card's message ID")
|
||||
}
|
||||
|
||||
q.err = printqueue.ErrQueueFull
|
||||
if err := p.Message(ctx, m, false); !errors.Is(err, printqueue.ErrQueueFull) {
|
||||
t.Errorf("err = %v, want the full queue back", err)
|
||||
|
||||
Reference in new issue
Block a user