This commit is contained in:
2026-11-22 17:20:34 +03:00
parent c10bf08feb
commit 7e1780be67
14 changed files with 938 additions and 60 deletions
+16 -7
View File
@@ -11,6 +11,7 @@ from pydantic import BaseModel, ValidationError
from starlette.responses import Response
from .auth import BASIC_CHALLENGE, BEARER_CHALLENGE, parse_basic, parse_bearer
from .broker import BrokerDown
from .drain import draining
from .errors import (
conflict,
@@ -160,10 +161,14 @@ async def liveness() -> JSONResponse:
@router.get('/health/ready')
async def readiness(store: Store = Depends(get_store)) -> JSONResponse:
async def readiness(request: Request, store: Store = Depends(get_store)) -> JSONResponse:
if draining.is_set():
raise unavailable('Реплика завершает работу')
await store.ready()
try:
await request.app.state.broker.ready()
except BrokerDown:
raise unavailable('Очередь недоступна') from None
return JSONResponse({'status': 'ok'})
@@ -276,7 +281,7 @@ async def upload_attachment(
spool = None
try:
spool, size, digest, actual = await _spool_upload(request, declared)
attachment = await store.create_attachment(
queued = await store.create_attachment(
chat.id,
actor.id,
filename,
@@ -288,7 +293,13 @@ async def upload_attachment(
finally:
if spool is not None:
spool.close()
return JSONResponse(attachment.to_dict(), status_code=202)
# The row is committed. 202 follows only a broker confirm, so a stopped API keeps the job.
try:
await request.app.state.broker.publish(queued.body)
except BrokerDown:
await store.abandon_upload(queued.attachment.id)
raise unavailable('Очередь недоступна') from None
return JSONResponse(queued.attachment.to_dict(), status_code=202)
def _variant(request: Request) -> str:
@@ -324,11 +335,9 @@ async def download_attachment(
raise not_found('Вложение не найдено')
if attachment.status != 'ready':
raise conflict('Вложение ещё не готово')
if variant != 'original':
raise not_found('Вариант не найден')
found = await store.read_original(attachment_id, actor.id)
found = await store.read_content(attachment_id, actor.id, variant)
if found is None:
raise not_found('Вложение не найдено')
raise not_found('Вариант не найден' if variant != 'original' else 'Вложение не найдено')
content_type, body = found
return Response(content=body, media_type=content_type)