Give each ntfy digest a reference of its own
Every digest went to the print queue under the fixed reference "digest", so after a crash the queue could not tell a digest it had queued from a new one. Once queued jobs survive a restart, a crash in the second between queueing a digest and saving the subscription's state would print it twice; the same window exists today for a digest that printed in that second. A digest now gets a reference of its own when it is made, "digest_" and a TypeID, and is saved with it in the subscription's state, apart from the held messages. It is offered to the queue only once that save has succeeded, and dropped once the queue takes it; while the state file cannot be written, digests wait. A restart with a saved digest offers that digest again first, with the same rows and reference, and the queue's duplicate check answers with the earlier job while it holds it. Messages held after a digest is made wait for the next one, also while quiet hours delay the saved digest after a restart: added to a digest the queue already holds, they would never print. The digest is submitted as unique, like a card. A digest can cost one more state save, two fsyncs. The reference shows in the log lines and Sentry reports where "digest" did. This completes 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
8b8b7da0c6
commit
64a72903d8
12 files changed
+431
-58
No files matched your search
@@ -152,7 +152,7 @@ type ntfySubmitter interface {
|
||||
|
||||
// ntfyPrinter prints what a subscription delivers: each message as a
|
||||
// card, and held messages as a digest, through the print queue. Jobs carry
|
||||
// the source ntfy:<name> and the message's ID, or "digest".
|
||||
// the source ntfy:<name> and the message's ID, or the digest's reference.
|
||||
type ntfyPrinter struct {
|
||||
name string
|
||||
jobs ntfySubmitter
|
||||
@@ -160,9 +160,6 @@ type ntfyPrinter struct {
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// digestRef is the reference of a digest's job.
|
||||
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. The card is
|
||||
// unique by m's ID, so a message offered again after a crash, whose card
|
||||
@@ -186,12 +183,14 @@ 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.
|
||||
// Digest prints the held messages on one receipt. It is unique by the
|
||||
// digest's reference, so a digest offered again after a crash, whose job
|
||||
// the queue already holds, is not queued twice.
|
||||
func (p *ntfyPrinter) Digest(_ context.Context, d ntfy.Digest) error {
|
||||
env := p.env()
|
||||
job, err := modules.DigestCard(d, env.Card, env)
|
||||
p.reportFallback(digestRef, err)
|
||||
return p.submit(job, printqueue.Meta{Ref: digestRef})
|
||||
p.reportFallback(d.Ref, err)
|
||||
return p.submit(job, printqueue.Meta{Ref: d.Ref, Unique: true})
|
||||
}
|
||||
|
||||
// submit validates job and queues it with meta, from the subscription.
|
||||
|
||||
@@ -248,14 +248,15 @@ func TestNtfyPrinter(t *testing.T) {
|
||||
if err := p.Message(ctx, ntfy.Message{ID: "b64", Event: "message", Message: "AAE=", Encoding: "base64"}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := p.Digest(ctx, ntfy.Digest{Rows: []ntfy.DigestRow{{ID: "x", Title: "SecretRow"}}, Count: 1, Until: testEnv().Now}); err != nil {
|
||||
digest := ntfy.Digest{Rows: []ntfy.DigestRow{{ID: "x", Title: "SecretRow"}}, Count: 1, Until: testEnv().Now, Ref: "digest_01kbx4n6m3ex8tq0wq3r5vrzq1"}
|
||||
if err := p.Digest(ctx, digest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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", Unique: true}) ||
|
||||
q.metas[1] != (printqueue.Meta{Source: "ntfy:home", Ref: "digest"}) {
|
||||
q.metas[1] != (printqueue.Meta{Source: "ntfy:home", Ref: digest.Ref, Unique: true}) {
|
||||
t.Errorf("metas %+v", q.metas)
|
||||
}
|
||||
if title := q.jobs[0].Elements[0].Title; title != "Disk almost full (updated)" {
|
||||
|
||||
Reference in new issue
Block a user