bbgo: call global persistence facade to sync data

This commit is contained in:
c9s 2022-06-21 14:32:43 +08:00
parent 612df45c5e
commit 7c9ad535fd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 17 additions and 9 deletions

View File

@ -26,24 +26,22 @@ var PersistenceServiceFacade = DefaultPersistenceServiceFacade
// Persistence is used for strategy to inject the persistence.
type Persistence struct {
PersistenceSelector *PersistenceSelector `json:"persistence,omitempty" yaml:"persistence,omitempty"`
Facade *service.PersistenceServiceFacade `json:"-" yaml:"-"`
}
func (p *Persistence) backendService(t string) (service.PersistenceService, error) {
switch t {
case "json":
return p.Facade.Json, nil
return PersistenceServiceFacade.Json, nil
case "redis":
if p.Facade.Redis == nil {
if PersistenceServiceFacade.Redis == nil {
log.Warn("redis persistence is not available, fallback to memory backend")
return p.Facade.Memory, nil
return PersistenceServiceFacade.Memory, nil
}
return p.Facade.Redis, nil
return PersistenceServiceFacade.Redis, nil
case "memory":
return p.Facade.Memory, nil
return PersistenceServiceFacade.Memory, nil
}
@ -88,7 +86,18 @@ func (p *Persistence) Sync(obj interface{}) error {
return nil
}
ps := p.Facade.Get()
ps := PersistenceServiceFacade.Get()
return storePersistenceFields(obj, id, ps)
}
// Sync syncs the object properties into the persistence layer
func Sync(obj interface{}) error {
id := callID(obj)
if len(id) == 0 {
return nil
}
ps := PersistenceServiceFacade.Get()
return storePersistenceFields(obj, id, ps)
}

View File

@ -389,7 +389,6 @@ var defaultPersistenceSelector = &PersistenceSelector{
func (trader *Trader) injectCommonServices(s interface{}) error {
persistence := &Persistence{
PersistenceSelector: defaultPersistenceSelector,
Facade: PersistenceServiceFacade,
}
// a special injection for persistence selector: