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:
@@ -0,0 +1,180 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// JobType is the fixed contract type from contracts/jobs.schema.json.
|
||||
const JobType = "attachment.process.v1"
|
||||
|
||||
// JobStatus is the durable state of one logical processing job. Superseded is
|
||||
// internal: a newer pipeline version replaced the job before it completed.
|
||||
type JobStatus string
|
||||
|
||||
const (
|
||||
JobQueued JobStatus = "queued"
|
||||
JobProcessing JobStatus = "processing"
|
||||
JobReady JobStatus = "ready"
|
||||
JobFailed JobStatus = "failed"
|
||||
JobSuperseded JobStatus = "superseded"
|
||||
)
|
||||
|
||||
func (s JobStatus) Valid() bool {
|
||||
switch s {
|
||||
case JobQueued, JobProcessing, JobReady, JobFailed, JobSuperseded:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Job is the durable processing intent. SourceKey is the trusted object the
|
||||
// worker reads: staging for a fresh upload, the original object for reprocess.
|
||||
type Job struct {
|
||||
ID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int
|
||||
SourceKey string
|
||||
Attempt int
|
||||
MaxAttempts int
|
||||
Status JobStatus
|
||||
AvailableAt time.Time
|
||||
LeaseOwner string
|
||||
LeaseExpiresAt *time.Time
|
||||
LastErrorCode *string
|
||||
ReplayCount int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
CompletedAt *time.Time
|
||||
}
|
||||
|
||||
// OutboxMessage is one pending broker publication. Payload is the exact
|
||||
// jobs.schema.json document; no binary data or credentials are stored.
|
||||
type OutboxMessage struct {
|
||||
ID int64
|
||||
JobID uuid.UUID
|
||||
Attempt int
|
||||
Kind string
|
||||
RoutingKey string
|
||||
Payload []byte
|
||||
AvailableAt time.Time
|
||||
PublishedAt *time.Time
|
||||
ClaimedBy string
|
||||
ClaimedUntil *time.Time
|
||||
PublishAttempts int
|
||||
LastError *string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// JobMessage is the broker carrier defined by contracts/jobs.schema.json.
|
||||
type JobMessage struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID uuid.UUID `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID uuid.UUID `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Traceparent string `json:"traceparent,omitempty"`
|
||||
Tracestate string `json:"tracestate,omitempty"`
|
||||
}
|
||||
|
||||
// NewJobMessage builds the only message the application is allowed to publish.
|
||||
func NewJobMessage(job Job) JobMessage {
|
||||
return JobMessage{
|
||||
SchemaVersion: 1,
|
||||
JobID: job.ID,
|
||||
Type: JobType,
|
||||
AttachmentID: job.AttachmentID,
|
||||
PipelineVersion: job.PipelineVersion,
|
||||
Attempt: job.Attempt,
|
||||
CreatedAt: job.CreatedAt.UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
// Validate enforces the closed schema. The worker must never trust paths or
|
||||
// storage metadata from this message.
|
||||
func (m JobMessage) Validate() error {
|
||||
if m.SchemaVersion != 1 {
|
||||
return errors.New("unsupported schema_version")
|
||||
}
|
||||
|
||||
if m.Type != JobType {
|
||||
return fmt.Errorf("unsupported type %q", m.Type)
|
||||
}
|
||||
|
||||
if m.JobID == uuid.Nil || m.AttachmentID == uuid.Nil {
|
||||
return errors.New("job_id and attachment_id are required")
|
||||
}
|
||||
|
||||
if m.PipelineVersion < 1 || m.Attempt < 1 {
|
||||
return errors.New("pipeline_version and attempt must be >= 1")
|
||||
}
|
||||
|
||||
if m.CreatedAt.IsZero() {
|
||||
return errors.New("created_at is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m JobMessage) MarshalPayload() ([]byte, error) {
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func ParseJobMessage(payload []byte) (JobMessage, error) {
|
||||
var message JobMessage
|
||||
|
||||
if err := json.Unmarshal(payload, &message); err != nil {
|
||||
return JobMessage{}, fmt.Errorf("decode job message: %w", err)
|
||||
}
|
||||
|
||||
if err := message.Validate(); err != nil {
|
||||
return JobMessage{}, err
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
// ClaimOutcome explains why a delivery did or did not become the active
|
||||
// processing attempt. The worker maps it to ack/nack without guessing.
|
||||
type ClaimOutcome string
|
||||
|
||||
const (
|
||||
ClaimAcquired ClaimOutcome = "acquired"
|
||||
ClaimAlreadyReady ClaimOutcome = "already_ready"
|
||||
ClaimAlreadyFailed ClaimOutcome = "already_failed"
|
||||
ClaimSuperseded ClaimOutcome = "superseded"
|
||||
ClaimStaleAttempt ClaimOutcome = "stale_attempt"
|
||||
ClaimFutureAttempt ClaimOutcome = "future_attempt"
|
||||
ClaimBusyLease ClaimOutcome = "busy_lease"
|
||||
ClaimNotDue ClaimOutcome = "not_due"
|
||||
ClaimVersionStale ClaimOutcome = "version_stale"
|
||||
ClaimJobNotFound ClaimOutcome = "job_not_found"
|
||||
ClaimSourceMissing ClaimOutcome = "source_missing"
|
||||
ClaimPayloadInvalid ClaimOutcome = "payload_invalid"
|
||||
)
|
||||
|
||||
type ClaimResult struct {
|
||||
Outcome ClaimOutcome
|
||||
Job Job
|
||||
Attachment Attachment
|
||||
}
|
||||
|
||||
// Job event names used by the operator audit trail.
|
||||
const (
|
||||
EventEnqueued = "enqueued"
|
||||
EventClaimed = "claimed"
|
||||
EventRetried = "retried"
|
||||
EventReady = "ready"
|
||||
EventFailed = "failed"
|
||||
EventLeaseExpired = "lease_expired"
|
||||
EventReplayed = "replayed"
|
||||
EventReprocessed = "reprocessed"
|
||||
EventSuperseded = "superseded"
|
||||
)
|
||||
Reference in New Issue
Block a user