mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
Merge pull request #982 from c9s/refactor/isolation
refactor isolation context for persistence facade configuration
This commit is contained in:
commit
39247bb9d8
|
@ -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(ctx, 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(ctx, userConfig.Persistence); err != nil {
|
||||
return errors.Wrap(err, "persistence configure error")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -2,8 +2,11 @@ package bbgo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/codingconcepts/env"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/dynamic"
|
||||
|
@ -70,3 +73,44 @@ func storePersistenceFields(obj interface{}, id string, persistence service.Pers
|
|||
return store.Save(inf)
|
||||
})
|
||||
}
|
||||
|
||||
func NewPersistenceServiceFacade(conf *PersistenceConfig) (*service.PersistenceServiceFacade, error) {
|
||||
facade := &service.PersistenceServiceFacade{
|
||||
Memory: service.NewMemoryService(),
|
||||
}
|
||||
|
||||
if conf.Redis != nil {
|
||||
if err := env.Set(conf.Redis); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
redisPersistence := service.NewRedisPersistenceService(conf.Redis)
|
||||
facade.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 {
|
||||
return nil, errors.Wrapf(err2, "can not create directory: %s", conf.Json.Directory)
|
||||
}
|
||||
}
|
||||
|
||||
jsonPersistence := &service.JsonPersistenceService{Directory: conf.Json.Directory}
|
||||
facade.Json = jsonPersistence
|
||||
}
|
||||
|
||||
return facade, nil
|
||||
}
|
||||
|
||||
func ConfigurePersistence(ctx context.Context, conf *PersistenceConfig) error {
|
||||
facade, err := NewPersistenceServiceFacade(conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isolation := GetIsolationFromContext(ctx)
|
||||
isolation.persistenceServiceFacade = facade
|
||||
|
||||
persistenceServiceFacade = facade
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user