From 4c473385b2b24ca065a4fbeb92556f18f7d9bbb7 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 8 Dec 2023 15:45:58 +0200 Subject: [PATCH] trigger OnTerminate() hook on app.Restart() call --- CHANGELOG.md | 3 +++ core/base.go | 15 +++++++++++++++ core/events.go | 3 ++- pocketbase.go | 3 +-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0fd471..4a858239 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,9 @@ - Use `IS NOT` instead of `!=` as not-equal SQL query operator to handle the cases when comparing with nullable columns or expressions (eg. `json_extract` over `json` field). _Based on my local dataset I wasn't able to find a significant difference in the performance between the 2 operators, but if you stumble on a query that you think may be affected negatively by this, please report it and I'll test it further._ +- Trigger the `app.OnTerminate()` hook on `app.Restart()` call. + _A new bool `IsRestart` field was also added to the `core.TerminateEvent` event._ + ## v0.20.0-rc3 diff --git a/core/base.go b/core/base.go index 4d0c0a34..9f3ed968 100644 --- a/core/base.go +++ b/core/base.go @@ -553,6 +553,21 @@ func (app *BaseApp) Restart() error { return err } + // restart the app bootstrap as a fallback in case the + // terminate event or execve fails for some reason + defer app.Bootstrap() + + // optimistically trigger the terminate event + terminateErr := app.OnTerminate().Trigger(&TerminateEvent{ + App: app, + IsRestart: true, + }, func(e *TerminateEvent) error { + return e.App.ResetBootstrapState() + }) + if terminateErr != nil { + return terminateErr + } + return syscall.Exec(execPath, os.Args, os.Environ()) } diff --git a/core/events.go b/core/events.go index 6e18f091..d12e0e6e 100644 --- a/core/events.go +++ b/core/events.go @@ -70,7 +70,8 @@ type BootstrapEvent struct { } type TerminateEvent struct { - App App + App App + IsRestart bool } type ServeEvent struct { diff --git a/pocketbase.go b/pocketbase.go index 648d9f22..0ce85f48 100644 --- a/pocketbase.go +++ b/pocketbase.go @@ -169,8 +169,7 @@ func (pb *PocketBase) Execute() error { return pb.OnTerminate().Trigger(&core.TerminateEvent{ App: pb, }, func(e *core.TerminateEvent) error { - e.App.ResetBootstrapState() - return nil + return e.App.ResetBootstrapState() }) }