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:
@@ -335,6 +335,117 @@ def png_rgba_large(width: int = 8000, height: int = 5000) -> bytes:
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def png_stripes(
|
||||
stripes: tuple[tuple[int, tuple[int, int, int]], ...] = (
|
||||
(100, (255, 0, 0)),
|
||||
(200, (0, 0, 255)),
|
||||
(100, (0, 255, 0)),
|
||||
),
|
||||
height: int = 200,
|
||||
) -> bytes:
|
||||
"""PNG RGB из вертикальных полос `(ширина, цвет)`: по умолчанию 400 × 200
|
||||
спека «Crop большого изображения»."""
|
||||
from PIL import Image # noqa: PLC0415
|
||||
|
||||
width = sum(stripe for stripe, _ in stripes)
|
||||
image = Image.new("RGB", (width, height))
|
||||
left = 0
|
||||
for stripe, color in stripes:
|
||||
image.paste(color, (left, 0, left + stripe, height))
|
||||
left += stripe
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, "PNG")
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def png_solid(width: int, height: int) -> bytes:
|
||||
"""PNG RGB одного цвета любых размеров в маленьком файле."""
|
||||
from PIL import Image # noqa: PLC0415
|
||||
|
||||
buffer = io.BytesIO()
|
||||
Image.new("RGB", (width, height), (10, 120, 200)).save(
|
||||
buffer,
|
||||
"PNG",
|
||||
compress_level=1,
|
||||
)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
# строка производителя в EXIF `jpeg_with_metadata`: её не должно быть в
|
||||
# вариантах (спек «Optimized без метаданных»)
|
||||
JPEG_MAKER = "student-tests-maker"
|
||||
|
||||
|
||||
def jpeg_with_metadata(orientation: int = 6) -> bytes:
|
||||
"""JPEG 400 × 200, левая половина красная, правая синяя, с EXIF orientation,
|
||||
строкой производителя и профилем ICC."""
|
||||
from PIL import Image, ImageCms # noqa: PLC0415
|
||||
|
||||
image = Image.new("RGB", (400, 200), (255, 0, 0))
|
||||
image.paste((0, 0, 255), (200, 0, 400, 200))
|
||||
exif = Image.Exif()
|
||||
exif[0x0112] = orientation
|
||||
exif[0x010F] = JPEG_MAKER
|
||||
profile = ImageCms.ImageCmsProfile(ImageCms.createProfile("sRGB"))
|
||||
buffer = io.BytesIO()
|
||||
image.save(
|
||||
buffer,
|
||||
"JPEG",
|
||||
quality=95,
|
||||
exif=exif,
|
||||
icc_profile=profile.tobytes(),
|
||||
)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def _scan_end(data: bytes, start: int) -> int:
|
||||
"""Конец сжатых данных скана: первый маркер, кроме `FF00` и RST."""
|
||||
pos = start
|
||||
while True:
|
||||
pos = data.index(b"\xff", pos)
|
||||
marker = data[pos + 1]
|
||||
if marker != 0 and not 0xD0 <= marker <= 0xD7:
|
||||
return pos
|
||||
pos += 2
|
||||
|
||||
|
||||
def jpeg_scan_bomb(size: int = MAX_BYTES - 64) -> bytes:
|
||||
"""«Дорогой» JPEG в пределах контракта: прогрессивный 8000 × 5000, самый
|
||||
маленький скан повторён до `size` байт (сотни тысяч сканов). Структурная
|
||||
проверка его принимает, а декодирование занимает больше минуты CPU
|
||||
(design change `pipeline-reliability`, таблица проб)."""
|
||||
from PIL import Image # noqa: PLC0415
|
||||
|
||||
buffer = io.BytesIO()
|
||||
Image.new("RGB", (8000, 5000), (128, 128, 128)).save(
|
||||
buffer,
|
||||
"JPEG",
|
||||
progressive=True,
|
||||
quality=50,
|
||||
)
|
||||
data = buffer.getvalue()
|
||||
scans = []
|
||||
pos = 0
|
||||
while (pos := data.find(b"\xff\xda", pos)) != -1:
|
||||
(length,) = struct.unpack_from(">H", data, pos + 2)
|
||||
end = _scan_end(data, pos + 2 + length)
|
||||
scans.append(data[pos:end])
|
||||
pos = end
|
||||
# только сканы AC (Ss > 0): их можно повторять, не ломая DC
|
||||
repeatable = [
|
||||
scan
|
||||
for scan in scans
|
||||
if scan[2 + struct.unpack_from(">H", scan, 2)[0] - 3] > 0
|
||||
]
|
||||
smallest = min(repeatable, key=len)
|
||||
body = data[: -len(JPEG_EOI)]
|
||||
count = (size - len(data)) // len(smallest)
|
||||
assert count > 100_000, f"scan of {len(smallest)} bytes is too big"
|
||||
bomb = body + smallest * count + JPEG_EOI
|
||||
assert len(bomb) <= MAX_BYTES
|
||||
return bomb
|
||||
|
||||
|
||||
def png_variants() -> dict[str, bytes]:
|
||||
"""Разновидности PNG спека: палитра, альфа, серый с альфой, 16 и 1
|
||||
бит, Adam7."""
|
||||
|
||||
Reference in New Issue
Block a user