forked from mtusi2026/mtusi2026lab5
- RabbitMQ quorum queue, persistent messages and publisher confirms - S3 staging plus PostgreSQL attachments/jobs outbox transaction - worker variants (thumbnail, crop, optimized WebP), DB lease and lease sweeper - jittered retry, permanent failures, explicit replay and admission cap - deterministic crash hooks, resilience script and evidence for grade 4 - async-aware custom tests, oversized pixel case, docs and changelog
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
// Package outbox publishes committed job intents to RabbitMQ. The database row
|
|
// is the outbox: published_at IS NULL means "this intent still needs a broker
|
|
// confirm". A crash between COMMIT and publish is recovered by the next loop.
|
|
package outbox
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/broker"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/model"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
|
)
|
|
|
|
const batchSize = 20
|
|
|
|
type Publisher struct {
|
|
store *store.Store
|
|
broker *broker.Publisher
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func New(st *store.Store, b *broker.Publisher, logger *slog.Logger) *Publisher {
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
return &Publisher{store: st, broker: b, logger: logger}
|
|
}
|
|
|
|
func (p *Publisher) Run(ctx context.Context) {
|
|
ticker := time.NewTicker(250 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
if err := p.PublishBatch(ctx); err != nil {
|
|
p.logger.Error("outbox publish pass failed", "err", err)
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-time.After(time.Second):
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *Publisher) PublishBatch(ctx context.Context) error {
|
|
jobs, err := p.store.ListPublishableJobs(ctx, batchSize)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, record := range jobs {
|
|
if err = p.publish(ctx, record); err != nil {
|
|
return fmt.Errorf("publish job %s: %w", record.JobID, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Publisher) publish(ctx context.Context, record model.JobRecord) error {
|
|
publishCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
message := model.Job{
|
|
SchemaVersion: 1,
|
|
JobID: record.JobID,
|
|
Type: record.Type,
|
|
AttachmentID: record.AttachmentID,
|
|
PipelineVersion: record.PipelineVersion,
|
|
Attempt: record.Attempt + 1,
|
|
CreatedAt: record.CreatedAt,
|
|
}
|
|
if err := p.broker.Publish(publishCtx, message); err != nil {
|
|
return err
|
|
}
|
|
// Compare-and-set on attempt/status: a worker may have claimed it, and a
|
|
// retry may have been scheduled while the confirm was in flight.
|
|
return p.store.MarkJobPublished(publishCtx, record.JobID, record.Attempt, time.Now().UTC())
|
|
}
|