feat: add job outbox, relay, leases and image variants to the pipeline
Move publishing out of the upload path: a relay process publishes queued jobs with confirms. Add lease and attempt fencing, backoff with jitter, a per-job render process with CPU and memory limits, crop and optimized variants, the failed registry with audited replay, and staging cleanup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,6 @@ Thumbnail, скачивание варианта, готовность и `faile
|
||||
(`test_failures.py`).
|
||||
"""
|
||||
|
||||
import io
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
@@ -20,6 +19,7 @@ from contract import (
|
||||
assert_ok,
|
||||
assert_replicas,
|
||||
assert_variant_content,
|
||||
assert_variant_image,
|
||||
)
|
||||
|
||||
# сколько раз читать метаданные через LB, чтобы увидеть другую реплику
|
||||
@@ -30,6 +30,12 @@ RED = (255, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
WHITE = (255, 255, 255)
|
||||
BLACK = (0, 0, 0)
|
||||
VARIANT_TYPES = {
|
||||
"thumbnail": "image/jpeg",
|
||||
"crop": "image/jpeg",
|
||||
"optimized": "image/webp",
|
||||
}
|
||||
VARIANT_NAMES = tuple(VARIANT_TYPES)
|
||||
|
||||
|
||||
def thumbnail_of(
|
||||
@@ -37,19 +43,32 @@ def thumbnail_of(
|
||||
actor: Actor,
|
||||
attachment: dict[str, Any],
|
||||
) -> Image.Image:
|
||||
"""Вариант из метаданных готового вложения: скачан, сверен и открыт
|
||||
как настоящий JPEG; настоящие размеры равны полям варианта."""
|
||||
assert [item["name"] for item in attachment["variants"]] == ["thumbnail"]
|
||||
variant = attachment["variants"][0]
|
||||
"""Вариант `thumbnail` готового вложения: скачан, сверен и открыт как
|
||||
настоящий JPEG; настоящие размеры равны полям варианта."""
|
||||
return variant_of(api, actor, attachment, "thumbnail")
|
||||
|
||||
|
||||
def variant_of(
|
||||
api: Api,
|
||||
actor: Actor,
|
||||
attachment: dict[str, Any],
|
||||
name: str,
|
||||
) -> Image.Image:
|
||||
"""Вариант из метаданных: у вложения по одному варианту каждого имени, байты
|
||||
скачаны и сверены с SHA-256 и размером, файл — настоящее изображение своего
|
||||
типа и размера."""
|
||||
assert sorted(item["name"] for item in attachment["variants"]) == sorted(
|
||||
VARIANT_NAMES,
|
||||
)
|
||||
variant = next(
|
||||
item for item in attachment["variants"] if item["name"] == name
|
||||
)
|
||||
assert variant["content_type"] == VARIANT_TYPES[name]
|
||||
data = assert_variant_content(
|
||||
api.attachment_content(actor, attachment["id"], variant="thumbnail"),
|
||||
api.attachment_content(actor, attachment["id"], variant=name),
|
||||
variant,
|
||||
)
|
||||
image = Image.open(io.BytesIO(data))
|
||||
assert image.format == "JPEG"
|
||||
assert image.size == (variant["width"], variant["height"])
|
||||
assert variant["content_type"] == "image/jpeg"
|
||||
return image
|
||||
return assert_variant_image(data, variant)
|
||||
|
||||
|
||||
def assert_color(
|
||||
@@ -85,7 +104,8 @@ def test_attachment_becomes_ready(
|
||||
ready = api.wait_attachment(alice, accepted["id"])
|
||||
print(f"ready in {time.monotonic() - started:.2f} s")
|
||||
assert ready["error_code"] is None
|
||||
assert [item["name"] for item in ready["variants"]] == ["thumbnail"]
|
||||
for name in VARIANT_NAMES:
|
||||
variant_of(api, alice, ready, name)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Fixture 320 × 200")
|
||||
@@ -218,7 +238,148 @@ def test_thumbnail_format_variants(
|
||||
content_type=content_type,
|
||||
filename=f"{name}.img",
|
||||
)
|
||||
thumbnail_of(api, alice, attachment)
|
||||
for variant in VARIANT_NAMES:
|
||||
variant_of(api, alice, attachment, variant)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Crop большого изображения")
|
||||
def test_crop_center(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(alice, chat["id"], images.png_stripes())
|
||||
image = variant_of(api, alice, attachment, "crop")
|
||||
assert image.size == (128, 128)
|
||||
for position in ((64, 64), (3, 3), (124, 124), (3, 124), (124, 3)):
|
||||
assert_color(image, position, BLUE)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Crop маленького изображения")
|
||||
def test_crop_small(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(alice, chat["id"], images.png_solid(50, 20))
|
||||
assert variant_of(api, alice, attachment, "crop").size == (128, 128)
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"attachment-processing",
|
||||
"Crop с ориентацией и прозрачностью",
|
||||
)
|
||||
def test_crop_orientation_alpha(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
rotated = api.upload_ready(
|
||||
alice,
|
||||
chat["id"],
|
||||
images.jpeg_halves(orientation=6),
|
||||
content_type="image/jpeg",
|
||||
)
|
||||
image = variant_of(api, alice, rotated, "crop")
|
||||
assert_color(image, (64, 16), RED)
|
||||
assert_color(image, (64, 112), BLUE)
|
||||
transparent = api.upload_ready(
|
||||
alice,
|
||||
chat["id"],
|
||||
images.png_half_transparent(),
|
||||
)
|
||||
image = variant_of(api, alice, transparent, "crop")
|
||||
assert_color(image, (16, 64), BLACK)
|
||||
assert_color(image, (112, 64), WHITE)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Optimized большого изображения")
|
||||
def test_optimized_fit(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
for size, expected in (
|
||||
((2000, 1000), (1280, 640)),
|
||||
((5000, 3001), (1280, 768)),
|
||||
((5000, 10), (1280, 2)),
|
||||
):
|
||||
attachment = api.upload_ready(
|
||||
alice,
|
||||
chat["id"],
|
||||
images.png_solid(*size),
|
||||
)
|
||||
image = variant_of(api, alice, attachment, "optimized")
|
||||
assert image.size == expected, size
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Optimized маленького изображения")
|
||||
def test_optimized_small(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(alice, chat["id"], images.png_solid(100, 50))
|
||||
assert variant_of(api, alice, attachment, "optimized").size == (100, 50)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Optimized без метаданных")
|
||||
def test_optimized_no_metadata(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(
|
||||
alice,
|
||||
chat["id"],
|
||||
images.jpeg_with_metadata(orientation=6),
|
||||
content_type="image/jpeg",
|
||||
)
|
||||
image = variant_of(api, alice, attachment, "optimized")
|
||||
assert image.size == (200, 400)
|
||||
assert_color(image, (100, 50), RED)
|
||||
assert_color(image, (100, 350), BLUE)
|
||||
data = api.attachment_content(
|
||||
alice,
|
||||
attachment["id"],
|
||||
variant="optimized",
|
||||
).content
|
||||
for chunk in (b"EXIF", b"XMP ", b"ICCP", images.JPEG_MAKER.encode()):
|
||||
assert chunk not in data, chunk
|
||||
|
||||
|
||||
@pytest.mark.spec("attachment-processing", "Optimized сохраняет прозрачность")
|
||||
def test_optimized_alpha(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(
|
||||
alice,
|
||||
chat["id"],
|
||||
images.png_half_transparent(),
|
||||
)
|
||||
image = variant_of(api, alice, attachment, "optimized")
|
||||
assert image.convert("RGBA").getpixel((190, 50))[3] == 0
|
||||
assert image.convert("RGBA").getpixel((10, 50))[3] == 255
|
||||
|
||||
|
||||
@pytest.mark.spec("attachments", "Скачивание crop и optimized")
|
||||
def test_download_crop_optimized(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(alice, chat["id"], images.sample_png())
|
||||
for name, content_type, size in (
|
||||
("crop", "image/jpeg", (128, 128)),
|
||||
("optimized", "image/webp", (320, 200)),
|
||||
):
|
||||
resp = api.attachment_content(bob, attachment["id"], variant=name)
|
||||
assert not resp.is_redirect
|
||||
assert resp.headers["Content-Type"] == content_type
|
||||
assert variant_of(api, bob, attachment, name).size == size
|
||||
|
||||
|
||||
@pytest.mark.spec("attachments", "Скачивание thumbnail")
|
||||
@@ -235,24 +396,13 @@ def test_download_thumbnail(
|
||||
variant="thumbnail",
|
||||
)
|
||||
assert not resp.is_redirect
|
||||
assert_variant_content(resp, attachment["variants"][0])
|
||||
thumbnail = next(
|
||||
item for item in attachment["variants"] if item["name"] == "thumbnail"
|
||||
)
|
||||
assert_variant_content(resp, thumbnail)
|
||||
assert resp.headers["Content-Type"] == "image/jpeg"
|
||||
|
||||
|
||||
@pytest.mark.spec("attachments", "Отсутствующий вариант")
|
||||
def test_missing_variant(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload_ready(alice, chat["id"], images.sample_png())
|
||||
for variant in ("crop", "optimized"):
|
||||
assert_error(
|
||||
api.attachment_content(alice, attachment["id"], variant=variant),
|
||||
404,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.spec("attachments", "Вариант для постороннего")
|
||||
def test_variant_outsider(
|
||||
api: Api,
|
||||
|
||||
Reference in New Issue
Block a user