feat(printer): badge print queue, Pi worker, captive-portal setup, admin printing view

- migration 20261018170000: pgmq badge_print/badge_print_dlq, print_stations,
  token-gated printer RPCs (claim/complete/fail/heartbeat), admin RPCs,
  welcome_checkin enqueue; badge_print_tests.sql (101 assertions)
- apps/printer: Python worker (Playwright render -> brother_ql via pyusb),
  comitup captive-portal (Wi-Fi + HTTPS endpoint + anon key + token),
  arm64 image build + idempotent Pi setup/install scripts, 77 pytest
- apps/admin: Printing view, station management/DLQ, global printer status
  pill, Print button on Registrations, standalone print renderer entry
  (print.html -> dist-print) with bundled fonts
- docs: printing.md + architecture/schema/rpcs/tests/admin-app updates,
  apps/printer/AGENT.md operating manual
- e2e: printing.spec.ts
This commit is contained in:
Lago committed 2026-10-18 22:25:38 +02:00
1 parent 04f3f55b78
commit c980effddc
55 files changed
+7406 -29

No files matched your search

+103
View File
@@ -97,3 +97,106 @@ touched.
absent from the served JS, `give-feedback` still present (non-service path).
- **45/45** mocked Playwright checks against `https://convivo.appe.at`.
---
# Badge Printing End-to-End (2026-10-18)
Welcome-desk badge printing on a Raspberry Pi: scan a lanyard QR, the badge
prints seconds later. Full reference: [backend/printing.md](../backend/printing.md).
**Status: local only — production deployment pending at the time of writing.**
## What shipped
- **Database** — migration
[`20261018170000_badge_print_queue.sql`](../../supabase/migrations/20261018170000_badge_print_queue.sql):
`pgmq` queues `badge_print` / `badge_print_dlq`, the `print_stations` table
(SHA-256 token digests, RLS with no policies), the self-contained
`convivo.badge-print/v1` payload, `welcome_checkin` re-created to enqueue the
badge on a **first** check-in in the same transaction, seven admin RPCs
(reprint, station create/rename/revoke, overview, DLQ requeue/discard) and
four token-gated printer RPCs (claim / complete / fail / heartbeat).
- **Admin** — the **Printing** view (stations, queue counters, recent jobs with
Reprint, dead letters with Requeue/Discard), the always-visible sidebar
**printer status pill** with staff-facing hints, the **Print** button on
Registrations rows, and the standalone print renderer entry (`print.html` →
`window.renderBadge`, built by `npm run build:print`) sharing the editor's
`BadgeRender` code and **bundled @fontsource fonts**, so preview = print on
any machine.
- **Worker** — `apps/printer/`: Python in Docker (repo-root build; a Node stage
builds the renderer into the Playwright Chromium runtime image). Long-polls
the queue with the station token, screenshots the badge at 300 dpi, composes
it onto the **detected** roll (die-cut exact canvas / endless cut-to-length /
auto-rotate / `LayoutTooLarge` → DLQ), prints on a Brother QL-1110NWB over
USB with status read-back, and heartbeats every 10 s (the station name comes
from the heartbeat response, so renames apply without re-provisioning).
- **Station rename** — `rename_print_station` RPC + inline rename in the
Printing view.
- **Pi provisioning + Wi-Fi portal** — `apps/printer/pi/setup.sh` +
`install.sh` (idempotent, staged): Docker, watchdog, `usblp`/`ipp-usb`
handling, the worker container, and **comitup** providing the open AP
`Convivo-Printer-<nnnn>` with a captive portal at **http://10.41.0.1**:
**pick any Wi-Fi + optional Convivo settings** (URL / anon key / station
token) from a phone. A comitup callback writes `/run/convivo/network.json`
for the worker. Operational detail:
[`apps/printer/AGENT.md`](../../apps/printer/AGENT.md).
- **Status labels (opt-in)** — `STATUS_LABELS=1` (default off) prints
locally-drawn "ready" / "needs Wi-Fi" labels on the printer itself, since
the Pi has no screen.
- **Fixes found on real hardware** — claim long-poll clamped to **2 s**
(Supabase `anon` has a 3 s `statement_timeout`; 10 s caused HTTP 500
`57014`); setup re-execs systemd only on config change and restarts comitup
after (a bare `daemon-reexec` broke the hotspot's DHCP); Docker restarted
only on `daemon.json` change (a bounce re-ran the worker on the old image);
install uploads to `/var/tmp` (the Pi's `/tmp` is a ~1 GB tmpfs); `ipp-usb`
masked (it grabbed the printer's USB interfaces — "Resource busy").
## Validation
- **SQL:** `supabase/tests/badge_print_tests.sql` — rollback-only, run combined
with the migration: **101/101 assertions** (payload shape, layout resolution,
first-scan enqueue, token lifecycle, retry/backoff/DLQ, heartbeat, role
guards, idempotent re-apply).
- **Worker:** pytest `apps/printer/tests` — **77 passing** (56 test
functions, 77 with parametrized label cases; no hardware or browser required).
- **Browser:** `e2e/tests/printing.spec.ts` — **15/15** API-mocked Playwright
tests (pill states + mobile, token-shown-once, rename, requeue/discard,
reprint, Print button, 5 s polling).
- **Real Pi end-to-end:** a provisioned Pi 4 with a QL-1110NWB printed live
badges against the local Supabase stack — welcome check-in → queue → print →
archive, plus reprint, revoke (token rejected immediately), rename visible on
the next heartbeat, and the DLQ path.
- **Portal:** joined the open `Convivo-Printer-<nnnn>` AP from a phone, picked
a Wi-Fi network at http://10.41.0.1, and the worker reconnected and resumed
claiming on the new network.
- Neighbour regression green: `badge_layout_tests.sql` (15 assertions — the
print payload reuses badge layouts).
## Deployment
**Deployed to production 2026-10-18** (commit `a2fe724`, admin-only + DB migration).
- **Migration** `20261018170000_badge_print_queue.sql` applied to prod in a
single transaction with `ON_ERROR_STOP=1` and recorded in the ledger
(ledger 38 → 39). `pgmq 1.5.1` was already available on the prod image (no
`shared_preload_libraries` change needed); the queues `badge_print` /
`badge_print_dlq`, `print_stations`, and all admin/printer RPCs are present.
Backup `/home/APPE/convivo-backups/printqueue-20261018T201701Z` (pg_dump,
counts, runtime admin source, rollback tag `convivo-admin:rollback-printqueue-…`).
- **SQL regression** `badge_print_tests.sql` run rollback-only against prod in
the same transaction as the migration: **101/101 assertions**, then rolled
back — `print_stations`/queues/events/counts unchanged afterwards.
- **Admin** rebuilt from HEAD with a baseline drift guard (all 9 changed prod
files byte-identical to `HEAD~1` → whole-file copy safe). Only
`convivo-admin` recreated; every unrelated container ID preserved. Live
bundle `index-pPo7RVlo.js` is byte-identical to the local HEAD build.
- **Station** `Welcome desk` provisioned in prod (token shown once, only its
SHA-256 stored). `printer_heartbeat` returns 200 over PostgREST with the
anon key + bearer token.
- **Pi** reconfigured from the local dev stack to `https://convivo.appe.at`
(`BADGE_CHECKIN_ORIGIN=https://admin.appe.at`); the running worker image
already matched HEAD, so no rebuild was needed — only `.env` swap +
`docker compose up -d --force-recreate`. The station now heartbeats to prod
(`last_seen_at` fresh, `last_status` reporting printer ready).
Troubleshooting lessons are collected in [printing.md §11](../backend/printing.md#11-troubleshooting-lessons-learned).