Реализовать асинхронную обработку вложений

This commit is contained in:
2026-11-17 18:21:09 +03:00
parent 4d54ba3848
commit e8becaf761
51 changed files with 2234 additions and 58 deletions
+6 -1
View File
@@ -1,13 +1,18 @@
from .attachment import AttachmentRow, MessageAttachmentRow
from .attachment import AttachmentRow, AttachmentVariantRow, MessageAttachmentRow
from .chat import ChatMemberRow, ChatRow
from .message import MessageRow
from .user import UserRow
from .job import AttachmentJobRow, JobEventRow, OutboxEventRow
__all__ = [
'AttachmentRow',
'AttachmentVariantRow',
'AttachmentJobRow',
'ChatMemberRow',
'ChatRow',
'MessageAttachmentRow',
'MessageRow',
'UserRow',
'JobEventRow',
'OutboxEventRow',
]
+23 -2
View File
@@ -14,14 +14,17 @@ class AttachmentRow(Base):
__tablename__ = 'attachments'
__table_args__ = (
Index('attachments_chat_created_idx', 'chat_id', 'created_at', 'id'),
UniqueConstraint('object_key', name='attachments_object_key_key'),
UniqueConstraint('original_key', name='attachments_object_key_key'),
UniqueConstraint('staging_key', name='attachments_staging_key_key'),
)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
chat_id: Mapped[UUID] = mapped_column(ForeignKey('chats.id', ondelete='CASCADE'), nullable=False)
owner_id: Mapped[UUID] = mapped_column(ForeignKey('users.id'), nullable=False)
filename: Mapped[str] = mapped_column(String(255), nullable=False)
object_key: Mapped[str] = mapped_column(String(255), nullable=False)
original_key: Mapped[str | None] = mapped_column(String(255), nullable=True)
staging_key: Mapped[str | None] = mapped_column(String(255), nullable=True)
pipeline_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
content_type: Mapped[str] = mapped_column(String(32), nullable=False)
size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
@@ -33,6 +36,24 @@ class AttachmentRow(Base):
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class AttachmentVariantRow(Base):
__tablename__ = 'attachment_variants'
attachment_id: Mapped[UUID] = mapped_column(
ForeignKey('attachments.id', ondelete='CASCADE'), primary_key=True
)
pipeline_version: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(16), primary_key=True)
object_key: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
content_type: Mapped[str] = mapped_column(String(32), nullable=False)
size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
width: Mapped[int] = mapped_column(Integer, nullable=False)
height: Mapped[int] = mapped_column(Integer, nullable=False)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class MessageAttachmentRow(Base):
__tablename__ = 'message_attachments'
__table_args__ = (
+70
View File
@@ -0,0 +1,70 @@
from __future__ import annotations
from datetime import datetime
from uuid import UUID
from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, Integer, JSON, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import Uuid
from ..db.postgres import Base
class AttachmentJobRow(Base):
__tablename__ = 'attachment_jobs'
__table_args__ = (
UniqueConstraint(
'attachment_id', 'pipeline_version', name='attachment_job_attachment_version_key'
),
Index('attachment_jobs_lease_idx', 'state', 'lease_until'),
)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
attachment_id: Mapped[UUID] = mapped_column(
ForeignKey('attachments.id', ondelete='CASCADE'), nullable=False
)
pipeline_version: Mapped[int] = mapped_column(Integer, nullable=False)
state: Mapped[str] = mapped_column(String(16), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False)
max_attempts: Mapped[int] = mapped_column(Integer, nullable=False)
next_attempt_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
lease_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
replay_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class OutboxEventRow(Base):
__tablename__ = 'outbox_events'
__table_args__ = (Index('outbox_events_pending_idx', 'published_at', 'available_at'),)
id: Mapped[UUID] = mapped_column(Uuid, primary_key=True)
event_type: Mapped[str] = mapped_column(String(64), nullable=False)
job_id: Mapped[UUID] = mapped_column(
ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False
)
payload: Mapped[dict] = mapped_column(JSON, nullable=False)
available_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
claim_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
claim_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
publish_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class JobEventRow(Base):
__tablename__ = 'job_events'
__table_args__ = (Index('job_events_job_created_idx', 'job_id', 'created_at'),)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
job_id: Mapped[UUID] = mapped_column(
ForeignKey('attachment_jobs.id', ondelete='CASCADE'), nullable=False
)
kind: Mapped[str] = mapped_column(String(32), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False)
error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)