improve server startup

This commit is contained in:
Ryan McGuire 2025-03-07 17:19:42 -05:00
parent e178956eef
commit f0a699029a

View File

@ -2,6 +2,7 @@ package app
import (
"errors"
"sync"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
@ -41,26 +42,38 @@ func (a *App) MustRun() {
ctx, initSpan := a.tracer.Start(a.AppContext, "init")
defer initSpan.End()
var serverWG sync.WaitGroup
// Start HTTP (does not block)
if a.cfg.HTTPEnabled() {
serverWG.Add(1)
go func() {
defer serverWG.Done()
if err := a.initHTTP(ctx); err != nil {
initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error())
}
initSpan.AddEvent("http server started")
initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs)))
}()
}
// Start GRPC (does not block)
if a.cfg.GRPCEnabled() {
serverWG.Add(1)
go func() {
defer serverWG.Done()
if err := a.initGRPC(ctx); err != nil {
initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error())
}
initSpan.AddEvent("grpc server started")
initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services)))
}()
}
serverWG.Wait()
// Monitor app lifecycle
go a.run()