improve shutdown and app service boilerplate
This commit is contained in:
77
main.go
77
main.go
@ -15,20 +15,29 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
|
||||
optsgrpc "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
||||
optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demogrpc"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demohttp"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demo"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/service"
|
||||
)
|
||||
|
||||
var flagSchema bool
|
||||
const terminationGracePeriod = 30 * time.Second
|
||||
|
||||
var (
|
||||
flagSchema bool
|
||||
|
||||
// TODO: Register your service.AppService implementers here
|
||||
appServices = []service.AppService{
|
||||
&demo.DemoService{},
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cncl := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
|
||||
@ -37,7 +46,9 @@ func main() {
|
||||
// Load configuration and setup logging. The go-app framework
|
||||
// will handle loading config and environment into our demo
|
||||
// app config struct which embeds app.AooConfig
|
||||
ctx, demoApp := app.MustLoadConfigInto(ctx, &config.DemoConfig{})
|
||||
ctx, serviceConfig := app.MustLoadConfigInto(ctx, &config.ServiceConfig{})
|
||||
|
||||
log := zerolog.Ctx(ctx)
|
||||
|
||||
// Print schema if that's all we have to do
|
||||
if flagSchema {
|
||||
@ -45,32 +56,50 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Debug().Any("demoAppMergedConfig", demoApp).Msg("demo app config prepared")
|
||||
log.Debug().Any("mergedConfig", serviceConfig).Msg("App configuration prepared")
|
||||
|
||||
// Prepare servers
|
||||
demoHTTP := demohttp.NewDemoHTTPServer(ctx, demoApp)
|
||||
demoGRPC := demogrpc.NewDemoGRPCServer(ctx, demoApp)
|
||||
// Prepare services
|
||||
var err error
|
||||
sdFuncs := make([]service.ShutdownFunc, len(appServices))
|
||||
for i, svc := range appServices {
|
||||
sdFuncs[i], err = svc.Init(ctx, serviceConfig)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Send()
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare app
|
||||
// Merge all service implementations for app
|
||||
grpcSvcs, httpSvcs := service.MergeServices(ctx, appServices...)
|
||||
|
||||
// Prepare app with all merged services
|
||||
app := &app.App{
|
||||
AppContext: ctx,
|
||||
GRPC: &optsgrpc.AppGRPC{
|
||||
Services: demoGRPC.GetServices(),
|
||||
GRPCDialOpts: demoGRPC.GetDialOpts(),
|
||||
},
|
||||
HTTP: &optshttp.AppHTTP{
|
||||
Ctx: ctx,
|
||||
HealthChecks: demoHTTP.GetHealthCheckFuncs(),
|
||||
Funcs: demoHTTP.GetHandleFuncs(),
|
||||
},
|
||||
GRPC: grpcSvcs,
|
||||
HTTP: httpSvcs,
|
||||
}
|
||||
|
||||
// Launch app
|
||||
app.MustRun()
|
||||
|
||||
// Wait for app to complete
|
||||
// Perform any extra shutdown here
|
||||
// Wait for app to complete and then perform internal shutdown
|
||||
<-app.Done()
|
||||
|
||||
log.Info().Int("numServices", len(appServices)).Msg("shutting down app services")
|
||||
|
||||
sdCtx, cncl := context.WithTimeout(context.Background(), terminationGracePeriod)
|
||||
defer cncl()
|
||||
|
||||
// Call registered shutdown funcs from all services
|
||||
var sdWg sync.WaitGroup
|
||||
for _, sdFunc := range sdFuncs {
|
||||
sdWg.Add(1)
|
||||
go func() {
|
||||
defer sdWg.Done()
|
||||
svcName, err := sdFunc(sdCtx)
|
||||
log.Err(err).Str("service", svcName).Msg("terminated")
|
||||
}()
|
||||
}
|
||||
sdWg.Wait()
|
||||
}
|
||||
|
||||
// flag.Parse will be called by go-app
|
||||
@ -79,7 +108,7 @@ func init() {
|
||||
}
|
||||
|
||||
func printSchema() {
|
||||
bytes, err := app.CustomSchema(&config.DemoConfig{})
|
||||
bytes, err := app.CustomSchema(&config.ServiceConfig{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user