mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 20:12:25 +00:00
add panic recover to run function
This commit is contained in:
@@ -314,8 +314,7 @@ var runCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
nodeApp.Fallback(func(ctx context.Context, cs *corestate.CoreState, x *app.AppX) {
|
||||||
<-ctxMain.Done()
|
|
||||||
if err := srv.Shutdown(ctxMain); err != nil {
|
if err := srv.Shutdown(ctxMain); err != nil {
|
||||||
x.Log.Printf("%s: Failed to stop the server gracefully: %s", logs.PrintError(), err.Error())
|
x.Log.Printf("%s: Failed to stop the server gracefully: %s", logs.PrintError(), err.Error())
|
||||||
} else {
|
} else {
|
||||||
@@ -328,7 +327,10 @@ var runCmd = &cobra.Command{
|
|||||||
x.Log.Printf("%s: Cleanup error: %s", logs.PrintError(), err.Error())
|
x.Log.Printf("%s: Cleanup error: %s", logs.PrintError(), err.Error())
|
||||||
}
|
}
|
||||||
x.Log.Println("bye!")
|
x.Log.Println("bye!")
|
||||||
|
})
|
||||||
|
|
||||||
|
<-ctxMain.Done()
|
||||||
|
nodeApp.CallFallback(ctx)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/akyaiy/GoSally-mvp/internal/core/corestate"
|
"github.com/akyaiy/GoSally-mvp/internal/core/corestate"
|
||||||
@@ -16,14 +17,20 @@ import (
|
|||||||
type AppContract interface {
|
type AppContract interface {
|
||||||
InitialHooks(fn ...func(cs *corestate.CoreState, x *AppX))
|
InitialHooks(fn ...func(cs *corestate.CoreState, x *AppX))
|
||||||
Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error)
|
Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error)
|
||||||
|
Fallback(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX))
|
||||||
|
|
||||||
|
CallFallback(ctx context.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
initHooks []func(cs *corestate.CoreState, x *AppX)
|
initHooks []func(cs *corestate.CoreState, x *AppX)
|
||||||
runHook func(ctx context.Context, cs *corestate.CoreState, x *AppX) error
|
runHook func(ctx context.Context, cs *corestate.CoreState, x *AppX) error
|
||||||
|
fallback func(ctx context.Context, cs *corestate.CoreState, x *AppX)
|
||||||
|
|
||||||
Corestate *corestate.CoreState
|
Corestate *corestate.CoreState
|
||||||
AppX *AppX
|
AppX *AppX
|
||||||
|
|
||||||
|
fallbackOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppX struct {
|
type AppX struct {
|
||||||
@@ -46,6 +53,10 @@ func (a *App) InitialHooks(fn ...func(cs *corestate.CoreState, x *AppX)) {
|
|||||||
a.initHooks = append(a.initHooks, fn...)
|
a.initHooks = append(a.initHooks, fn...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) Fallback(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX)) {
|
||||||
|
a.fallback = fn
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error) {
|
func (a *App) Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX) error) {
|
||||||
a.runHook = fn
|
a.runHook = fn
|
||||||
|
|
||||||
@@ -56,9 +67,30 @@ func (a *App) Run(fn func(ctx context.Context, cs *corestate.CoreState, x *AppX)
|
|||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
a.AppX.Log.Printf("PANIC recovered: %v", r)
|
||||||
|
if a.fallback != nil {
|
||||||
|
a.fallback(ctx, a.Corestate, a.AppX)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var runErr error
|
||||||
if a.runHook != nil {
|
if a.runHook != nil {
|
||||||
if err := a.runHook(ctx, a.Corestate, a.AppX); err != nil {
|
runErr = a.runHook(ctx, a.Corestate, a.AppX)
|
||||||
log.Fatalf("fatal in Run: %v", err)
|
}
|
||||||
|
|
||||||
|
if runErr != nil {
|
||||||
|
a.AppX.Log.Fatalf("fatal in Run: %v", runErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) CallFallback(ctx context.Context) {
|
||||||
|
a.fallbackOnce.Do(func() {
|
||||||
|
if a.fallback != nil {
|
||||||
|
a.fallback(ctx, a.Corestate, a.AppX)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user