@@ -19,6 +19,7 @@ type AppConfig struct {
1919 HealthCheck * HealthCheck
2020 WebApp * WebApp
2121 Worker * Worker
22+ EventBus * EventBus
2223 ShutdownTimeout time.Duration
2324}
2425
@@ -45,6 +46,10 @@ func NewApp(config *AppConfig) *App {
4546 config .ShutdownTimeout = 10 * time .Second
4647 }
4748
49+ config .EventBus = NewEventBus (
50+ & EventBusConfig {},
51+ )
52+
4853 return & App {
4954 config ,
5055 }
@@ -62,6 +67,7 @@ func (app *App) Run() error {
6267
6368 var healthCheckServer * http.Server
6469 var webAppServer * http.Server
70+ var workerServer * WorkerServer
6571
6672 signal .Notify (signals , syscall .SIGINT , syscall .SIGTERM )
6773
@@ -104,25 +110,35 @@ func (app *App) Run() error {
104110 }
105111
106112 if app .Worker != nil {
107- go func (w * Worker ) {
108- log .Info ().Msg ("starting worker" )
109- w .Start ()
110- }(app .Worker )
113+ workerServer = NewWorkerServer ()
114+ go func () {
115+ log .Info ().Msg ("starting worker server" )
116+ ctx := context .Background ()
117+ workerServer .Start (ctx , app .Worker )
118+ }()
111119 }
112120
113121 <- shutdown
114122
115123 shutdownCtx , cancel := context .WithTimeout (context .Background (), app .ShutdownTimeout )
116124 defer cancel ()
117125
118- if err := webAppServer .Shutdown (shutdownCtx ); err != nil {
119- log .Fatal ().Err (err ).Msg ("main server shutdown failed" )
120- }
121-
122126 if err := healthCheckServer .Shutdown (shutdownCtx ); err != nil {
123127 log .Fatal ().Err (err ).Msg ("health check server shutdown failed" )
124128 }
125129
130+ if app .WebApp != nil && webAppServer != nil {
131+ if err := webAppServer .Shutdown (shutdownCtx ); err != nil {
132+ log .Fatal ().Err (err ).Msg ("main server shutdown failed" )
133+ }
134+ }
135+
136+ if app .Worker != nil && workerServer != nil {
137+ if err := workerServer .Shutdown (shutdownCtx ); err != nil {
138+ log .Fatal ().Err (err ).Msg ("worker shutdown failed" )
139+ }
140+ }
141+
126142 log .Info ().Msg ("shutdown complete" )
127143
128144 return nil
0 commit comments