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
+100 -26
View File
@@ -6,53 +6,120 @@ import (
"github.com/google/uuid"
)
// AttachmentStatus is the lifecycle of an attachment. Only ready attachments
// are exposed to members; pending rows exist while the object is being written,
// failed rows are reserved for later labs.
// AttachmentStatus is the public lifecycle of an attachment. Pending is an
// internal presign state and is never exposed by the API.
type AttachmentStatus string
const (
AttachmentPending AttachmentStatus = "pending"
AttachmentReady AttachmentStatus = "ready"
AttachmentFailed AttachmentStatus = "failed"
AttachmentPending AttachmentStatus = "pending"
AttachmentQueued AttachmentStatus = "queued"
AttachmentProcessing AttachmentStatus = "processing"
AttachmentReady AttachmentStatus = "ready"
AttachmentFailed AttachmentStatus = "failed"
)
func (s AttachmentStatus) Valid() bool {
switch s {
case AttachmentPending, AttachmentReady, AttachmentFailed:
case AttachmentPending, AttachmentQueued, AttachmentProcessing, AttachmentReady, AttachmentFailed:
return true
default:
return false
}
}
// Attachment describes the original object. ObjectKey is internal: it never
// leaves the service and must not be mapped into an API response.
type Attachment struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
Filename string
// Public reports whether the status is part of the API enum.
func (s AttachmentStatus) Public() bool {
switch s {
case AttachmentQueued, AttachmentProcessing, AttachmentReady, AttachmentFailed:
return true
default:
return false
}
}
// VariantName is the contract variant identifier.
type VariantName string
const (
VariantOriginal VariantName = "original"
VariantThumbnail VariantName = "thumbnail"
VariantCrop VariantName = "crop"
VariantOptimized VariantName = "optimized"
)
func (n VariantName) Valid() bool {
switch n {
case VariantThumbnail, VariantCrop, VariantOptimized:
return true
default:
return false
}
}
// ContentType returns the contract media type of the variant.
func (n VariantName) ContentType() string {
if n == VariantOptimized {
return "image/webp"
}
return "image/jpeg"
}
// ObjectName returns the object suffix for the variant.
func (n VariantName) ObjectName() string {
if n == VariantOptimized {
return string(n) + ".webp"
}
return string(n) + ".jpg"
}
// Variant is one derived image. ObjectKey is internal and never leaves the
// service.
type Variant struct {
Name VariantName
ContentType string
SizeBytes int64
Width int
Height int
SHA256 string
Status AttachmentStatus
ErrorCode *string
ObjectKey string
CreatedAt time.Time
UpdatedAt time.Time
}
// Attachment describes the original object and the current pipeline result.
type Attachment struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
Filename string
ContentType string
SizeBytes int64
SHA256 string
Status AttachmentStatus
ErrorCode *string
ObjectKey string
StagingKey string
PipelineVersion int
Variants []Variant
CreatedAt time.Time
UpdatedAt time.Time
ReadyAt *time.Time
FailedAt *time.Time
}
// AttachmentDraft is a fully validated attachment awaiting persistence.
type AttachmentDraft struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
ObjectKey string
Filename string
ContentType string
SizeBytes int64
SHA256 string
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
ObjectKey string
StagingKey string
Filename string
ContentType string
SizeBytes int64
SHA256 string
Status AttachmentStatus
PipelineVersion int
}
// AttachmentState is the minimal projection used while validating message
@@ -70,6 +137,13 @@ type AttachmentKey struct {
CreatedAt time.Time
}
// StaleStaging is one staging object whose owner row is already terminal.
type StaleStaging struct {
AttachmentID uuid.UUID
StagingKey string
Status AttachmentStatus
}
// PresignRequest is the validated client intent for a direct upload.
type PresignRequest struct {
ChatID uuid.UUID