Skip to content

Commit

Permalink
Merge pull request #982 from c9s/refactor/isolation
Browse files Browse the repository at this point in the history
refactor isolation context for persistence facade configuration
  • Loading branch information
c9s authored Oct 5, 2022
2 parents 4c0fdee + e922191 commit 39247bb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/bbgo/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand All @@ -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")
}
}
Expand Down
26 changes: 0 additions & 26 deletions pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"time"

"github.com/codingconcepts/env"
"github.com/pkg/errors"
"github.com/pquerna/otp"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/bbgo/isolation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions pkg/bbgo/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 39247bb

Please sign in to comment.