From 41beb9e44f5b0786bcaddc3a5728ca46ce7ee756 Mon Sep 17 00:00:00 2001 From: atterpac Date: Wed, 27 Nov 2024 14:01:51 -0700 Subject: [PATCH] rework service.OnStartup to close applicatoin on error --- v3/pkg/application/application.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 9d575a14536..b0652bdc256 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -136,7 +136,7 @@ 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 { @@ -144,8 +144,18 @@ func New(appOptions Options) *App { 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) } } }