bbgo_origin/pkg/bbgo/persistence.go

123 lines
3.4 KiB
Go
Raw Permalink Normal View History

package bbgo
import (
2022-10-03 10:45:24 +00:00
"context"
"os"
"reflect"
"sync"
"github.com/codingconcepts/env"
2022-10-05 10:46:26 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
2022-03-14 13:21:04 +00:00
"github.com/c9s/bbgo/pkg/dynamic"
2022-03-14 13:21:04 +00:00
"github.com/c9s/bbgo/pkg/service"
)
2020-12-07 03:43:17 +00:00
var defaultPersistenceServiceFacade = &service.PersistenceServiceFacade{
Memory: service.NewMemoryService(),
}
// Sync syncs the object properties into the persistence layer
2022-10-03 10:45:24 +00:00
func Sync(ctx context.Context, obj interface{}) {
2022-08-22 18:12:26 +00:00
id := dynamic.CallID(obj)
if len(id) == 0 {
2022-06-21 07:57:26 +00:00
log.Warnf("InstanceID() is not provided, can not sync persistence")
return
}
isolation := GetIsolationFromContext(ctx)
ps := isolation.persistenceServiceFacade.Get()
locker, ok := obj.(sync.Locker)
if ok {
locker.Lock()
defer locker.Unlock()
}
2022-06-21 07:57:26 +00:00
err := storePersistenceFields(obj, id, ps)
if err != nil {
log.WithError(err).Errorf("persistence sync failed")
}
2022-05-05 10:02:08 +00:00
}
2022-05-05 05:05:01 +00:00
func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return dynamic.IterateFieldsByTag(obj, "persistence", true, func(tag string, field reflect.StructField, value reflect.Value) error {
log.Debugf("[loadPersistenceFields] loading value into field %v, tag = %s, original value = %v", field, tag, value)
newValueInf := dynamic.NewTypeValueInterface(value.Type())
// inf := value.Interface()
2022-05-05 09:56:41 +00:00
store := persistence.NewStore("state", id, tag)
if err := store.Load(&newValueInf); err != nil {
if err == service.ErrPersistenceNotExists {
log.Debugf("[loadPersistenceFields] state key does not exist, id = %v, tag = %s", id, tag)
return nil
}
return err
}
newValue := reflect.ValueOf(newValueInf)
if value.Kind() != reflect.Ptr && newValue.Kind() == reflect.Ptr {
newValue = newValue.Elem()
}
log.Debugf("[loadPersistenceFields] %v = %v -> %v\n", field, value, newValue)
value.Set(newValue)
return nil
})
}
2022-05-05 05:05:01 +00:00
func storePersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {
return dynamic.IterateFieldsByTag(obj, "persistence", true, func(tag string, ft reflect.StructField, fv reflect.Value) error {
log.Debugf("[storePersistenceFields] storing value from field %v, tag = %s, original value = %v", ft, tag, fv)
inf := fv.Interface()
2022-05-05 09:56:41 +00:00
store := persistence.NewStore("state", id, tag)
return store.Save(inf)
})
}
2022-10-05 10:46:26 +00:00
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 {
2022-10-05 10:46:26 +00:00
return nil, err
}
redisPersistence := service.NewRedisPersistenceService(conf.Redis)
2022-10-05 10:46:26 +00:00
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 {
2022-10-05 10:46:26 +00:00
return nil, errors.Wrapf(err2, "can not create directory: %s", conf.Json.Directory)
}
}
jsonPersistence := &service.JsonPersistenceService{Directory: conf.Json.Directory}
2022-10-05 10:46:26 +00:00
facade.Json = jsonPersistence
}
return facade, nil
}
func ConfigurePersistence(ctx context.Context, environ *Environment, conf *PersistenceConfig) error {
2022-10-05 10:46:26 +00:00
facade, err := NewPersistenceServiceFacade(conf)
if err != nil {
return err
}
isolation := GetIsolationFromContext(ctx)
isolation.persistenceServiceFacade = facade
environ.PersistentService = facade
return nil
}