Skip to content

Commit

Permalink
fixed graceful shutdown handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 8, 2023
1 parent d86e20b commit 506b759
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
- Trigger the `app.OnTerminate()` hook on `app.Restart()` call.
_A new bool `IsRestart` field was also added to the `core.TerminateEvent` event._

- Fixed the graceful shutdown handling.


## v0.20.0-rc3

Expand Down
24 changes: 24 additions & 0 deletions apis/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"path/filepath"
"strings"
"sync"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -189,14 +190,37 @@ func Serve(app core.App, config ServeConfig) (*http.Server, error) {
regular.Printf("└─ Admin UI: %s\n", color.CyanString("%s://%s/_/", schema, addr))
}

// WaitGroup to block until server.ShutDown() returns because Serve and similar methods exit immediately.
// Note that the WaitGroup would not do anything if the app.OnTerminate() hook isn't triggered.
var wg sync.WaitGroup

// try to gracefully shutdown the server on app termination
app.OnTerminate().Add(func(e *core.TerminateEvent) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

wg.Add(1)
server.Shutdown(ctx)
if e.IsRestart {
// wait for execve up to 3 seconds before exit
time.AfterFunc(3*time.Second, func() {
wg.Done()
})
} else {
wg.Done()
}

return nil
})

// wait for the graceful shutdown to complete before exit
defer wg.Wait()

// ---
// @todo consider removing the server return value because it is
// not really useful when combined with the blocking serve calls
// ---

// start HTTPS server
if config.HttpsAddr != "" {
// if httpAddr is set, start an HTTP server to redirect the traffic to the HTTPS version
Expand Down
17 changes: 6 additions & 11 deletions core/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,15 @@ 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{
return app.OnTerminate().Trigger(&TerminateEvent{
App: app,
IsRestart: true,
})
if terminateErr != nil {
return terminateErr
}
}, func(e *TerminateEvent) error {
// attempt to restart the bootstrap process in case execve returns an error for some reason
defer app.Bootstrap()

return syscall.Exec(execPath, os.Args, os.Environ())
return syscall.Exec(execPath, os.Args, os.Environ())
})
}

// RefreshSettings reinitializes and reloads the stored application settings.
Expand Down
2 changes: 1 addition & 1 deletion examples/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func main() {
// GitHub selfupdate
ghupdate.MustRegister(app, app.RootCmd, ghupdate.Config{})

app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
app.OnAfterBootstrap().PreAdd(func(e *core.BootstrapEvent) error {
app.Dao().ModelQueryTimeout = time.Duration(queryTimeout) * time.Second
return nil
})
Expand Down
3 changes: 2 additions & 1 deletion pocketbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,13 @@ func (pb *PocketBase) Execute() error {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
<-sigch

done <- true
}()

// execute the root command
go func() {
// leave to the commands to decide whether to print their error or not
// note: leave to the commands to decide whether to print their error
pb.RootCmd.Execute()

done <- true
Expand Down

0 comments on commit 506b759

Please sign in to comment.