Record a job done only once the printer confirms it

Breaking: a job reads done only once the printer has confirmed that it
ran it, and holds the printer's connection until then. A job written in
full that the printer does not confirm within printer.write_timeout now
reads uncertain and is not sent again; before, it read done. Before
each job, nothing is sent until the printer answers that it can print,
so with its cover open or no paper a job is retried and fails, with no
sent_at, as when the printer is unreachable, where before it read done.
Receipts that take longer than write_timeout to print need a larger
one. With confirmation the daemon also turns the printer's automatic
status off (GS a 0) before each job and before its confirmation, so
another client that relies on automatic status from the same printer
stops getting it. printer.confirm = false keeps the old meaning of done
and sends none of this.

The third paper check of the job store showed why: with the
TM-T88IV's cover open, Windows took all of a 128 KB receipt into its
send buffer, so the job read done; the daemon stopped, and once the
cover closed only the first third printed, without its cut. The printer
was then left inside the image command, waiting for the rest, until it
was switched off and on.

The daemon now turns on the printer client's confirmation. The queue
treats an unconfirmed send as written but unknown: it records the
delivery uncertain, with an error that says the printer did not confirm
it and that it may have printed in full, in part or not at all, logs it
at WARN with the printer, and neither retries it nor reports it as a
failure. Its commit is not synced: lost, it rolls back to sending,
which the next start turns uncertain too. A printer that is not ready
gives a plain error before the sending mark, which the queue retries as
a failed dial.

The new key printer.confirm, on by default, turns confirmation off for
a printer that does not answer GS r with bits 4 and 7 clear, or does not
answer DLE EOT on receipt; only the TM-T88IV is checked. The key's
comments in config.go and the example config say what it sends and
needs. docs/api.md warns that raw bytes which make the printer answer
can be taken for the confirmation, and says that /healthz answers 503
timeout while a job holds the connection. It and design.md say that
each job waits about 200 ms for answers still owed, for DLE EOT 1 to
say that the printer is online, and for no answer to come late.
design.md says why each connection opens with DLE EOT 1, GS a 0 and
GS r 1, and names what stays open: a raw job holding a command answered
with bits 4 and 7 clear, such as GS r, GS I, ESC v or ESC u 0, with
printing after it, and an answer taken for the ready answer that is not
it, one still queued when the fence arrived or one owed from before a
late DLE EOT answer that stood in for the fence, followed by a pause of
more than 100 ms while the printer reads online, as might happen during
the TM-m50II's feed and cut at cover close. It relies on the printer
answering DLE EOT on receipt and running GS a 0 in order,
unverified beyond the TM-T88IV until stage 4a's raw-byte probe, and now
says a send that fails part-way can print a whole receipt twice. The
unit and install.md give the stop timeout with and without state.dir:
without it the job being sent runs on from its rendering, image fetches
included, up to the job's 2-minute cap, so such hosts keep systemd's
default or set state.dir. configuration.md says the online check can
add 2 s to write_timeout. The printers plan says what Send now does
before a job, and its table of what a failed send can mean gains the
ready wait and the confirmation.

The tests' fake printers answer DLE EOT 1 and GS r 1 as an online
printer with paper does; a test runs the daemon against one that
answers that it is ready but never confirms the job. On paper, a
receipt read done 884 ms after it was written, when the printer
answered, and a receipt whose daemon was killed while it waited, with
the cover open, read uncertain after the restart and printed once, cut
short. With GS a 0 and DLE EOT 1, a receipt printed once with nothing
extra and read done 888 ms after it was written; after a power cycle,
with /healthz polled every 250 ms, one sent with the cover open waited
15.7 s with nothing sent and printed once, cut once, when the cover
closed. With the fence, a receipt printed once with nothing extra and
read done 921 ms after it was written.

Part of stage 1d 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 21:28:38 -06:00
1 parent 9376653e1a
commit d3c3025e64
19 files changed
+429 -68

No files matched your search

