lab5: очереди и асинхронная обработка изображений

Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 14:34:24 +03:00
parent 6c5ba026f1
commit 01b885e846
99 changed files with 10164 additions and 1452 deletions
+107 -26
View File
@@ -6,53 +6,127 @@ 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
}
// AdmissionLimits bounds accepted-but-unfinished work. Zero means unlimited.
type AdmissionLimits struct {
PerUser int
Total int
Bytes int64
}
// 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 +144,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