fix graceful shutdown

This commit is contained in:
c9s 2021-05-10 02:17:19 +08:00
parent 405f9c863f
commit dde998aced
2 changed files with 7 additions and 6 deletions

View File

@ -159,13 +159,12 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableWebServer
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
cancelTrading()
log.Infof("shutting down stratgies...")
shutdownCtx, cancelShutdown := context.WithDeadline(ctx, time.Now().Add(30*time.Second))
log.Infof("shutting down...")
trader.Graceful.Shutdown(shutdownCtx)
cancelShutdown()
cancelTrading()
return nil
}

View File

@ -487,9 +487,11 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
select {
case <-s.stopC:
log.Warnf("%s maker goroutine stopped, due to the stop signal", s.Symbol)
return
case <-ctx.Done():
log.Warnf("%s maker goroutine stopped, due to the cancelled context", s.Symbol)
return
case <-quoteTicker.C:
@ -510,7 +512,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
close(s.stopC)
time.Sleep(1 * time.Second)
time.Sleep(s.UpdateInterval.Duration())
for {
if err := s.makerSession.Exchange.CancelOrders(ctx, s.activeMakerOrders.Orders()...); err != nil {
@ -534,7 +536,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
log.Infof("state is saved => %+v", s.state)
s.Notify("%s hedge position %f is saved", s.Symbol, s.state.HedgePosition.Float64())
s.Notify("%s position is saved: position = %f", s.Symbol, s.state.HedgePosition.Float64())
}
})