feat(outbox): lab5 run outbox background publisher in api server
This commit is contained in:
@@ -13,11 +13,13 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"high-loud-apps-labs/internal/broker"
|
||||||
"high-loud-apps-labs/internal/config"
|
"high-loud-apps-labs/internal/config"
|
||||||
"high-loud-apps-labs/internal/domain"
|
"high-loud-apps-labs/internal/domain"
|
||||||
"high-loud-apps-labs/internal/httpapi"
|
"high-loud-apps-labs/internal/httpapi"
|
||||||
"high-loud-apps-labs/internal/httpapi/handler"
|
"high-loud-apps-labs/internal/httpapi/handler"
|
||||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||||
|
"high-loud-apps-labs/internal/outbox"
|
||||||
"high-loud-apps-labs/internal/service"
|
"high-loud-apps-labs/internal/service"
|
||||||
"high-loud-apps-labs/internal/storage/postgres"
|
"high-loud-apps-labs/internal/storage/postgres"
|
||||||
redisstore "high-loud-apps-labs/internal/storage/redis"
|
redisstore "high-loud-apps-labs/internal/storage/redis"
|
||||||
@@ -55,6 +57,17 @@ func main() {
|
|||||||
|
|
||||||
store := postgres.New(pool)
|
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)
|
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
||||||
defer closeSession()
|
defer closeSession()
|
||||||
|
|
||||||
@@ -145,6 +158,8 @@ func main() {
|
|||||||
|
|
||||||
log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain)
|
log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain)
|
||||||
ready.Store(false)
|
ready.Store(false)
|
||||||
|
|
||||||
|
outboxCancel()
|
||||||
reconcilerCancel()
|
reconcilerCancel()
|
||||||
|
|
||||||
if cfg.ShutdownDrain > 0 {
|
if cfg.ShutdownDrain > 0 {
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user