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>
126 lines
4.1 KiB
Python
126 lines
4.1 KiB
Python
"""Queue behaviour. Needs a real PostgreSQL: SKIP LOCKED has no equivalent
|
|
anywhere else, and mocking it would test nothing.
|
|
|
|
These share the ``jobs`` table with everything else, including /ingest, which
|
|
enqueues a thumbnail job per capture. So nothing here may assume it owns the
|
|
queue: test jobs are enqueued at a priority that puts them first, and every
|
|
claim is checked to be one of ours before anything is done to it. Acting on a
|
|
claim without that check could fail somebody else's job.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from conftest import needs_db
|
|
|
|
pytestmark = [pytest.mark.db, needs_db]
|
|
|
|
# claim() orders by priority DESC, so this wins against anything real.
|
|
TEST_PRIORITY = 1000
|
|
|
|
|
|
@pytest.fixture
|
|
def session():
|
|
from poorbird.db import SessionLocal
|
|
from poorbird.models import Job
|
|
|
|
with SessionLocal() as s:
|
|
yield s
|
|
s.query(Job).filter(Job.kind.like("test_%")).delete(synchronize_session=False)
|
|
s.commit()
|
|
|
|
|
|
def claim_ours(queue, session, kind: str) -> dict:
|
|
"""Claim, and insist it is the job this test enqueued."""
|
|
claimed = queue.claim(session)
|
|
assert claimed is not None, "our own job should have been claimable"
|
|
assert claimed["kind"] == kind, f"claimed {claimed['kind']!r}, not our {kind!r}"
|
|
return claimed
|
|
|
|
|
|
def test_claim_returns_a_queued_job_then_nothing(session):
|
|
from poorbird.worker import queue
|
|
|
|
queue.enqueue(session, "test_noop", {"n": 1}, priority=TEST_PRIORITY)
|
|
session.commit()
|
|
|
|
claimed = claim_ours(queue, session, "test_noop")
|
|
assert claimed["payload"] == {"n": 1}
|
|
assert claimed["attempts"] == 1
|
|
|
|
queue.complete(session, claimed["id"])
|
|
|
|
# Same job must not come back. Anything else that turns up belongs to
|
|
# another test, so put it back rather than stranding it in 'running' --
|
|
# a claim that is never released is invisible until something else
|
|
# mysteriously stops being processed.
|
|
again = queue.claim(session)
|
|
assert again is None or again["kind"] != "test_noop"
|
|
if again is not None:
|
|
queue.fail(session, again["id"], "released by test", retry_in_s=0)
|
|
|
|
|
|
def test_a_second_worker_skips_a_locked_row_instead_of_blocking(session):
|
|
"""The whole point of FOR UPDATE SKIP LOCKED.
|
|
|
|
The invariant is that the second worker is not blocked by the first and
|
|
never gets the same row -- not that the queue is otherwise empty, which
|
|
is not something a shared table can promise.
|
|
"""
|
|
from poorbird.db import SessionLocal
|
|
from poorbird.worker import queue
|
|
|
|
queue.enqueue(session, "test_only_one", priority=TEST_PRIORITY)
|
|
session.commit()
|
|
|
|
first = claim_ours(queue, session, "test_only_one")
|
|
|
|
with SessionLocal() as other:
|
|
second = queue.claim(other)
|
|
assert second is None or second["id"] != first["id"]
|
|
if second is not None:
|
|
queue.fail(other, second["id"], "released by test", retry_in_s=0)
|
|
|
|
queue.complete(session, first["id"])
|
|
|
|
|
|
def test_failure_retries_until_max_attempts_then_parks(session):
|
|
from poorbird.models import Job
|
|
from poorbird.worker import queue
|
|
|
|
job = queue.enqueue(session, "test_doomed", max_attempts=2, priority=TEST_PRIORITY)
|
|
session.commit()
|
|
job_id = job.id
|
|
|
|
for _ in range(2):
|
|
claimed = claim_ours(queue, session, "test_doomed")
|
|
queue.fail(session, claimed["id"], "boom", retry_in_s=0)
|
|
|
|
session.expire_all()
|
|
parked = session.get(Job, job_id)
|
|
assert parked.status == "failed"
|
|
assert parked.attempts == 2
|
|
assert "boom" in parked.last_error
|
|
|
|
|
|
def test_stale_running_jobs_are_reclaimed(session):
|
|
from sqlalchemy import text
|
|
|
|
from poorbird.models import Job
|
|
from poorbird.worker import queue
|
|
|
|
job = queue.enqueue(session, "test_orphan", priority=TEST_PRIORITY)
|
|
session.commit()
|
|
claim_ours(queue, session, "test_orphan")
|
|
|
|
# Pretend the worker died an hour ago.
|
|
session.execute(
|
|
text("UPDATE jobs SET locked_at = now() - interval '1 hour' WHERE id = :id"),
|
|
{"id": job.id},
|
|
)
|
|
session.commit()
|
|
|
|
assert queue.requeue_stale(session, older_than_s=60) >= 1
|
|
session.expire_all()
|
|
assert session.get(Job, job.id).status == "queued"
|