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
@@ -220,7 +220,8 @@ Request bodies and the `Authorization` header are never sent to Sentry.
|
||||
or the token.
|
||||
- The worker logs each job with its `job_id`, its `source` (`print`,
|
||||
`module:<name>` or `ntfy:<name>`) and its `ref`, the ID of the request or
|
||||
ntfy message that sent it, never with what it prints.
|
||||
ntfy message that sent it, or a digest's own reference, never with what
|
||||
it prints.
|
||||
- Each [ntfy subscription](ntfy.md) logs with its `subscription` name,
|
||||
never with what a message says or its topic. It logs at WARN when its
|
||||
connection is lost, when its credentials are refused and while the queue
|
||||
|
||||
+18
-11
@@ -201,20 +201,22 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
|
||||
match. A malformed scope can therefore never widen access.
|
||||
- **Jobs remember their source.** `Submit` takes a `Meta`: the source
|
||||
(`print`, `module:<name>` or `ntfy:<subscription>`) and a reference, the
|
||||
request ID on the HTTP routes and the message's ID, or `digest`, for a
|
||||
subscription. Job status shows the source and every
|
||||
request ID on the HTTP routes and the message's ID, or the digest's own
|
||||
reference, for a subscription. Job status shows the source and every
|
||||
worker log line of the job carries both, so a printed receipt can be
|
||||
traced to its request without logging what it says. The server copies
|
||||
both strings, since a request ID the client sent lives in a buffer that
|
||||
fasthttp reuses for the next request.
|
||||
A card's reference, its message's ID, is unique at its subscription: a
|
||||
card whose source and reference the queue holds, among the last 512 of
|
||||
that source kept apart from the statuses, is not queued again, and the
|
||||
queue answers with the earlier job. A message the subscription would
|
||||
hold for a digest never reaches Submit, so the subscription asks the
|
||||
queue first, and one whose card it holds counts as printed. So a
|
||||
message offered again after a restart prints once while its job is
|
||||
stored. A request ID is never checked, since a client may set it.
|
||||
A subscription's references are unique at it: a card's is its message's
|
||||
ID, and a digest's is a TypeID with the prefix `digest`, minted when the
|
||||
digest is made. A job whose source and reference the queue holds, among
|
||||
the last 512 of that source kept apart from the statuses, is not queued
|
||||
again, and the queue answers with the earlier job. A message the
|
||||
subscription would hold for a digest never reaches Submit, so the
|
||||
subscription asks the queue first, and one whose card it holds counts
|
||||
as printed. So a message or a digest offered again after a restart
|
||||
prints once while its job is stored. A request ID is never checked,
|
||||
since a client may set it.
|
||||
- **The preview reads the bytes back.** `escpos.Interpret` parses the stream
|
||||
the renderer produced, so the preview and the tests show what the printer
|
||||
receives rather than what the layout meant. It simulates the printer's own
|
||||
@@ -417,7 +419,12 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue (
|
||||
during them, `quiet.min_priority` decides first. The receipt times are
|
||||
saved, so a restart cannot reset the budget. A digest lists 50
|
||||
messages and counts the rest; only those with a row count as printed
|
||||
for updates.
|
||||
for updates. Once made, a digest gets its reference and is saved with
|
||||
it, apart from the messages held, and is submitted only once that save
|
||||
has succeeded; messages held after that wait for the next digest. A
|
||||
restart before the queue's answer is saved submits the same digest
|
||||
again under the same reference, ahead of the later messages, so the
|
||||
queue's duplicate check finds it while its job is stored.
|
||||
- **Quiet hours print a morning digest (owner, 2026-11-30).** During a
|
||||
subscription's daily window only `quiet.min_priority` (5) and above
|
||||
print; the rest are held in the same digest buffer and print as one
|
||||
|
||||
+9
-3
@@ -91,10 +91,16 @@ a volume for the state and the token file next to the config, as
|
||||
within a second of a message being printed, held or left out, at most
|
||||
every 10 minutes when only keepalives have moved the cursor, and at
|
||||
shutdown. A damaged file is ignored with a warning, and the subscription
|
||||
starts as if new. Held messages survive a restart.
|
||||
starts as if new. Held messages survive a restart, and so does a digest
|
||||
made and not yet queued: it is saved before it is queued, and a restart
|
||||
queues it again, unchanged. While the file cannot be written, digests
|
||||
wait.
|
||||
- **Jobs.** Each card and digest is an ordinary job with the source
|
||||
`ntfy:<name>`. Like any job, one that is still waiting when the daemon
|
||||
shuts down is marked failed, and is not printed after the restart.
|
||||
`ntfy:<name>` and a reference: a card's is its message's ID, and a
|
||||
digest's is its own, `digest_` and a TypeID. A card or digest whose
|
||||
source and reference the queue already holds is not queued again. Like
|
||||
any job, one that is still waiting when the daemon shuts down is marked
|
||||
failed, and is not printed after the restart.
|
||||
|
||||
## Securing the topics
|
||||
|
||||
|
||||
+20
-9
@@ -1,6 +1,6 @@
|
||||
# Plan: persistent job store and image previews
|
||||
|
||||
Status: **in progress; stage 1a's measurements are done (Investigation findings), and so is stage 1b's `[state] dir`. The store itself is not built.** The owner accepted every proposed default on 2026-12-01 (Decisions), and design.md's Planned next item 1 records them. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decision 16, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. Every check on the live printer needs the owner's OK.
|
||||
Status: **in progress; stage 1a's measurements are done (Investigation findings), and so are stage 1b's `[state] dir` and stage 1c's state machine on the memory store. The store on disk is not built.** The owner accepted every proposed default on 2026-12-01 (Decisions), and design.md's Planned next item 1 records them. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decision 16, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. Every check on the live printer needs the owner's OK.
|
||||
|
||||
## Decisions (owner, 2026-11-30)
|
||||
|
||||
@@ -389,7 +389,7 @@ ACKNOWLEDGEMENT, WHERE THE PRINTER HAS IT (phase 5, decision 21)
|
||||
- Holding the connection until the receipt prints would make `/healthz` time out (3 s) during long prints. While a job holds the connection, `/healthz` reports the status that connection last polled, with its age.
|
||||
|
||||
THE STORE INTERFACE (printqueue)
|
||||
printqueue defines the interface and an in-memory implementation and imports no database. A new package, `internal/pkg/jobstore`, implements it on disk, and internal/app wires it in. The engine can change without touching the queue. A sketch; stage 1c settles the names:
|
||||
printqueue defines the interface and an in-memory implementation and imports no database. A new package, `internal/pkg/jobstore`, implements it on disk, and internal/app wires it in. The engine can change without touching the queue. Stage 1c settled it as below; `printqueue/store.go` has the full doc comments:
|
||||
|
||||
```go
|
||||
// Record is one job as the store keeps it, with its deliveries.
|
||||
@@ -424,6 +424,14 @@ type Loaded struct {
|
||||
Uncertain []uint64 // the deliveries this Load turned from sending into uncertain
|
||||
}
|
||||
|
||||
// Prune names what Finish drops in its commit, by Seq. A job that goes is
|
||||
// listed with each of its deliveries and with its content, if it still has it.
|
||||
type Prune struct {
|
||||
Jobs []uint64
|
||||
Deliveries []uint64
|
||||
Content []uint64
|
||||
}
|
||||
|
||||
// Store keeps jobs across restarts.
|
||||
type Store interface {
|
||||
// Durable reports whether records outlive the process.
|
||||
@@ -435,12 +443,13 @@ type Store interface {
|
||||
// Add stores a new job, its queued deliveries and its content. With
|
||||
// sync, the commit is durable when Add returns.
|
||||
Add(ctx context.Context, r Record, job []byte, sync bool) error
|
||||
// Sending records attempt n of a delivery on the printer it uses and
|
||||
// returns once that is durable.
|
||||
Sending(ctx context.Context, delivery uint64, printer string, attempt int, at time.Time) error
|
||||
// Finish records a delivery's final state and drops the given jobs,
|
||||
// deliveries and contents, in one commit; sync as for Add.
|
||||
Finish(ctx context.Context, d Delivery, dropJobs, dropDeliveries, dropContent []uint64, sync bool) error
|
||||
// Sending records d, an attempt about to write to its printer, in
|
||||
// sending: its printer, attempts, StartedAt and SentAt. It returns once
|
||||
// that is durable.
|
||||
Sending(ctx context.Context, d Delivery) error
|
||||
// Finish records d in its final state and drops what p names, in one
|
||||
// commit; sync as for Add.
|
||||
Finish(ctx context.Context, d Delivery, p Prune, sync bool) error
|
||||
// Job returns a stored job's content, or ErrContentGone.
|
||||
Job(ctx context.Context, seq uint64) ([]byte, error)
|
||||
// Idle is called when the queue has nothing to do.
|
||||
@@ -658,6 +667,8 @@ Docs
|
||||
|
||||
### Stage 1c: The state machine, without a database
|
||||
|
||||
Done on 2026-12-01.
|
||||
|
||||
**Goal.** The queue runs on the `Store` interface, with the memory store only: jobs with deliveries, the state machine, `sent_at` and `uncertain`, restoring at start, the shutdown rules, store failures, the duplicate check and digest references, and retention by content bytes. No new dependency. With the memory store the daemon behaves as today, apart from what the API and the logs add, the duplicate check on ntfy jobs, and a digest kept apart from later held messages once it is made.
|
||||
|
||||
**Work.**
|
||||
@@ -858,7 +869,7 @@ internal/pkg/printqueue
|
||||
- A store error stops the queue (decision 11).
|
||||
- If docs/plans/printers.md stage 3 has shipped, a delivery with any `sending` mark is not cancellable, even while it waits on the next printer of an anycast list, and a move takes the delivery lock (its decision 26).
|
||||
- A cancelled job counts as finished for retention. An ntfy job keeps its reference (decision 10), so a replay after a restart does not queue it again.
|
||||
- store.go: `StoredState` gains `cancelled`, and `Store` gains `Cancel(ctx, deliveries []uint64, at time.Time, dropJobs, dropDeliveries, dropContent []uint64) error`, one synced commit. The memory store implements it.
|
||||
- store.go: `StoredState` gains `cancelled`, and `Store` gains `Cancel(ctx, deliveries []uint64, at time.Time, p Prune) error`, one synced commit. The memory store implements it.
|
||||
|
||||
internal/pkg/jobstore
|
||||
- `Cancel`: one synced transaction that sets each delivery's `state` and `finished_at` and drops the given jobs, deliveries and contents. The `sequence` column is in the schema and written from stage 1d (RECORDS), so there is no schema step.
|
||||
|
||||
Reference in new issue
Block a user