Send each print as one job, queue web prints, and harden deploys
CI / test (3.10) (push) Failing after 56s
CI / test (3.12) (push) Failing after 42s
CI / test (3.13) (push) Failing after 47s

Render every print to a complete ESC/POS payload (reset, content, cut)
and send it once over a fresh connection. Only the connect is retried,
so a link that drops mid-job can no longer print a partial image followed
by a full one, and no text size leaks into the next job. Prints are
capped at about 1 m of paper: tall images shrink, over-long notes are
refused.

Replace the argparse/stdout plumbing with PrinterConfig, InvalidJob and
PrinterUnavailable, and one run_command() dispatcher shared by the CLI
and TUI. Status now really connects; before, escpos's lazy open meant
the web server always reported the printer ready. Rendering with the
TM-T20II profile makes QR centering take effect.

The web server validates and renders jobs in the request (400 on bad
input), queues them for a single worker and answers 202; the page
long-polls /api/jobs/{id}, so no request outlives nginx's proxy timeout
while its job still prints. Status reports busy instead of waiting, and
jobs are logged with the nginx user.

update.sh tests new commits in a scratch worktree before touching the
live clone, and rolls back if /healthz doesn't answer after the restart.
The unit runs from the venv, and the web server defaults to localhost.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 22:07:58 +01:00
co-authored by Claude Opus 5.5
parent 30d1b83817
commit 597bace442
10 changed files with 778 additions and 436 deletions
+10 -11
View File
@@ -64,7 +64,9 @@ fortune | ./receipt.py note
./receipt.py --ip 10.0.0.5 --port 9100 status
```
Images are prepared for thermal output automatically: transparency is composited onto white, then the image is resized to full width, autocontrast-stretched, and Floyd-Steinberg dithered.
Images are prepared for thermal output automatically: transparency is composited onto white, then the image is resized to full width, autocontrast-stretched, and Floyd-Steinberg dithered. Nothing prints longer than about 1 m: a very tall image is shrunk to that height (and centered), and a note that long is refused.
Each print is rendered to one complete ESC/POS job (printer reset, content, cut) and sent over a fresh connection. A failed connect is retried once; a connection that drops mid-job is reported, never resent, so a flaky link can't print the same thing twice.
## Printing from the website
@@ -98,13 +100,10 @@ sudo nmcli con add type ethernet ifname eth0 con-name receipt-printer connection
"$(openssl rand -hex 32)" "$(tailscale ip -4)" > ~/.config/receipt-web.env)
```
3. Install the units, point the server at the venv, and start everything at boot:
3. Install the units (the server runs from the venv) and start everything at boot:
```bash
cp deploy/receipt-web.service deploy/receipt-update.{service,timer} ~/.config/systemd/user/
mkdir -p ~/.config/systemd/user/receipt-web.service.d
printf '[Service]\nExecStart=\nExecStart=%%h/Coding/receipt_maker/.venv/bin/python %%h/Coding/receipt_maker/receipt.py serve\n' \
> ~/.config/systemd/user/receipt-web.service.d/venv.conf
systemctl --user daemon-reload
systemctl --user enable --now receipt-web receipt-update.timer
sudo loginctl enable-linger "$USER"
@@ -112,18 +111,18 @@ sudo nmcli con add type ethernet ifname eth0 con-name receipt-printer connection
4. On stpvm, set `RECEIPT_WEB_UPSTREAM=<tailscale ip>:8765` in web_server's `.env`, then `docker compose up -d nginx`.
Jobs run one at a time. If the server machine is off, the site shows "printer machine offline".
Jobs are checked and rendered when submitted (a bad note or image is rejected straight away), then queued and printed one at a time. The page shows each job as queued, printing, then done or failed, and the status pill shows "printer busy" during a print rather than waiting for it. Each job is logged to the service's journal with the signed-in user nginx forwards. If the server machine is off, the site shows "printer machine offline".
### Automatic updates
`receipt-update.timer` runs `deploy/update.sh` every 5 minutes. When `master` on Gitea has moved, the script:
1. fast-forwards the clone
2. reinstalls the dependencies if `pyproject.toml` changed
3. runs the tests
4. restarts `receipt-web`
1. runs the tests on the new commit in a scratch worktree (with a throwaway venv if `pyproject.toml` changed)
2. fast-forwards the clone and, if needed, reinstalls the dependencies
3. restarts `receipt-web`
4. waits up to 20 s for `/healthz` to answer
If the tests fail, the clone goes back to the previous commit, the old code keeps serving, and that commit is skipped until a newer one is pushed. Its output is in `journalctl --user -u receipt-update`. Pushing to Gitea is the whole deploy.
If the tests fail, the live clone is never touched. If `/healthz` doesn't answer, the previous commit is put back and restarted. Either way, the old code keeps serving and that commit is skipped until a newer one is pushed. Its output is in `journalctl --user -u receipt-update`. Pushing to Gitea is the whole deploy.
## Development