Skip to content

Commit

Permalink
[V3] Rework service.OnStartup to close application on error (#3920)
Browse files Browse the repository at this point in the history
* rework service.OnStartup to close applicatoin on error

CHANGELOG.md

fix index out of bounds potential

* os.Exit -> runtime.Goexit

* Revert to os.Exit

---------

Co-authored-by: Lea Anthony <[email protected]>
  • Loading branch information
atterpac and leaanthony authored Nov 30, 2024
1 parent 9173537 commit 0ebb21f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
in [#3888](https://github.com/wailsapp/wails/pull/3888)

### Changed
- `service.OnStartup` now shutdowns the application on error and runs `service.OnShutdown`for any prior services that started by @atterpac in [#3920](https://github.com/wailsapp/wails/pull/3920)
- Refactored systray click messaging to better align with user interactions by @atterpac in [#3907](https://github.com/wailsapp/wails/pull/3907)
- Asset embed to include `all:frontend/dist` to support frameworks that generate subfolders by @atterpac in [#3887](https://github.com/wailsapp/wails/pull/3887)
- Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748)
Expand Down
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] {
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 0ebb21f

Please sign in to comment.