From c870defd4791e7f6a47f0378fe50d9c45c0abca1 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 13 Dec 2023 16:29:07 +0800 Subject: [PATCH] xdepthmaker: improve shutdown process --- pkg/bbgo/activeorderbook.go | 14 ++++++++++++-- pkg/strategy/xdepthmaker/strategy.go | 14 +++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/bbgo/activeorderbook.go b/pkg/bbgo/activeorderbook.go index e33830e1b..6234c5693 100644 --- a/pkg/bbgo/activeorderbook.go +++ b/pkg/bbgo/activeorderbook.go @@ -199,14 +199,23 @@ func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange, // since ctx might be canceled, we should use background context here if err := ex.CancelOrders(context.Background(), orders...); err != nil { - log.WithError(err).Errorf("[ActiveOrderBook] can not cancel %s orders", b.Symbol) + log.WithError(err).Warnf("[ActiveOrderBook] can not cancel %s orders", b.Symbol) } log.Debugf("[ActiveOrderBook] waiting %s for %s orders to be cancelled...", waitTime, b.Symbol) if cancelAll { clear, err := b.waitAllClear(ctx, waitTime, orderCancelTimeout) - if clear || err != nil { + if err != nil { + if !errors.Is(err, context.Canceled) { + log.WithError(err).Errorf("order cancel error") + } + + break + } + + if clear { + log.Debugf("[ActiveOrderBook] %s orders are canceled", b.Symbol) break } @@ -216,6 +225,7 @@ func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange, } else { existingOrders := b.filterExistingOrders(orders) if len(existingOrders) == 0 { + log.Debugf("[ActiveOrderBook] orders are canceled") break } } diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index 5bdf04cb0..a553c9824 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -474,14 +474,11 @@ func (s *Strategy) CrossRun( // wait for the quoter to stop time.Sleep(s.UpdateInterval.Duration()) - shutdownCtx, cancelShutdown := context.WithTimeout(context.TODO(), time.Minute) - defer cancelShutdown() - - if err := s.MakerOrderExecutor.GracefulCancel(shutdownCtx); err != nil { + if err := s.MakerOrderExecutor.GracefulCancel(ctx); err != nil { log.WithError(err).Errorf("graceful cancel %s order error", s.Symbol) } - if err := s.HedgeOrderExecutor.GracefulCancel(shutdownCtx); err != nil { + if err := s.HedgeOrderExecutor.GracefulCancel(ctx); err != nil { log.WithError(err).Errorf("graceful cancel %s order error", s.Symbol) } @@ -890,6 +887,13 @@ func (s *Strategy) cleanUpOpenOrders(ctx context.Context) error { return err } + if len(openOrders) == 0 { + return nil + } + + log.Infof("found existing open orders:") + types.OrderSlice(openOrders).Print() + if err := s.makerSession.Exchange.CancelOrders(ctx, openOrders...); err != nil { return err }