feat(outbox): relay committed outbox events to Kafka
This commit is contained in:
1 parent
78f1651f6b
commit
6c3b34f73e
42 files changed
+3731
-45
No files matched your search
@@ -17,6 +17,7 @@ import (
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/worker/outboxrelay"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -32,6 +33,9 @@ type Application struct {
|
||||
// mailSender is the transport selected by EMAIL_PROVIDER. No usecase
|
||||
// sends mail yet; the delivery layer will take it from here.
|
||||
mailSender outbound.MailSender
|
||||
// relay publishes outbox events while the servers run. It is nil when
|
||||
// the relay is disabled or the database is not configured.
|
||||
relay *outboxrelay.Relay
|
||||
}
|
||||
|
||||
func NewApplication(
|
||||
@@ -40,8 +44,16 @@ func NewApplication(
|
||||
metrics *prometheus.Metrics,
|
||||
logger *slog.Logger,
|
||||
mailSender outbound.MailSender,
|
||||
relay *outboxrelay.Relay,
|
||||
) *Application {
|
||||
return &Application{config: config, apiHandler: apiHandler, metrics: metrics, logger: logger, mailSender: mailSender}
|
||||
return &Application{
|
||||
config: config,
|
||||
apiHandler: apiHandler,
|
||||
metrics: metrics,
|
||||
logger: logger,
|
||||
mailSender: mailSender,
|
||||
relay: relay,
|
||||
}
|
||||
}
|
||||
|
||||
func Run() (runErr error) {
|
||||
@@ -137,10 +149,16 @@ func (application *Application) run(ctx context.Context) error {
|
||||
slog.String("metrics_addr", application.config.MetricsAddr),
|
||||
)
|
||||
|
||||
// The relay stops when the servers stop, including after a server
|
||||
// failure that leaves ctx running.
|
||||
runCtx, stopRun := context.WithCancel(ctx)
|
||||
defer stopRun()
|
||||
relayStopped := application.startRelay(runCtx)
|
||||
|
||||
apiServer := newHTTPServer(application.apiHandler)
|
||||
metricsServer := newHTTPServer(application.metrics.Handler())
|
||||
err = serveServers(
|
||||
ctx,
|
||||
runCtx,
|
||||
apiListener,
|
||||
apiServer,
|
||||
metricsListener,
|
||||
@@ -148,9 +166,28 @@ func (application *Application) run(ctx context.Context) error {
|
||||
application.config.ShutdownTimeout,
|
||||
)
|
||||
application.logger.Info("servers stopped")
|
||||
stopRun()
|
||||
// The relay finishes its pass in progress within SHUTDOWN_TIMEOUT, so
|
||||
// the cleanup that closes its pool and producer runs after it.
|
||||
<-relayStopped
|
||||
return err
|
||||
}
|
||||
|
||||
// startRelay runs the relay until ctx ends and returns a channel that closes
|
||||
// when it has stopped.
|
||||
func (application *Application) startRelay(ctx context.Context) <-chan struct{} {
|
||||
stopped := make(chan struct{})
|
||||
if application.relay == nil {
|
||||
close(stopped)
|
||||
return stopped
|
||||
}
|
||||
go func() {
|
||||
defer close(stopped)
|
||||
application.relay.Run(ctx)
|
||||
}()
|
||||
return stopped
|
||||
}
|
||||
|
||||
func newHTTPServer(handler http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Handler: handler,
|
||||
|
||||
Reference in new issue
Block a user