Captures now get a WebP thumbnail: 400px on the long edge, written to
thumbs/<uuid>_400.webp (SPEC 3.3). /ingest enqueues the job in the same
transaction as the capture row, so the worker never sees a job for a capture
that was rolled back, and decoding stays off the request path - it is the one
genuinely expensive part of a capture, and the device is waiting with a burst
still to upload.
api/images.py moves up to poorbird/images.py. Both sides use it now, and a
worker importing from api would be the wrong way round. The split by cost
stays: everything the ingest path touches still reads nine bytes of JPEG
header, and Pillow appears only in the thumbnail half.
Handler behaviour is about which failures are worth retrying:
- An undecodable frame returns rather than raises. The handler contract
makes a raise mean "retry", and a frame that will never decode should not
spend three attempts proving it. Logged, thumb_path left NULL.
- A missing source file raises, because it should be there and a mount that
has not come back yet is worth retrying for.
- A capture deleted between enqueue and run is not an error. Retention will
do that routinely in Phase 5.
- A malformed payload is validated with a Pydantic model and recorded, not
retried (SEC-003 applied to a job payload).
- Never upscales: blowing a 100px frame up to 400px produces a blurry lie.
- Fills in width/height when the ingest header parse could not. This is the
first full decode, so it is the chance to fix that.
Verified end to end against a real device row, a real HTTP ingest and the
real worker dispatch path - claim, _run_one, complete - not just the handler
called directly: 1600x1200 JPEG in, WEBP 400x300 on disk, job marked done.
That run is what caught the rest of this commit.
Tests were leaking jobs into the shared queue, which broke test_queue.py once
/ingest started enqueuing. Three separate causes, all the same shape as the
teardown bug fixed in 269a6ea:
- test_ingest cleaned captures but not their thumbnail jobs, and one test
hand-rolled teardown for a second device and forgot half of it. Devices
now come from a fixture that cleans frames, captures and jobs for every
device a test makes.
- test_queue claimed jobs without checking they were its own, so a leftover
thumbnail job could be failed on its behalf, and one test claimed a
second job to prove the first did not recur and then stranded it in
'running' forever. It now enqueues at a priority that wins, asserts every
claim is its own, and releases anything else it touches.
The suite now runs twice in a row leaving zero jobs behind, which is the
assertion that actually matters here.
Tests: 175 -> 193.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
poorBird server
Phase 1 skeleton: configuration, schema, health, job queue and scheduler.
There are no device endpoints yet — /enroll, /ingest, /heartbeat,
/status and /config are Phase 2. The contract they must satisfy is
../firmware/API.md.
Stack per SPEC §6, deployed natively per ../TODO.md Phase 1.2:
FastAPI + PostgreSQL 17 + SQLAlchemy 2.0 + Alembic, run as systemd units
behind Caddy. No containers — but nothing here blocks them either.
Developing on Windows (uv + Podman)
This is the verified setup on the dev machine. Python is managed by uv, so
nothing touches the system PATH or the registry, and PostgreSQL runs in a
throwaway Podman container. Neither changes how the project deploys — the app
still talks loopback TCP exactly as it will on the server.
winget install astral-sh.uv
uv python install 3.14.7
podman machine start
podman run -d --name poorbird-db -p 127.0.0.1:5432:5432 `
-e POSTGRES_USER=poorbird -e POSTGRES_PASSWORD=poorbird -e POSTGRES_DB=poorbird `
docker.io/library/postgres:17
cd server
uv venv --python 3.14.7
uv pip install -e ".[dev]"
# write .env (see the Linux section below; use Windows paths for the roots)
.venv\Scripts\poorbird check
.venv\Scripts\poorbird migrate
.venv\Scripts\python -m pytest
Later sessions only need podman machine start and podman start poorbird-db.
Try it on any Linux machine
Needs Python ≥ 3.11 and a PostgreSQL you can reach. Production targets 3.13; the floor is lower so this runs on a stock Debian 12 or Ubuntu 24.04 box.
sudo apt install -y python3-venv postgresql
sudo -u postgres psql <<'SQL'
CREATE ROLE poorbird LOGIN PASSWORD 'poorbird';
CREATE DATABASE poorbird OWNER poorbird;
SQL
cd server
python3 -m venv .venv && . .venv/bin/activate
pip install -e '.[dev]'
cat > .env <<'ENV'
POORBIRD_DATABASE_URL=postgresql+psycopg://poorbird:poorbird@127.0.0.1:5432/poorbird
POORBIRD_IMAGE_ROOT=/tmp/poorbird/images
POORBIRD_THUMB_ROOT=/tmp/poorbird/thumbs
POORBIRD_STATE_DIR=/tmp/poorbird/state
POORBIRD_API_PORT=8090
ENV
mkdir -p /tmp/poorbird/{images,thumbs,state}
poorbird check # config, paths, database — before starting anything
poorbird migrate # alembic upgrade head
poorbird serve # http://127.0.0.1:8090/api/v1/health
In a second shell:
cd server && . .venv/bin/activate
poorbird worker &
poorbird enqueue noop '{"hello": "world"}' # the worker logs it within ~2s
poorbird check is the fast answer to "is this machine set up correctly" — it
validates config, storage paths and database connectivity and prints what it
found, without starting a service.
Tests
pytest # everything reachable
pytest -m 'not db' # no PostgreSQL needed
Queue and schema tests need a real PostgreSQL — FOR UPDATE SKIP LOCKED has
no equivalent elsewhere, and mocking it would test nothing. They skip
automatically when the database is unreachable, so a green run with no
Postgres proves less than it looks. Check for skipped in the summary;
a complete run is 12 passed, 0 skipped.
test_schema_matches_models.py creates and drops a poorbird_schema_check
database on the same server. It needs a role that may CREATE DATABASE.
Layout
src/poorbird/
config.py every value from the environment, nothing baked in
logging_setup.py stdout only; journald or a log driver catches it
db.py engine + session factory (sync, deliberately)
models/ SPEC §3 schema + the SPEC §0 additions
api/ FastAPI app; Phase 2 mounts device routes here
worker/ queue (SKIP LOCKED), handlers, scheduler
cli.py serve | worker | migrate | check | enqueue
migrations/ Alembic
deploy/ systemd units, Caddyfile, env template, INSTALL.md
tests/
Decisions worth knowing before you read the code
Sync SQLAlchemy, not async. At one camera and a few thousand frames a day
the async machinery buys nothing and costs clarity, and the worker's claim
loop is far easier to reason about synchronously. FastAPI runs def handlers
in a threadpool, so this does not block the event loop. The one thing that may
argue otherwise later is the MJPEG relay in Phase 6.
Device config is one JSONB column, not forty. GET /config returns ~40
optional keys and the firmware merges and clamps partial documents, so a
column per key would mean a migration every time the firmware gains a tunable.
Validation belongs in a Pydantic model, not in the DDL. The trade: you cannot
index or constrain an individual setting in SQL. If one ever needs querying
across devices, promote just that key to a column.
Server defaults are compared through a normaliser. Postgres reflects a
varchar default as 'viewer'::character varying while the model renders
'viewer'. Compared literally, every string default in the schema reads as
drift forever — which trains you to ignore the one time it isn't. See
schema_compare.py; autogenerate and the drift test share it, so the tool and
the test can never disagree about what counts as a difference.
Migration 0001 builds from Base.metadata. A bounded exception, and
tests/test_schema_matches_models.py migrates a scratch database and asserts
an empty autogenerate diff, so the models and the migration cannot silently
diverge. Every later revision is a normal autogenerated one. If you prefer
explicit DDL in the baseline, autogenerate it against an empty database and
replace the file — the drift test passes either way.
The scheduler only enqueues, never executes. Every unit of work goes through the queue, where it is visible, retried and auditable, rather than running inside a timer callback where a failure disappears.
Two things the code already guards, and the UI must too
battery_valid = falsemeans not measured — no divider is wired by default. Render it as "—", never as a low-battery alarm.- The daily counters live in the device's RTC memory and reset on power loss.
capturesis authoritative. The counters' value is the gap between captured and delivered, which is an upload-health signal.
Deploying for real
See deploy/INSTALL.md.