From 4caa457fbe0a32eedac08a79fe99d79b8f51273d Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 5 Oct 2022 18:42:31 +0800 Subject: [PATCH] bbgo: pull out ConfigurePersistence method to simple function --- pkg/bbgo/bootstrap.go | 4 ++-- pkg/bbgo/environment.go | 26 -------------------------- pkg/bbgo/isolation.go | 7 +++++++ pkg/bbgo/persistence.go | 27 +++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/pkg/bbgo/bootstrap.go b/pkg/bbgo/bootstrap.go index 73188c7d2..c635ba9cd 100644 --- a/pkg/bbgo/bootstrap.go +++ b/pkg/bbgo/bootstrap.go @@ -15,7 +15,7 @@ func BootstrapEnvironmentLightweight(ctx context.Context, environ *Environment, } if userConfig.Persistence != nil { - if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil { + if err := ConfigurePersistence(userConfig.Persistence); err != nil { return errors.Wrap(err, "persistence configure error") } } @@ -33,7 +33,7 @@ func BootstrapEnvironment(ctx context.Context, environ *Environment, userConfig } if userConfig.Persistence != nil { - if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil { + if err := ConfigurePersistence(userConfig.Persistence); err != nil { return errors.Wrap(err, "persistence configure error") } } diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 455a8d640..9f57420db 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -13,7 +13,6 @@ import ( "sync" "time" - "github.com/codingconcepts/env" "github.com/pkg/errors" "github.com/pquerna/otp" log "github.com/sirupsen/logrus" @@ -276,31 +275,6 @@ func (environ *Environment) Start(ctx context.Context) (err error) { return } -func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error { - if conf.Redis != nil { - if err := env.Set(conf.Redis); err != nil { - return err - } - - redisPersistence := service.NewRedisPersistenceService(conf.Redis) - persistenceServiceFacade.Redis = redisPersistence - } - - if conf.Json != nil { - if _, err := os.Stat(conf.Json.Directory); os.IsNotExist(err) { - if err2 := os.MkdirAll(conf.Json.Directory, 0777); err2 != nil { - log.WithError(err2).Errorf("can not create directory: %s", conf.Json.Directory) - return err2 - } - } - - jsonPersistence := &service.JsonPersistenceService{Directory: conf.Json.Directory} - persistenceServiceFacade.Json = jsonPersistence - } - - return nil -} - func (environ *Environment) SetStartTime(t time.Time) *Environment { environ.startTime = t return environ diff --git a/pkg/bbgo/isolation.go b/pkg/bbgo/isolation.go index f85b8d915..44df20034 100644 --- a/pkg/bbgo/isolation.go +++ b/pkg/bbgo/isolation.go @@ -22,6 +22,13 @@ func NewDefaultIsolation() *Isolation { } } +func NewIsolation(persistenceFacade *service.PersistenceServiceFacade) *Isolation { + return &Isolation{ + gracefulShutdown: GracefulShutdown{}, + persistenceServiceFacade: persistenceFacade, + } +} + func GetIsolationFromContext(ctx context.Context) *Isolation { isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation) if ok { diff --git a/pkg/bbgo/persistence.go b/pkg/bbgo/persistence.go index 8d2a2aa33..3a555cb1c 100644 --- a/pkg/bbgo/persistence.go +++ b/pkg/bbgo/persistence.go @@ -2,8 +2,10 @@ package bbgo import ( "context" + "os" "reflect" + "github.com/codingconcepts/env" log "github.com/sirupsen/logrus" "github.com/c9s/bbgo/pkg/dynamic" @@ -70,3 +72,28 @@ func storePersistenceFields(obj interface{}, id string, persistence service.Pers return store.Save(inf) }) } + +func ConfigurePersistence(conf *PersistenceConfig) error { + if conf.Redis != nil { + if err := env.Set(conf.Redis); err != nil { + return err + } + + redisPersistence := service.NewRedisPersistenceService(conf.Redis) + persistenceServiceFacade.Redis = redisPersistence + } + + if conf.Json != nil { + if _, err := os.Stat(conf.Json.Directory); os.IsNotExist(err) { + if err2 := os.MkdirAll(conf.Json.Directory, 0777); err2 != nil { + log.WithError(err2).Errorf("can not create directory: %s", conf.Json.Directory) + return err2 + } + } + + jsonPersistence := &service.JsonPersistenceService{Directory: conf.Json.Directory} + persistenceServiceFacade.Json = jsonPersistence + } + + return nil +}