forked from mtusi2026/mtusi2026lab6
Forward the lab 5 foundation while preserving lab 6 telemetry
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files. Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
This commit is contained in:
@@ -34,8 +34,8 @@ BOMBS = (Image.DecompressionBombError, Image.DecompressionBombWarning)
|
||||
JPEG_SCALE = 8
|
||||
WIDE_MODES = frozenset({"I", "I;16", "I;16B", "I;16L", "I;16N"})
|
||||
ALPHA_MODES = frozenset({"LA", "La", "PA", "RGBa"})
|
||||
JPEG = {"quality": 85, "optimize": True}
|
||||
WEBP = {"quality": 80, "method": 4}
|
||||
JPEG = {1: {"quality": 85, "optimize": True}, 2: {"quality": 90, "optimize": True}}
|
||||
WEBP = {1: {"quality": 80, "method": 4}, 2: {"quality": 85, "method": 4}}
|
||||
ENCODINGS = {"JPEG": (ImageKind.JPEG.value, "jpg"), "WEBP": ("image/webp", "webp")}
|
||||
REDUCING_GAP = 3.0
|
||||
|
||||
@@ -128,15 +128,27 @@ def decoded(data: bytes, max_pixels: int) -> tuple[Image.Image, int, int]:
|
||||
raise UnreadableImage("изображение не декодируется") from error
|
||||
|
||||
|
||||
def render_variants(data: bytes, limits: RenderLimits) -> list[Rendition]:
|
||||
def render_variants(
|
||||
data: bytes, limits: RenderLimits, pipeline_version: int = 1
|
||||
) -> list[Rendition]:
|
||||
if len(data) > limits.max_bytes:
|
||||
raise ImageTooDemanding("оригинал больше допустимого размера")
|
||||
upright, width, height = decoded(data, limits.max_pixels)
|
||||
square = ImageOps.fit(upright, (CROP_SIDE, CROP_SIDE), Image.Resampling.LANCZOS)
|
||||
return [
|
||||
encoded(THUMBNAIL, scaled(upright, fit(width, height, THUMBNAIL_BOX)), "JPEG", **JPEG),
|
||||
encoded(CROP, square, "JPEG", **JPEG),
|
||||
encoded(OPTIMIZED, scaled(upright, fit(width, height, OPTIMIZED_BOX)), "WEBP", **WEBP),
|
||||
encoded(
|
||||
THUMBNAIL,
|
||||
scaled(upright, fit(width, height, THUMBNAIL_BOX)),
|
||||
"JPEG",
|
||||
**JPEG[pipeline_version],
|
||||
),
|
||||
encoded(CROP, square, "JPEG", **JPEG[pipeline_version]),
|
||||
encoded(
|
||||
OPTIMIZED,
|
||||
scaled(upright, fit(width, height, OPTIMIZED_BOX)),
|
||||
"WEBP",
|
||||
**WEBP[pipeline_version],
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -145,7 +157,7 @@ def limit_memory(limit_bytes: int) -> None:
|
||||
resource.setrlimit(resource.RLIMIT_AS, (limit_bytes, limit_bytes))
|
||||
|
||||
|
||||
type Render = Callable[[bytes, RenderLimits], list[Rendition]]
|
||||
type Render = Callable[[bytes, RenderLimits, int], list[Rendition]]
|
||||
|
||||
|
||||
class ProcessVariantRenderer:
|
||||
@@ -173,7 +185,9 @@ class ProcessVariantRenderer:
|
||||
async def render(self, data: bytes, pipeline_version: int) -> list[Rendition]:
|
||||
pool = await self._idle.get()
|
||||
try:
|
||||
future = asyncio.wrap_future(pool.submit(self._render, data, self._limits))
|
||||
future = asyncio.wrap_future(
|
||||
pool.submit(self._render, data, self._limits, pipeline_version)
|
||||
)
|
||||
return await asyncio.wait_for(future, self._timeout)
|
||||
except TimeoutError as error:
|
||||
pool = self._replace(pool)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from collections.abc import AsyncIterator, Sequence
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any
|
||||
|
||||
@@ -8,7 +8,7 @@ from botocore.exceptions import BotoCoreError, ClientError
|
||||
from botocore.handlers import add_expect_header
|
||||
|
||||
from messenger.attachments.application.errors import ObjectMissingError
|
||||
from messenger.attachments.application.ports import ObjectContent
|
||||
from messenger.attachments.application.ports import ObjectContent, StoredObject
|
||||
from messenger.shared.application.errors import Unavailable
|
||||
|
||||
CHUNK_BYTES = 64 * 1024
|
||||
@@ -98,6 +98,28 @@ class S3ObjectStorage:
|
||||
except FAILURES as error:
|
||||
raise Unavailable("S3 недоступно") from error
|
||||
|
||||
async def listing(self, prefix: str, page_size: int) -> AsyncIterator[Sequence[StoredObject]]:
|
||||
pages = self._client.get_paginator("list_objects_v2").paginate(
|
||||
Bucket=self._bucket, Prefix=prefix, PaginationConfig={"PageSize": page_size}
|
||||
)
|
||||
try:
|
||||
async for page in pages:
|
||||
yield [
|
||||
StoredObject(item["Key"], item["Size"], item["LastModified"])
|
||||
for item in page.get("Contents", ())
|
||||
]
|
||||
except FAILURES as error:
|
||||
raise Unavailable("S3 недоступно") from error
|
||||
|
||||
async def stat(self, key: str) -> StoredObject | None:
|
||||
try:
|
||||
head = await self._client.head_object(Bucket=self._bucket, Key=key)
|
||||
except FAILURES as error:
|
||||
if missing(error):
|
||||
return None
|
||||
raise Unavailable("S3 недоступно") from error
|
||||
return StoredObject(key, head["ContentLength"], head["LastModified"])
|
||||
|
||||
async def _chunks(self, body: Any) -> AsyncIterator[bytes]:
|
||||
async with body as stream:
|
||||
async for chunk in stream.iter_chunks(CHUNK_BYTES):
|
||||
|
||||
Reference in New Issue
Block a user