forked from mtusi2026/mtusi2026lab5
level 4
This commit is contained in:
+9
-38
@@ -13,7 +13,7 @@ import asyncpg
|
||||
|
||||
from .config import job_max_attempts
|
||||
from .errors import conflict, invalid_request, not_found, unavailable
|
||||
from .jobs import PIPELINE_VERSION, dump_job, original_key, staging_key
|
||||
from .jobs import PIPELINE_VERSION, original_key, staging_key
|
||||
from .migrate import expected_versions
|
||||
from .objects import MissingObject, ObjectStore, StorageError
|
||||
from .pagination import Key
|
||||
@@ -129,7 +129,7 @@ class Attachment:
|
||||
@dataclass(frozen=True)
|
||||
class QueuedUpload:
|
||||
attachment: Attachment
|
||||
body: bytes
|
||||
job_id: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -200,6 +200,10 @@ class Store:
|
||||
self._last_moment: Optional[datetime] = None
|
||||
self._migrations = expected_versions()
|
||||
|
||||
@property
|
||||
def pool(self) -> asyncpg.Pool:
|
||||
return self._pool
|
||||
|
||||
def _now(self) -> datetime:
|
||||
"""Strictly increasing within one process; across replicas order relies on the shared host clock."""
|
||||
with self._lock:
|
||||
@@ -426,9 +430,8 @@ class Store:
|
||||
owner_uuid = _as_uuid(owner_id, 'Некорректный user_id')
|
||||
attachment_id = uuid.uuid4()
|
||||
job_id = uuid.uuid4()
|
||||
# filename is never a storage key. Bytes go to staging/ first; the worker copies them
|
||||
# unchanged to originals/ and writes the thumbnail. 202 is sent only after the broker
|
||||
# confirms the job, which happens in the route after this commit.
|
||||
# filename is never a storage key. Bytes go to staging/ first. The job row is the
|
||||
# outbox: 202 follows this commit, and a missing broker confirm is retried from the row.
|
||||
staged = staging_key(str(attachment_id))
|
||||
final = original_key(str(attachment_id))
|
||||
created = self._now()
|
||||
@@ -489,39 +492,7 @@ class Store:
|
||||
stamp,
|
||||
stamp,
|
||||
)
|
||||
return QueuedUpload(
|
||||
attachment,
|
||||
dump_job(str(job_id), str(attachment_id), 1, stamp),
|
||||
)
|
||||
|
||||
async def abandon_upload(self, attachment_id: str) -> None:
|
||||
"""Drop a queued upload whose job never reached the broker."""
|
||||
attachment_uuid = _as_uuid(attachment_id, 'Некорректный attachment_id')
|
||||
staged: Optional[str] = None
|
||||
async with self._pool.acquire() as conn:
|
||||
async with conn.transaction():
|
||||
row = await conn.fetchrow(
|
||||
'''
|
||||
SELECT staging_key
|
||||
FROM attachments
|
||||
WHERE id = $1 AND status = 'queued'
|
||||
FOR UPDATE
|
||||
''',
|
||||
attachment_uuid,
|
||||
)
|
||||
if row is None:
|
||||
return
|
||||
staged = row['staging_key']
|
||||
await conn.execute(
|
||||
'DELETE FROM attachment_jobs WHERE attachment_id = $1',
|
||||
attachment_uuid,
|
||||
)
|
||||
await conn.execute(
|
||||
"DELETE FROM attachments WHERE id = $1 AND status = 'queued'",
|
||||
attachment_uuid,
|
||||
)
|
||||
if staged:
|
||||
await self._discard_object(staged)
|
||||
return QueuedUpload(attachment, str(job_id))
|
||||
|
||||
async def attachment_index(self) -> List[AttachmentRef]:
|
||||
"""Every attachment row. `in_use` means a message still points at it.
|
||||
|
||||
Reference in New Issue
Block a user