diff --git a/compose.yaml b/compose.yaml index 2671fe02..ed54f2b3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -9,6 +9,7 @@ services: secrets: - database_url - cache_url + - broker_url - s3_access_key_id - s3_secret_access_key - s3_bootstrap_access_key_id diff --git a/internal/app/app.go b/internal/app/app.go index 80a3c9f2..a9508d8c 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -16,6 +16,7 @@ import ( "mtusi2026-messenger/internal/config" "mtusi2026-messenger/internal/db" "mtusi2026-messenger/internal/logger" + "mtusi2026-messenger/internal/outbox" "mtusi2026-messenger/internal/postgres" "mtusi2026-messenger/internal/redis" s3store "mtusi2026-messenger/internal/s3" @@ -80,17 +81,27 @@ func Run() error { log.Info("s3 storage ready", "endpoint", cfg.S3.Endpoint, "bucket", s3Client.Bucket()) - cleanupCtx, stopCleanup := context.WithCancel(context.Background()) - var cleanupWG sync.WaitGroup - cleanupWG.Go(func() { - runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval) + brokerURL, err := cfg.Broker.URL() + if err != nil { + log.Error("read broker url", "error", err) + return err + } + publisher := outbox.NewPublisher(pool, brokerURL, log) + + backgroundCtx, stopBackground := context.WithCancel(context.Background()) + var backgroundWG sync.WaitGroup + backgroundWG.Go(func() { + publisher.Run(backgroundCtx) }) - cleanupWG.Go(func() { - runIdempotencyCleanup(cleanupCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval) + backgroundWG.Go(func() { + runSessionCleanup(backgroundCtx, log, queries, sessionCleanupInterval) }) - cleanupWG.Go(func() { + backgroundWG.Go(func() { + runIdempotencyCleanup(backgroundCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval) + }) + backgroundWG.Go(func() { runAttachmentCleanup( - cleanupCtx, + backgroundCtx, log, queries, s3Client, @@ -99,8 +110,8 @@ func Run() error { ) }) defer func() { - stopCleanup() - cleanupWG.Wait() + stopBackground() + backgroundWG.Wait() }() authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log) diff --git a/internal/model/attachment_job.go b/internal/model/attachment_job.go new file mode 100644 index 00000000..d3bffe59 --- /dev/null +++ b/internal/model/attachment_job.go @@ -0,0 +1,17 @@ +package model + +import ( + "time" + + "github.com/google/uuid" +) + +type AttachmentJob 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"` +} diff --git a/internal/outbox/publisher.go b/internal/outbox/publisher.go new file mode 100644 index 00000000..23f17ef4 --- /dev/null +++ b/internal/outbox/publisher.go @@ -0,0 +1,119 @@ +package outbox + +import ( + "context" + "encoding/json" + "log/slog" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/rabbitmq" +) + +const ( + pollInterval = time.Second + retryInterval = 2 * time.Second + publishTimeout = 5 * time.Second +) + +type Publisher struct { + pool *pgxpool.Pool + url string + log *slog.Logger +} + +func NewPublisher(pool *pgxpool.Pool, url string, log *slog.Logger) *Publisher { + return &Publisher{ + pool: pool, + url: url, + log: log, + } +} + +func (p *Publisher) Run(ctx context.Context) { + for ctx.Err() == nil { + client, err := rabbitmq.New(p.url) + if err == nil { + p.log.Info("outbox publisher connected") + err = p.publish(ctx, client) + _ = client.Close() + } + if ctx.Err() != nil { + return + } + p.log.Error("outbox publisher retry", "error", err) + if !wait(ctx, retryInterval) { + return + } + } +} + +func (p *Publisher) publish(ctx context.Context, client *rabbitmq.Client) error { + for ctx.Err() == nil { + published, err := p.publishOne(ctx, client) + if err != nil { + return err + } + if !published && !wait(ctx, pollInterval) { + return ctx.Err() + } + } + return ctx.Err() +} + +func (p *Publisher) publishOne(ctx context.Context, client *rabbitmq.Client) (bool, error) { + ctx, cancel := context.WithTimeout(ctx, publishTimeout) + defer cancel() + + tx, err := p.pool.Begin(ctx) + if err != nil { + return false, err + } + defer tx.Rollback(ctx) + + q := db.New(tx) + rows, err := q.LockPendingOutboxEntries(ctx, 1) + if err != nil { + return false, err + } + if len(rows) == 0 { + return false, nil + } + row := rows[0] + body, err := json.Marshal(model.AttachmentJob{ + SchemaVersion: 1, + JobID: row.JobID, + Type: "attachment.process.v1", + AttachmentID: row.AttachmentID, + PipelineVersion: row.PipelineVersion, + Attempt: row.Attempt, + CreatedAt: row.CreatedAt.UTC(), + }) + if err != nil { + return false, err + } + if err := client.Publish(ctx, row.JobID.String(), body); err != nil { + return false, err + } + if _, err := q.MarkOutboxPublished(ctx, row.ID); err != nil { + return false, err + } + if err := tx.Commit(ctx); err != nil { + return false, err + } + return true, nil +} + +func wait(ctx context.Context, delay time.Duration) bool { + timer := time.NewTimer(delay) + defer timer.Stop() + select { + case <-ctx.Done(): + return false + case <-timer.C: + return true + } +}