Files
57_WolveandClaude Opus 5.5 d3c3025e64 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>
2026-12-01 21:28:38 -06:00

285 lines
18 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Configuration
The daemon reads its settings from a TOML file and from environment
variables. [configs/config.example.toml](../configs/config.example.toml)
shows every setting with its default: copy it to `config.toml` to start.
`install.sh` and the Docker image keep the config in
`/etc/uberbringer/config.toml`.
## Contents
- [Loading](#loading)
- [Environment variables](#environment-variables)
- [Settings](#settings): [webserver](#webserver), [api](#api),
[printer](#printer), [format](#format), [ntfy](#ntfy),
[ntfy subscriptions](#ntfy-subscriptions), [queue](#queue),
[state](#state), [images](#images), [sentry](#sentry), [log](#log)
- [Logs](#logs)
## Loading
- Only `api.secret` and `printer.address` are required. Every other setting
has a default.
- Unknown keys are errors, so a typo can't silently fall back to a default,
and so are two keys that differ only in case.
- A duration is a string such as `"5s"` or `"1m30s"`; a bare number is
refused.
- `--config` names the file, and defaults to `config.toml` or to
`UBERBRINGER_CONFIG` when that is set. `--config ""`, or an empty
`UBERBRINGER_CONFIG`, reads no file, so the settings come from the
environment and the defaults alone.
- [`uberbringer config get <key>`](cli.md#config-get) prints a setting as
the daemon would use it.
## Environment variables
Every key can also be set by an environment variable named
`UBERBRINGER_<SECTION>_<KEY>`, such as `UBERBRINGER_PRINTER_ADDRESS` for
`printer.address`, and a variable wins over the file. The exception is the
[ntfy subscriptions](#ntfy-subscriptions), a list of tables, which only the
file sets.
- Values are written without quotes: decimal digits for a number, `true` or
`false` for a flag, `5s` for a duration. Lists take comma-separated values,
e.g. `UBERBRINGER_WEBSERVER_TRUSTED_PROXIES=127.0.0.1,::1`; an empty item,
from a doubled or trailing comma, is an error. In the file, a number, flag
or list may also be written as such a string.
- A table of your own entries, `ntfy.markers`, takes `name=value` entries
separated by commas, e.g. `UBERBRINGER_NTFY_MARKERS=backup=[B],nas01=`; a
value cannot hold a comma.
- An empty variable counts as unset, and one that holds only spaces is an
error. Variables that name no key are ignored, but when the configuration
fails to load, the error names them, since they are likely misspelt.
## Settings
### webserver
| Key | Default | Meaning |
|-----|---------|---------|
| `bind_ip` | `"127.0.0.1"` | The IP address the HTTP server listens on; `"0.0.0.0"` in [Docker](docker.md) |
| `port` | `8080` | The port it listens on, 1–65535 |
| `rate_limit`, `rate_window` | `60`, `"1m"` | Each client may make this many `/api` requests per window; more get `429` `rate_limited` |
| `trusted_proxies` | `["127.0.0.1", "::1"]` | IP addresses or CIDR ranges of reverse proxies. A request from one of them is counted, for the rate limit, against the client address in `proxy_header` |
| `proxy_header` | `"X-Real-IP"` | The header the proxy sets to the client's address. `install.sh` sets Caddy up to send it |
| `body_limit` | `4194304` | The largest request body, in bytes (4 MiB); a larger one gets `413` `body_too_large`. Raise it for large base64 images, and raise the proxy's limit with it |
| `read_timeout`, `write_timeout`, `idle_timeout` | `"15s"`, `"15s"`, `"60s"` | The HTTP server's timeouts |
`install.sh` copies `bind_ip`, `port` and `body_limit` into Caddy's config,
so on a host it set up, re-run it after changing them
([install.md](install.md#upgrading-and-changing-the-config)).
### api
| Key | Default | Meaning |
|-----|---------|---------|
| `secret` | none | Required. The key that signs bearer tokens (HS256), at least 32 bytes. `uberbringer secret` makes one; see [security.md](security.md#the-api-secret) |
### printer
| Key | Default | Meaning |
|-----|---------|---------|
| `address` | none | Required. The printer's IP address or host name |
| `port` | `9100` | The printer's TCP port, 1–65535 |
| `model` | `"tm-t88"` | The printer's profile, which sets the print width and characters per line; see below |
| `codepage` | `"cp437"` | The code page text is transcoded to: `cp437`, `cp850`, `cp858` (has €) or `cp1252` |
| `paper_width` | the model's | The printable width in dots; overrides the model |
| `columns` | from the width | Font A characters per line, for word wrap; overrides the model |
| `box_line_spacing` | `0` | The line spacing, in motion units (ESC 3, 0–255), used while a box prints. Lines of box drawing leave small gaps in the vertical borders at the printer's default spacing; a value checked on paper closes them. `0` keeps the default |
| `cut_feed` | `3` | Blank lines, 0–255, fed before every cut, the one after each copy and `cut` elements alike, so that the last printed line does not sit right at the cut. They show in previews and count toward `format.max_lines` |
| `dial_timeout`, `write_timeout` | `"5s"`, `"30s"` | How long connecting to the printer, and sending it a job, may take. With `confirm`, `write_timeout` also bounds the wait for the printer to be ready before a job, which the check that it is online can extend by up to 2 s, and the wait for its confirmation after it, which comes once the job has printed: raise it for receipts that take longer to print, and the stop timeout with it. A job's whole run is capped at 2 minutes, so a larger value changes nothing |
| `confirm` | `true` | Ask the printer for its status with `GS r 1`, which it answers only once it has run every byte before it, and, on the TM-T88IV, only while it can print. Before each job, ask `DLE EOT 1`, which the printer answers at once, then `GS r 1`, drop whatever the printer sent before the `DLE EOT` answer, and wait up to `write_timeout` for the answer to `GS r 1`: without it, as with the cover open or no paper, nothing is sent and the job is retried, then fails, as when the printer is unreachable. Before each job, also turn the printer's automatic status off (`GS a 0`) and, once it has answered, check with `DLE EOT 1` again that it is online and that no answer comes late. These checks take about 200 ms per job. After each job, turn automatic status off again and ask again, and mark the job `done` only on the answer; one the printer does not confirm within `write_timeout` is `uncertain` and is not sent again. Checked on the TM-T88IV. `false`, for a printer that does not answer `GS r` with bits 4 and 7 clear or does not answer `DLE EOT` on receipt, sends none of these and marks a job `done` once the operating system has taken its bytes, which a stop or a crash can still lose |
| `retries` | `3` | How many times a failed send is retried before the job fails |
| `retry_delay` | `"1s"` | The wait before the first retry; it doubles after each |
The models:
| Model | Printers | Width | Font A / B columns |
|-------|----------|-------|--------------------|
| `tm-t88` (default) | TM-T88IV, V, VI, VII | 512 dots (180 dpi) | 42 / 56 |
| `tm-m50` | TM-m50, TM-m50II | 512 dots (180 dpi) | 42 / 56 |
| `tm-t20` | TM-T20, II, III | 576 dots (203 dpi) | 48 / 64 |
| `generic-80mm` | other 80 mm, 203 dpi | 576 dots | 48 / 64 |
| `generic-58mm` | 58 mm paper, 203 dpi | 384 dots | 32 / 42 |
For 58 mm paper on a 180-dpi printer (TM-T88VII, TM-m50II), use a `tm-t88`
or `tm-m50` model with `paper_width = 360` (30 Font A columns).
If words break at the right edge (for example `seve` on one line and `ral`
on the next), the model is wider than the printer.
### format
| Key | Default | Meaning |
|-----|---------|---------|
| `max_lines` | `2000` | A job that would print more lines, all copies counted, fails instead of printing, and `/api/v1/preview` answers it with `422`. It is a backstop against runaway text |
| `timezone` | `"Local"` | The zone of the times [modules](api.md#modules) print, such as a note's stamp: an IANA name such as `Europe/London` or `UTC`, or `Local` for the host's zone. The binary carries the zone database, so names load on hosts and containers without one |
| `time_layout` | `"02 Jan 15:04"` | How those times print, in Go's notation, in which `02 Jan 2006 15:04:05` stands for the parts of a date, e.g. `2006-01-02 15:04`. A layout that shows no part of a date is an error |
### ntfy
These settings shape the [notification cards](api.md#notification-cards)
of the `notify` and `ntfy` modules and of the [ntfy subscriptions](ntfy.md),
and their [digests](api.md#digests).
`[ntfy.format]`:
| Key | Default | Meaning |
|-----|---------|---------|
| `style` | `"auto"` | `auto` picks the form from the priority; `box` prints priorities 1 and 2 in a box too; `compact` prints every priority without a box |
| `max_lines` | `40` | 1–400 lines of the message, as written, before `... N more lines` |
| `qr` | `"all"` | Which links print as QR codes: `all`, `first` (the rest as text) or `none` |
| `images` | `true` | `false` names attached images on a `File` row instead of printing them |
| `markdown` | `true` | `false` prints markdown messages as written |
| `updates` | `"print"` | What a subscription does with a message that replaces one it printed: `print` marks its title `(updated)`, `skip` leaves it out |
`[ntfy.markers]` adds or changes the [markers](api.md#markers) printed for
tags, at most 16 characters each, and `""` hides a tag: `backup = "[B]"`,
`nas01 = ""`.
### ntfy subscriptions
Each `[[ntfy.subscriptions]]` table is one connection to one ntfy server,
for one or more topics. They are set in the file only, and need
[`state.dir`](#state). [ntfy.md](ntfy.md) shows an example and how a
subscription behaves.
| Key | Default | Meaning |
|-----|---------|---------|
| `name` | required | 1–32 lowercase letters, digits, `-` and `_`. It names the subscription in `/healthz`, in logs, in its state file and in its jobs' source, `ntfy:<name>`; each must be unique |
| `server` | required | The server's base URL. It must be `https` unless `allow_insecure = true`. A redirect is never followed: the connection fails, and `last_error` says to set the address it points to |
| `allow_insecure` | `false` | `true` allows a plain `http` server, whose credentials and messages cross the network unencrypted |
| `topics` | required | 1–150 topic names, each 1–64 letters, digits, `-` and `_` |
| `token`, `token_file`, or `user` with `password_file` | none | The credentials, at most one kind. A file holds the secret alone; spaces and line breaks around it are ignored, and a file the group or others can read is accepted with a warning. Without any, the subscription reads as anyone may |
| `min_priority` | `1` | The lowest priority that prints; the server filters by it |
| `max_backlog` | `"1h"` | How far back messages missed during a restart or an outage still print, at most `12h`, the time ntfy keeps them by default |
| `rate_limit`, `rate_window` | `5`, `"10m"` | The paper budget: at most this many receipts (1–1000) in any window. Messages over it are held, and print together on one [digest](api.md#digests) once the budget has room |
| `bypass_priority` | `5` | Outside quiet hours, messages of this priority and above print at once, outside the budget. During quiet hours, `quiet.min_priority` decides first |
| `keepalive` | `"45s"` | The server's keepalive interval, 5s–1h. A connection silent for twice this plus 15 s is opened again |
Quiet hours are off unless the subscription has a
`[ntfy.subscriptions.quiet]` table, which goes right after its
`[[ntfy.subscriptions]]` table:
| Key | Default | Meaning |
|-----|---------|---------|
| `start`, `end` | `"22:00"`, `"07:00"` | The window, as HH:MM. An end before the start is on the next day; the times hold on the nights the clocks change |
| `days` | every day | The days a window starts on: `mon`, `tue`, `wed`, `thu`, `fri`, `sat`, `sun`. With `["fri"]`, Friday's window runs into Saturday morning |
| `timezone` | `format.timezone` | The zone of `start` and `end` |
| `min_priority` | `5` | The lowest priority that prints during the window |
| `action` | `"digest"` | `digest` holds the other messages and prints them on one `Overnight` digest when the window ends; `drop` leaves them out |
### queue
| Key | Default | Meaning |
|-----|---------|---------|
| `size` | `100` | Jobs that may wait to print; more get `503` `queue_full` |
| `history` | `1000` | Finished jobs whose status is kept; an older job's ID answers `404` `job_not_found` |
| `content_bytes` | `67108864` | The bytes of finished jobs' content kept, newest first, each job as its JSON: 64 MiB, at most 1 GiB (`1073741824`). Jobs not yet finished keep theirs whatever the total, and so do `uncertain` jobs while their status is kept. The content is kept in the [job store](#state) with `state.dir`, and in memory without it |
### state
| Key | Default | Meaning |
|-----|---------|---------|
| `dir` | none | The directory of the job store, `jobs.db`, and of the [ntfy subscriptions](#ntfy-subscriptions)' state, one `ntfy-<name>.json` file each, all readable by the daemon's user only. Empty keeps jobs in memory. Required with a subscription, and never only spaces. When it is set, the daemon creates it, readable by its user only, if it is missing, and does not start if it cannot write there. `install.sh` and the Docker image set it to `/var/lib/uberbringer` |
With `state.dir`, the daemon keeps every job in the job store, `jobs.db`, a
SQLite database, with its log `jobs.db-wal` beside it:
- Jobs still waiting when the daemon stops or crashes print after the next
start, in order, however old, and every kept status answers after a
restart. A job that was being sent to the printer when the daemon stopped
reads `uncertain` and is never sent again
([api.md](api.md#job-status)). Without `state.dir`, waiting jobs are
marked failed when the daemon stops, and forgotten after a crash.
- A job accepted from the HTTP routes is written to disk within about 30
seconds, or sooner with the next job sent to the printer, so a power cut
just after its `202` can lose it, but never prints it twice. A job from
an ntfy subscription is on disk before it is queued.
- One daemon uses a directory at a time: a second one on it does not
start. No other program, the `sqlite3` command included, can read the
database while the daemon runs.
- The store holds what the receipts print, each job as its JSON, never a
token. Its files are readable by the daemon's user only, and the content
of a job that is gone can linger in the file's free pages until they are
reused. `queue.content_bytes` bounds how much content is kept.
- A store that cannot be opened, such as a damaged one, stops the daemon
with an error naming the file. [install.md](install.md#a-job-store-that-will-not-open)
and [docker.md](docker.md#a-job-store-that-will-not-open) say how to set
it aside.
- `jobs.db` and `jobs.db-wal` belong together: never delete the log, or
copy the database without it.
- Going back to 0.2.0: it refuses every key added since as unknown, such
as `[state]`, `queue.content_bytes`, `printer.cut_feed` and the `ntfy`
tables, so remove them first, and it never reads `jobs.db`, so the jobs
waiting in it do not print.
### images
| Key | Default | Meaning |
|-----|---------|---------|
| `timeout` | `"10s"` | How long fetching an image may take |
| `max_bytes` | `10485760` | The largest image download, in bytes (10 MiB) |
| `max_pixels` | `25000000` | The largest decoded image, in pixels, against decompression bombs |
| `max_height` | `4000` | The tallest printed image, in dots after scaling, about 50 cm |
| `allow_private_networks` | `false` | `true` lets image URLs reach loopback, private and link-local addresses |
| `allowed_hosts` | `[]` | When set, the only hosts images may come from; those hosts may be on your LAN |
An image that breaks a limit fails its job, unless the image is `optional`
([image](api.md#image)).
### sentry
| Key | Default | Meaning |
|-----|---------|---------|
| `dsn` | none | Where errors are reported. When neither the file nor `UBERBRINGER_SENTRY_DSN` sets it, it falls back to the `SENTRY_DSN` environment variable, then to a DSN built into release binaries |
| `environment` | `"production"` | The environment events are reported under |
| `debug` | `false` | `true` has the Sentry client log what it does |
Request bodies and the `Authorization` header are never sent to Sentry.
### log
| Key | Default | Meaning |
|-----|---------|---------|
| `level` | `"info"` | `debug`, `info`, `warn` or `error` |
| `format` | `"text"` | `text` or `json` |
## Logs
- Each request is logged once, as `request`: at INFO, or at WARN when the
status is 5xx. Only the path is logged, never the query string, the body
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, or a digest's own reference, never with what
it prints.
- With `state.dir`, a start that finds jobs in the store logs
`print queue restored` at INFO, with how many `jobs` it holds, how many
are `waiting` and how many are `uncertain`. Each job that was being sent
when the daemon stopped is logged at WARN once, by its `job_id`,
`printer`, `source` and `ref`: it may have printed and is not sent again,
so check the paper. A job that a stop catches before it reached the
printer is logged at INFO as `job left queued` and prints after the
start.
- A job written to the printer in full that it does not confirm is logged
at WARN, as `job may not have printed in full`, with the printer's
error; it reads `uncertain` and is not sent again. If its receipt was cut
short, the printer may still be waiting for the rest of an image and
print the next receipt as noise: switch it off and on.
- 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
is full, and at DEBUG what happens to each message.
- An unexpected failure is logged at ERROR and reported to Sentry once: a
request that fails or panics, a job that fails after its retries
(`job failed`), a module that builds an invalid receipt, a notification
card or digest that prints its plain fallback, an ntfy state file that
cannot be written, and a job store that fails (`print queue stopped`),
which stops the daemon. A request whose job it could not store answers
`500` `internal_error`; the failure is logged and reported with the
stop, not again for the request.
- Refusals are never logged at ERROR and never go to Sentry. They are the
4xx answers other than `module_failed`, the `queue_full` and
`shutting_down` 503s, and 503s from `/healthz`.