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
+1 -1
View File
@@ -15,7 +15,7 @@ Wants=network-online.target
[Service]
EnvironmentFile=%h/.config/receipt-web.env
ExecStart=/usr/bin/python3 %h/Coding/receipt_maker/receipt.py serve
ExecStart=%h/Coding/receipt_maker/.venv/bin/python %h/Coding/receipt_maker/receipt.py serve
# Binding fails until Tailscale has brought its address up; just retry.
Restart=always
RestartSec=5
+42 -13
View File
@@ -3,9 +3,10 @@
# when the code changed. Run every few minutes by receipt-update.timer on the
# machine the service runs on.
#
# The tests gate the restart: if they fail on the new commit, the clone goes
# back to the old one, the service keeps running the old code, and that
# commit is skipped until a newer one lands.
# The new commit is tested in a scratch worktree first, so the live clone and
# venv never hold untested code. After the restart, /healthz must answer or
# the old commit is put back. Either failure skips that commit until a newer
# one lands.
set -euo pipefail
cd "$(dirname "$0")/.."
@@ -19,18 +20,46 @@ if [ "$(cat "$failed_marker" 2>/dev/null)" = "$new" ]; then
exit 0
fi
git merge --ff-only --quiet "$new"
if ! git diff --quiet "$old" "$new" -- pyproject.toml; then
.venv/bin/pip install --quiet '.[web,dev]'
fi
if ! output=$(.venv/bin/python -m pytest -q -p no:cacheprovider 2>&1); then
echo "$output" | tail -n 20
echo "Tests failed on $(git rev-parse --short "$new"); staying on $(git rev-parse --short "$old")." >&2
git reset --quiet --hard "$old"
fail() {
echo "$1 on $(git rev-parse --short "$new"); staying on $(git rev-parse --short "$old")." >&2
echo "$new" >"$failed_marker"
exit 1
}
deps_changed() { ! git diff --quiet "$old" "$new" -- pyproject.toml; }
scratch=$(mktemp -d)
trap 'git worktree remove --force "$scratch" 2>/dev/null; rm -rf "$scratch"; git worktree prune' EXIT
git worktree add --quiet --detach "$scratch" "$new"
python=$PWD/.venv/bin/python
if deps_changed; then
# New dependencies get a throwaway venv; the live one is only touched once they pass.
python3 -m venv "$scratch/.venv"
"$scratch/.venv/bin/pip" install --quiet "$scratch[web,dev]"
python=$scratch/.venv/bin/python
fi
# pytest's pythonpath = ["."] makes the worktree's modules win over any installed copy.
if ! output=$(cd "$scratch" && "$python" -m pytest -q -p no:cacheprovider 2>&1); then
echo "$output" | tail -n 20
fail "Tests failed"
fi
git merge --ff-only --quiet "$new"
deps_changed && .venv/bin/pip install --quiet '.[web,dev]'
systemctl --user restart receipt-web
echo "Updated $(git rev-parse --short "$old") -> $(git rev-parse --short "$new")."
listen=$(sed -n 's/^RECEIPT_WEB_LISTEN=//p' "$HOME/.config/receipt-web.env" 2>/dev/null)
healthz="http://${listen:-127.0.0.1:8765}/healthz"
for _ in {1..20}; do
if curl -fsS --max-time 2 "$healthz" >/dev/null 2>&1; then
echo "Updated $(git rev-parse --short "$old") -> $(git rev-parse --short "$new")."
exit 0
fi
sleep 1
done
journalctl --user -u receipt-web -n 20 --no-pager >&2 || true
git reset --quiet --hard "$old"
deps_changed && .venv/bin/pip install --quiet '.[web,dev]'
systemctl --user restart receipt-web
fail "$healthz didn't answer after the restart"