From 872406773ff005baef76667326bdefc61815ff30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 9 Nov 2026 09:12:48 +0300 Subject: [PATCH] feat(outbox): lab5 run outbox background publisher in api server --- backend/cmd/server/main.go | 15 ++++++ backend/internal/outbox/publisher.go | 72 ++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 backend/internal/outbox/publisher.go diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index abadff16..66ab20a1 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -13,11 +13,13 @@ import ( "syscall" "time" + "high-loud-apps-labs/internal/broker" "high-loud-apps-labs/internal/config" "high-loud-apps-labs/internal/domain" "high-loud-apps-labs/internal/httpapi" "high-loud-apps-labs/internal/httpapi/handler" "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/outbox" "high-loud-apps-labs/internal/service" "high-loud-apps-labs/internal/storage/postgres" redisstore "high-loud-apps-labs/internal/storage/redis" @@ -55,6 +57,17 @@ func main() { store := postgres.New(pool) + rabbitPub, err := broker.NewRabbitPublisher(cfg.RabbitMQURL) + if err != nil { + log.Fatalf("rabbitmq: %v", err) + } + defer rabbitPub.Close() + + outboxPub := outbox.NewPublisher(store.Outbox(), rabbitPub, 50) + outboxCtx, outboxCancel := context.WithCancel(context.Background()) + defer outboxCancel() + go outboxPub.Run(outboxCtx, cfg.OutboxInterval) + sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store) defer closeSession() @@ -145,6 +158,8 @@ func main() { log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain) ready.Store(false) + + outboxCancel() reconcilerCancel() if cfg.ShutdownDrain > 0 { diff --git a/backend/internal/outbox/publisher.go b/backend/internal/outbox/publisher.go new file mode 100644 index 00000000..2eb5ef29 --- /dev/null +++ b/backend/internal/outbox/publisher.go @@ -0,0 +1,72 @@ +package outbox + +import ( + "context" + "log" + "time" + + "high-loud-apps-labs/internal/broker" + "high-loud-apps-labs/internal/domain" +) + +type Publisher struct { + repo domain.OutboxRepository + broker broker.Publisher + batch int +} + +func NewPublisher(repo domain.OutboxRepository, b broker.Publisher, batch int) *Publisher { + if batch <= 0 { + batch = 50 + } + return &Publisher{repo: repo, broker: b, batch: batch} +} + +func (p *Publisher) Run(ctx context.Context, interval time.Duration) { + if interval <= 0 { + interval = 1 * time.Second + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + log.Printf("outbox: publisher запущен (interval=%s)", interval) + + for { + select { + case <-ctx.Done(): + log.Println("outbox: publisher остановлен") + return + case <-ticker.C: + p.tick(ctx) + } + } +} + +func (p *Publisher) tick(ctx context.Context) { + entries, err := p.repo.ClaimBatch(ctx, p.batch) + if err != nil { + log.Printf("outbox: ошибка ClaimBatch: %v", err) + return + } + if len(entries) == 0 { + return + } + + var successIDs []string + for _, e := range entries { + err := p.broker.Publish(ctx, "process", e.Payload) + if err != nil { + log.Printf("outbox: ошибка публикации %s: %v", e.ID, err) + break // Прерываем пачку + } + successIDs = append(successIDs, e.ID) + } + + if len(successIDs) > 0 { + if err := p.repo.MarkPublished(ctx, successIDs); err != nil { + log.Printf("outbox: ошибка MarkPublished: %v", err) + } else { + log.Printf("outbox: успешно опубликовано %d сообщений", len(successIDs)) + } + } +}