+40 -14
View File
@@ -123,17 +123,33 @@ curl -H @"$HOME/.uberbringer-auth" https://printer.example.com/api/v1/jobs/job_0
}
```
`status` is `queued`, `printing`, `done`, `failed` or `uncertain`. A failed job
includes an `error` field. `uncertain` means the daemon stopped while it was
sending the job to the printer, so the receipt may have printed; its `error`
says so. Such a job is never sent again: if the paper shows it missing, send
it again. `sent_at` is when the first attempt was about to write to the
printer. A failed job with `sent_at` reached the printer, and part of it may
have printed; one without it never reached the printer.
`source` says where the job came from: `print` for
`/api/v1/print`, `module:<name>` for a [module](#modules) route, or
`ntfy:<name>` for an [ntfy subscription](ntfy.md)
in the config. The last `queue.history` finished jobs are kept.
`status` is `queued`, `printing`, `done`, `failed` or `uncertain`. A job is
`done` once the printer has confirmed that it ran it: after each job the
daemon asks the printer for its status, which it answers only once every
byte before the question has run (with `printer.confirm = false`, `done`
means only that the job was written to the printer's connection). The
answer names no job, so before each job the daemon drops the answers
the printer still owed to earlier questions: those it sent before its
answer to a real-time question asked first, and those that come within
100 ms of each other after it. It then checks with `DLE EOT 1` that the
printer is online, and if another answer comes it sends nothing and
tries again; each job waits about 200 ms for these checks. A
[`raw`](#raw) element can still let a job read `done` before it has
printed; docs/design.md describes the one other case, where the printer,
still reporting online, pauses more than 100 ms between two answers,
which is unverified.
A failed job includes an `error` field. `uncertain` means
the receipt may have printed in full, in part or not at all: the daemon
stopped while it was sending the job, or the printer did not confirm it
within `printer.write_timeout`, as when its cover is open; its `error`
says which. Such a job is never sent again: if the paper shows it missing,
send it again. `sent_at` is when the first attempt was about to write
to the printer. A failed job with `sent_at` reached the printer, and
part of it may have printed; one without it never reached the printer.
`source` says where the job came from: `print` for `/api/v1/print`,
`module:<name>` for a [module](#modules) route, or `ntfy:<name>` for an
[ntfy subscription](ntfy.md) in the config. The last `queue.history`
finished jobs are kept.
With [`state.dir`](configuration.md#state) set, jobs are kept on disk, so
a job's status answers after a restart as before it, and a job still
@@ -250,9 +266,11 @@ Details:
- **Brief problems are re-checked.** A printer that looks not ready is read
again 750 ms later, and only a problem that is still there gives `503`.
While cutting or feeding, a printer can report a problem for a moment.
- **Timeouts.** The printer accepts one connection at a time. While it is still
taking in a large job, the 3-second health check can run out of time. That
gives `503` with printer status `"timeout"`, because the printer's state is
- **Timeouts.** The printer accepts one connection at a time, and a job holds
it until the printer has printed it and confirmed so (`printer.confirm`),
or, while the printer cannot print, as the job waits for it to be ready.
When that takes longer than the 3-second health check, the check gives
`503` with printer status `"timeout"`, because the printer's state is
unknown.
- **The cover flag** is only reported while the printer is offline. Epson
printers always go offline when the cover opens.
@@ -408,6 +426,14 @@ or link-local addresses (including cloud metadata endpoints) are refused unless
|-------|--------|
| `hex` | ESC/POS bytes as hex, spaces allowed, e.g. `"1B 61 01"` |
Bytes that make the printer answer with a byte whose bits 4 and 7 are
clear, such as `GS r`, `GS I`, `ESC v` or `ESC u 0`, can be taken for the
printer's confirmation of the job (`printer.confirm`) when printing follows
them, so the job can read `done` before it has printed, or `uncertain`
though it printed. Answers they leave owed can do the same to the next
job: before each job the daemon drops the answers still owed to earlier
requests, but only those that come within 100 ms of each other.
The bytes are sent as given, so a job with a `raw` element prints only with
a token issued with `uberbringer token --raw`; any other token gets `403`
`token_scope`. Previewing one needs no such token.