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]>
66 lines
2.3 KiB
Bash
Executable File
66 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# Fast-forward this clone to its upstream (Gitea) and restart the web server
|
|
# when the code changed. Run every few minutes by receipt-update.timer on the
|
|
# machine the service runs on.
|
|
#
|
|
# 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")/.."
|
|
|
|
git fetch --quiet
|
|
old=$(git rev-parse HEAD)
|
|
new=$(git rev-parse '@{upstream}')
|
|
[ "$old" = "$new" ] && exit 0
|
|
|
|
failed_marker="$(git rev-parse --git-dir)/receipt-update-failed"
|
|
if [ "$(cat "$failed_marker" 2>/dev/null)" = "$new" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
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
|
|
|
|
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"
|