mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
bbgo: fix: pass isolated context to SaveState() call
This commit is contained in:
parent
2378951c85
commit
2fbe90b1e7
|
@ -402,6 +402,7 @@ func (trader *Trader) IterateStrategies(f func(st StrategyID) error) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTICE: the ctx here is the trading context, which could already be canceled.
|
||||||
func (trader *Trader) SaveState(ctx context.Context) error {
|
func (trader *Trader) SaveState(ctx context.Context) error {
|
||||||
if trader.environment.BacktestService != nil {
|
if trader.environment.BacktestService != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -124,7 +124,7 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
_ = grpcBind
|
_ = grpcBind
|
||||||
_ = enableGrpc
|
_ = enableGrpc
|
||||||
|
|
||||||
ctx, cancelTrading := context.WithCancel(basectx)
|
tradingCtx, cancelTrading := context.WithCancel(basectx)
|
||||||
defer cancelTrading()
|
defer cancelTrading()
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
|
@ -135,21 +135,21 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
}
|
}
|
||||||
|
|
||||||
if lightweight {
|
if lightweight {
|
||||||
if err := bbgo.BootstrapEnvironmentLightweight(ctx, environ, userConfig); err != nil {
|
if err := bbgo.BootstrapEnvironmentLightweight(tradingCtx, environ, userConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := bbgo.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
|
if err := bbgo.BootstrapEnvironment(tradingCtx, environ, userConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := environ.Init(ctx); err != nil {
|
if err := environ.Init(tradingCtx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !noSync {
|
if !noSync {
|
||||||
if err := environ.Sync(ctx, userConfig); err != nil {
|
if err := environ.Sync(tradingCtx, userConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,7 +167,7 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := trader.Run(ctx); err != nil {
|
if err := trader.Run(tradingCtx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
Trader: trader,
|
Trader: trader,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Run(ctx, webServerBind); err != nil {
|
if err := s.Run(tradingCtx, webServerBind); err != nil {
|
||||||
log.WithError(err).Errorf("http server bind error")
|
log.WithError(err).Errorf("http server bind error")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -198,18 +198,19 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
cmdutil.WaitForSignal(tradingCtx, syscall.SIGINT, syscall.SIGTERM)
|
||||||
cancelTrading()
|
cancelTrading()
|
||||||
|
|
||||||
gracefulShutdownPeriod := 30 * time.Second
|
gracefulShutdownPeriod := 30 * time.Second
|
||||||
shtCtx, cancelShutdown := context.WithTimeout(bbgo.NewTodoContextWithExistingIsolation(ctx), gracefulShutdownPeriod)
|
shtCtx, cancelShutdown := context.WithTimeout(bbgo.NewTodoContextWithExistingIsolation(tradingCtx), gracefulShutdownPeriod)
|
||||||
bbgo.Shutdown(shtCtx)
|
bbgo.Shutdown(shtCtx)
|
||||||
cancelShutdown()
|
|
||||||
|
|
||||||
if err := trader.SaveState(nil); err != nil {
|
if err := trader.SaveState(shtCtx); err != nil {
|
||||||
log.WithError(err).Errorf("can not save strategy states")
|
log.WithError(err).Errorf("can not save strategy persistence states")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancelShutdown()
|
||||||
|
|
||||||
for _, session := range environ.Sessions() {
|
for _, session := range environ.Sessions() {
|
||||||
if err := session.MarketDataStream.Close(); err != nil {
|
if err := session.MarketDataStream.Close(); err != nil {
|
||||||
log.WithError(err).Errorf("[%s] market data stream close error", session.Name)
|
log.WithError(err).Errorf("[%s] market data stream close error", session.Name)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user