lab5: асинхронная очередь, worker и идемпотентный pipeline

API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
Vladislav Kunetskiy
2026-10-30 22:36:52 +03:00
parent 6c5ba026f1
commit 4bdcac3dfa
95 changed files with 8466 additions and 764 deletions
@@ -6,6 +6,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/storage/postgres/internal/sqlcgen"
@@ -48,7 +49,7 @@ func (r *AttachmentRepository) Create(ctx context.Context, draft domain.Attachme
return domain.Attachment{}, fmt.Errorf("create attachment: %w", mapError(err))
}
return mapAttachment(row), nil
return mapCreateAttachment(row), nil
}
func (r *AttachmentRepository) MarkReady(
@@ -67,7 +68,7 @@ func (r *AttachmentRepository) MarkReady(
return domain.Attachment{}, fmt.Errorf("mark attachment ready: %w", mapError(err))
}
return mapAttachment(row), nil
return mapMarkReadyAttachment(row), nil
}
func (r *AttachmentRepository) DeletePending(ctx context.Context, id uuid.UUID) error {
@@ -96,7 +97,7 @@ func (r *AttachmentRepository) GetForMember(
return domain.Attachment{}, fmt.Errorf("get attachment for member: %w", mapError(err))
}
return mapAttachment(row), nil
return mapMemberAttachment(row), nil
}
func (r *AttachmentRepository) GetForOwner(
@@ -114,7 +115,7 @@ func (r *AttachmentRepository) GetForOwner(
return domain.Attachment{}, fmt.Errorf("get attachment for owner: %w", mapError(err))
}
return mapAttachment(row), nil
return mapOwnerAttachment(row), nil
}
func (r *AttachmentRepository) ListStates(
@@ -160,7 +161,12 @@ func (r *AttachmentRepository) ListStalePending(
return nil, fmt.Errorf("list stale attachments: %w", mapError(err))
}
return mapAll(rows, mapAttachment), nil
stale := make([]domain.Attachment, 0, len(rows))
for _, row := range rows {
stale = append(stale, mapStaleAttachment(row))
}
return stale, nil
}
func (r *AttachmentRepository) ListKeys(
@@ -191,23 +197,47 @@ func (r *AttachmentRepository) ListKeys(
return keys, nil
}
func mapAttachment(row sqlcgen.Attachment) domain.Attachment {
attachment := domain.Attachment{
ID: row.ID,
ChatID: row.ChatID,
OwnerID: row.OwnerID,
ObjectKey: row.ObjectKey,
Filename: row.Filename,
ContentType: row.ContentType,
SizeBytes: row.SizeBytes,
SHA256: row.Sha256,
Status: domain.AttachmentStatus(row.Status),
CreatedAt: toUTCTime(row.CreatedAt),
UpdatedAt: toUTCTime(row.UpdatedAt),
}
func mapCreateAttachment(row sqlcgen.CreateAttachmentRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
if row.ErrorCode.Valid {
code := row.ErrorCode.String
func mapMarkReadyAttachment(row sqlcgen.MarkAttachmentReadyRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapMemberAttachment(row sqlcgen.GetAttachmentForMemberRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapOwnerAttachment(row sqlcgen.GetAttachmentForOwnerRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapStaleAttachment(row sqlcgen.ListStalePendingAttachmentsRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func legacyAttachment(
id, chatID, ownerID uuid.UUID,
objectKey, filename, contentType string,
sizeBytes int64,
sha, status string,
errorCode pgtype.Text,
createdAt, updatedAt time.Time,
) domain.Attachment {
attachment := domain.Attachment{
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: objectKey,
Filename: filename, ContentType: contentType, SizeBytes: sizeBytes,
SHA256: sha, Status: domain.AttachmentStatus(status),
PipelineVersion: 1, CreatedAt: createdAt, UpdatedAt: updatedAt,
}
if errorCode.Valid {
code := errorCode.String
attachment.ErrorCode = &code
}