This commit is contained in:
2026-11-22 17:59:11 +03:00
parent 7e1780be67
commit 813fc03da5
18 changed files with 1859 additions and 265 deletions
+13 -4
View File
@@ -1,5 +1,7 @@
import hashlib
import json
import logging
import os
import re
import tempfile
import uuid
@@ -12,6 +14,8 @@ from starlette.responses import Response
from .auth import BASIC_CHALLENGE, BEARER_CHALLENGE, parse_basic, parse_bearer
from .broker import BrokerDown
from .fault import consume_fault
from .outbox import publish_job
from .drain import draining
from .errors import (
conflict,
@@ -31,6 +35,8 @@ from .store import Chat, Store, User
# The original is never persisted on the API volume; the spool dies with the request.
_SPOOL_RAM = 64 * 1024
logger = logging.getLogger('messenger')
router = APIRouter()
Body = TypeVar('Body', bound=BaseModel)
@@ -293,12 +299,15 @@ async def upload_attachment(
finally:
if spool is not None:
spool.close()
# The row is committed. 202 follows only a broker confirm, so a stopped API keeps the job.
# The row is committed. A crash before the confirm leaves it for the outbox relay.
if await consume_fault(store.pool, 'commit-before-publish'):
logger.warning('fault commit-before-publish: exiting before publish of %s', queued.attachment.id)
logging.shutdown()
os._exit(75)
try:
await request.app.state.broker.publish(queued.body)
await publish_job(store.pool, request.app.state.broker, queued.job_id)
except BrokerDown:
await store.abandon_upload(queued.attachment.id)
raise unavailable('Очередь недоступна') from None
logger.warning('upload %s is committed and waits in the outbox', queued.attachment.id)
return JSONResponse(queued.attachment.to_dict(), status_code=202)