Skip to content

Commit

Permalink
rework service.OnStartup to close applicatoin on error
Browse files Browse the repository at this point in the history
  • Loading branch information
atterpac committed Nov 27, 2024
1 parent d27e75c commit 41beb9e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,26 @@ func New(appOptions Options) *App {
result.handleFatalError(fmt.Errorf("Fatal error in application initialisation: " + err.Error()))
}

for _, service := range appOptions.Services {
for i, service := range appOptions.Services {
if thisService, ok := service.instance.(ServiceStartup); ok {
err := thisService.OnStartup(result.ctx, service.options)
if err != nil {
name := service.options.Name
if name == "" {
name = getServiceName(service.instance)
}
globalApplication.Logger.Error("OnStartup() failed:", "service", name, "error", err.Error())
continue
globalApplication.Logger.Error("OnStartup() failed shutting down application:", "service", name, "error", err.Error())
// Run shutdown on all services that have already started
for _, service := range appOptions.Services[:i-1] {
if thisService, ok := service.instance.(ServiceShutdown); ok {
err := thisService.OnShutdown()
if err != nil {
globalApplication.Logger.Error("Error shutting down service: " + err.Error())
}
}
}
// Shutdown the application
os.Exit(1)
}
}
}
Expand Down

0 comments on commit 41beb9e

Please sign in to comment.