lab5: очередь RabbitMQ, outbox, worker, варианты, fault-injection

This commit is contained in:
2026-11-09 17:34:10 +03:00
parent 276dbdad8a
commit 5f4b10ac2b
96 changed files with 10573 additions and 1 deletions
+95
View File
@@ -0,0 +1,95 @@
package domain
import (
"encoding/json"
"time"
)
const (
AttachmentUploading = "uploading" // внутреннее намерение до 202, клиенту не видно
AttachmentQueued = "queued"
AttachmentProcessing = "processing"
AttachmentReady = "ready"
AttachmentFailed = "failed"
AttachmentMissing = "missing" // карантин reconciler
)
// Variant — результат обработки одной версии pipeline.
type Variant struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
Width int `json:"width"`
Height int `json:"height"`
SHA256 string `json:"sha256"`
ObjectKey string `json:"-"`
PipelineVersion int `json:"-"`
}
// Attachment — метаданные вложения. Ключи S3 никогда не уходят клиенту.
type Attachment struct {
ID string
ChatID string
OwnerID string
Filename string
ObjectKey string // текущее место оригинала: staging до ready, конечный prefix после
StagingKey string // staging-объект, пока он не удалён после публикации
Status string
ContentType string
SizeBytes int64
SHA256 string
ErrorCode string
PipelineVersion int // версия, чей результат опубликован (0 — ещё нет)
Variants []Variant
CreatedAt time.Time
UpdatedAt time.Time
}
// MarshalJSON — представление по контракту: queued/processing/ready/failed,
// варианты текущей опубликованной версии, error_code только у failed.
func (a Attachment) MarshalJSON() ([]byte, error) {
variants := a.Variants
if variants == nil {
variants = []Variant{}
}
var errCode *string
if a.Status == AttachmentFailed && a.ErrorCode != "" {
ec := a.ErrorCode
errCode = &ec
}
return json.Marshal(struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
OwnerID string `json:"owner_id"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
SHA256 string `json:"sha256"`
Status string `json:"status"`
Variants []Variant `json:"variants"`
ErrorCode *string `json:"error_code"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}{
ID: a.ID, ChatID: a.ChatID, OwnerID: a.OwnerID, Filename: a.Filename,
ContentType: a.ContentType, SizeBytes: a.SizeBytes, SHA256: a.SHA256,
Status: a.Status, Variants: variants, ErrorCode: errCode,
CreatedAt: a.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
UpdatedAt: a.UpdatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
})
}
// Job — сообщение broker по contracts/jobs.schema.json. Только ID и служебные
// поля: ни байтов, ни URL, ни credentials.
type Job struct {
SchemaVersion int `json:"schema_version"`
JobID string `json:"job_id"`
Type string `json:"type"`
AttachmentID string `json:"attachment_id"`
PipelineVersion int `json:"pipeline_version"`
Attempt int `json:"attempt"`
CreatedAt string `json:"created_at"`
Traceparent string `json:"traceparent,omitempty"`
}
const JobType = "attachment.process.v1"
+30
View File
@@ -0,0 +1,30 @@
package domain
import (
"encoding/json"
"time"
)
type Chat struct {
ID string `json:"id"`
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
CreatedAt time.Time `json:"created_at"`
}
func (c Chat) MarshalJSON() ([]byte, error) {
type Alias Chat
members := c.MemberIDs
if members == nil {
members = []string{}
}
return json.Marshal(&struct {
Alias
MemberIDs []string `json:"member_ids"`
CreatedAt string `json:"created_at"`
}{
Alias: Alias(c),
MemberIDs: members,
CreatedAt: c.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
})
}
+30
View File
@@ -0,0 +1,30 @@
package domain
import (
"encoding/json"
"time"
)
type Message struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt time.Time `json:"created_at"`
// AttachmentIDs — в порядке отправки; всегда массив (пустой, если вложений нет).
AttachmentIDs []string `json:"attachment_ids"`
}
func (m Message) MarshalJSON() ([]byte, error) {
type Alias Message
if m.AttachmentIDs == nil {
m.AttachmentIDs = []string{}
}
return json.Marshal(&struct {
Alias
CreatedAt string `json:"created_at"`
}{
Alias: Alias(m),
CreatedAt: m.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
})
}
+10
View File
@@ -0,0 +1,10 @@
package domain
import "time"
type Session struct {
Token string `json:"token"`
UserID string `json:"-"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"-"`
}
+25
View File
@@ -0,0 +1,25 @@
package domain
import (
"encoding/json"
"time"
)
type User struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt time.Time `json:"created_at"`
}
// MarshalJSON форматирует CreatedAt в RFC3339 без микросекунд.
func (u User) MarshalJSON() ([]byte, error) {
type Alias User
return json.Marshal(&struct {
Alias
CreatedAt string `json:"created_at"`
}{
Alias: Alias(u),
CreatedAt: u.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
})
}