feat(external): zip-parity provenance/tagging + thorough worker logging
Operator-requested: a worker download must be tagged + provenance-associated exactly like an extracted zip, and the path must log well (we won't get it right first try). - _route_files now mirrors download_service._phase3_persist branch-for-branch: imported/superseded → collect member_image_ids+image_id (provenance-linked via the synthesized sidecar, same as extracted-zip members) → caller enqueues tag_and_embed + generate_thumbnail; attached → drop on-disk original, and warn on an UNEXTRACTED archive (#718 symptom); skipped duplicate → unlink; failed → unlink + warn. - Logging at every stage: start (link/host/post/artist/attempt/url), requeue, fetch result (files/bytes) or fetch failure, per-file import decision, dead- letter transitions, and done (files/images/duration). - Parity test: an archive downloaded by the worker is extracted, provenance- linked to the SAME post, and tag_and_embed+generate_thumbnail are queued for exactly the member images. Refs FC #830. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,15 +1,39 @@
|
||||
"""Integration tests for the external-link download worker (tasks/external)."""
|
||||
|
||||
import io
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
from sqlalchemy import func, select
|
||||
|
||||
import backend.app.tasks.external as ext
|
||||
from backend.app.models import Artist, ExternalLink, Post, PostAttachment, Source
|
||||
from backend.app.models import (
|
||||
Artist,
|
||||
ExternalLink,
|
||||
ImageProvenance,
|
||||
Post,
|
||||
PostAttachment,
|
||||
Source,
|
||||
)
|
||||
from backend.app.services.external_fetch import FetchResult
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
|
||||
def _structured_jpeg_bytes():
|
||||
# A non-uniform image so it survives the import filters (a solid color would
|
||||
# phash-collapse / fail thresholds).
|
||||
im = Image.new("RGB", (256, 256))
|
||||
px = im.load()
|
||||
for y in range(256):
|
||||
for x in range(256):
|
||||
px[x, y] = (x, y, (x + y) % 256)
|
||||
buf = io.BytesIO()
|
||||
im.save(buf, "JPEG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
class _FakeLock:
|
||||
def acquire(self, blocking=False):
|
||||
return True
|
||||
@@ -136,3 +160,44 @@ def test_sweep_enqueues_pending_and_retryable(db_sync, monkeypatch):
|
||||
out = ext.sweep_external_links()
|
||||
assert out["enqueued"] == 2
|
||||
assert set(enqueued) == {l1.id, l2.id}
|
||||
|
||||
|
||||
def test_downloaded_archive_gets_provenance_and_tagging(db_sync, tmp_path, monkeypatch):
|
||||
"""An archive downloaded by the worker is extracted, provenance-linked to the
|
||||
post, AND queued for tagging + thumbnails — exactly like an extracted zip on
|
||||
the normal download path."""
|
||||
post, link = _seed(db_sync, host="mega")
|
||||
link_id = link.id
|
||||
|
||||
monkeypatch.setattr(ext, "IMAGES_ROOT", tmp_path)
|
||||
monkeypatch.setattr(ext, "_redis", lambda: _FakeRedis())
|
||||
|
||||
def fake_fetch(host, url, dest_dir, *, timeout, should_stop=lambda: False):
|
||||
dest_dir.mkdir(parents=True, exist_ok=True)
|
||||
zpath = dest_dir / "pack.zip"
|
||||
with zipfile.ZipFile(zpath, "w") as z:
|
||||
z.writestr("art.jpg", _structured_jpeg_bytes())
|
||||
return FetchResult(files=[zpath], bytes=zpath.stat().st_size)
|
||||
|
||||
monkeypatch.setattr(ext, "fetch_external", fake_fetch)
|
||||
|
||||
tagged, thumbed = [], []
|
||||
import backend.app.tasks.ml as ml_mod
|
||||
import backend.app.tasks.thumbnail as thumb_mod
|
||||
monkeypatch.setattr(ml_mod.tag_and_embed, "delay", lambda i: tagged.append(i))
|
||||
monkeypatch.setattr(thumb_mod.generate_thumbnail, "delay", lambda i: thumbed.append(i))
|
||||
|
||||
out = ext.fetch_external_link(link_id)
|
||||
assert out["images"] >= 1
|
||||
|
||||
db_sync.expire_all()
|
||||
assert db_sync.get(ExternalLink, link_id).status == "downloaded"
|
||||
# Extracted member is provenance-linked to the SAME post (as extracted zips).
|
||||
prov = db_sync.execute(
|
||||
select(ImageProvenance).where(ImageProvenance.post_id == post.id)
|
||||
).scalars().all()
|
||||
assert len(prov) >= 1
|
||||
member_ids = {p.image_record_id for p in prov}
|
||||
# Tagging + thumbnails queued for exactly those member images.
|
||||
assert set(tagged) == member_ids
|
||||
assert set(thumbed) == member_ids
|
||||
|
||||
Reference in New Issue
Block a user