bbgo: pull out ConfigurePersistence method to simple function

This commit is contained in:
c9s 2022-10-05 18:42:31 +08:00
parent 4d42a61607
commit 4caa457fbe
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 36 additions and 28 deletions

View File

@ -15,7 +15,7 @@ func BootstrapEnvironmentLightweight(ctx context.Context, environ *Environment,
} }
if userConfig.Persistence != nil { 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") return errors.Wrap(err, "persistence configure error")
} }
} }
@ -33,7 +33,7 @@ func BootstrapEnvironment(ctx context.Context, environ *Environment, userConfig
} }
if userConfig.Persistence != nil { 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") return errors.Wrap(err, "persistence configure error")
} }
} }

View File

@ -13,7 +13,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/codingconcepts/env"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pquerna/otp" "github.com/pquerna/otp"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -276,31 +275,6 @@ func (environ *Environment) Start(ctx context.Context) (err error) {
return 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 { func (environ *Environment) SetStartTime(t time.Time) *Environment {
environ.startTime = t environ.startTime = t
return environ return environ

View File

@ -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 { func GetIsolationFromContext(ctx context.Context) *Isolation {
isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation) isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation)
if ok { if ok {

View File

@ -2,8 +2,10 @@ package bbgo
import ( import (
"context" "context"
"os"
"reflect" "reflect"
"github.com/codingconcepts/env"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/dynamic" "github.com/c9s/bbgo/pkg/dynamic"
@ -70,3 +72,28 @@ func storePersistenceFields(obj interface{}, id string, persistence service.Pers
return store.Save(inf) 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
}