// 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()) }