From c75eb6b5ba4fcec95f4dd2d485d21e8be3c4bd78 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 16 Feb 2021 16:13:52 +0800 Subject: [PATCH] pull out Persistence injection to the common injection --- pkg/bbgo/trader.go | 54 ++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 85f3f5208..2b6c4103c 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -206,7 +206,7 @@ func (trader *Trader) getSessionOrderExecutor(sessionName string) OrderExecutor // Since the risk controls are loaded from the config file if trader.riskControls != nil && trader.riskControls.SessionBasedRiskControl != nil { - if control, ok := trader.riskControls.SessionBasedRiskControl[sessionName] ; ok { + if control, ok := trader.riskControls.SessionBasedRiskControl[sessionName]; ok { control.SetBaseOrderExecutor(session.orderExecutor) // pick the wrapped order executor @@ -260,33 +260,6 @@ func (trader *Trader) Run(ctx context.Context) error { continue } - if field, ok := hasField(rs, "Persistence"); ok { - if trader.environment.PersistenceServiceFacade == nil { - log.Warnf("strategy has Persistence field but persistence service is not defined") - } else { - log.Infof("found Persistence field, injecting...") - if field.IsNil() { - field.Set(reflect.ValueOf(&Persistence{ - PersistenceSelector: &PersistenceSelector{ - StoreID: "default", - Type: "memory", - }, - Facade: trader.environment.PersistenceServiceFacade, - })) - } else { - elem := field.Elem() - if elem.Kind() != reflect.Struct { - return fmt.Errorf("the field Persistence is not a struct element") - } - - if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil { - log.WithError(err).Errorf("strategy Persistence injection failed") - return err - } - } - } - } - if err := trader.injectCommonServices(rs); err != nil { return err } @@ -312,6 +285,31 @@ func (trader *Trader) injectCommonServices(rs reflect.Value) error { return errors.Wrap(err, "failed to inject Notifiability") } + if field, ok := hasField(rs, "Persistence"); ok { + if trader.environment.PersistenceServiceFacade == nil { + log.Warnf("strategy has Persistence field but persistence service is not defined") + } else { + if field.IsNil() { + field.Set(reflect.ValueOf(&Persistence{ + PersistenceSelector: &PersistenceSelector{ + StoreID: "default", + Type: "memory", + }, + Facade: trader.environment.PersistenceServiceFacade, + })) + } else { + elem := field.Elem() + if elem.Kind() != reflect.Struct { + return fmt.Errorf("field Persistence is not a struct element") + } + + if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil { + return errors.Wrap(err, "failed to inject Persistence") + } + } + } + } + return nil }