call bbgo.Sync to sync persistence

This commit is contained in:
c9s 2022-06-21 15:57:26 +08:00
parent 5b239ad8ee
commit 6ef54bf2fb
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 16 additions and 12 deletions

View File

@ -91,14 +91,18 @@ func (p *Persistence) Sync(obj interface{}) error {
}
// Sync syncs the object properties into the persistence layer
func Sync(obj interface{}) error {
func Sync(obj interface{}) {
id := callID(obj)
if len(id) == 0 {
return nil
log.Warnf("InstanceID() is not provided, can not sync persistence")
return
}
ps := PersistenceServiceFacade.Get()
return storePersistenceFields(obj, id, ps)
err := storePersistenceFields(obj, id, ps)
if err != nil {
log.WithError(err).Errorf("persistence sync failed")
}
}
func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error {

View File

@ -494,7 +494,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.OnSuspend(func() {
s.Status = types.StrategyStatusStopped
_ = s.orderExecutor.GracefulCancel(ctx)
_ = s.Persistence.Sync(s)
bbgo.Sync(s)
})
s.OnEmergencyStop(func() {
@ -578,9 +578,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// TODO: migrate persistance to singleton
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
if err := s.Persistence.Sync(s); err != nil {
log.WithError(err).Errorf("can not sync state to persistence")
}
bbgo.Sync(s)
})
s.SmartStops.RunStopControllers(ctx, session, s.orderExecutor.TradeCollector())

View File

@ -231,6 +231,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats)
s.orderExecutor.BindTradeStats(s.TradeStats)
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
bbgo.Sync(s)
})
s.orderExecutor.Bind()
store, _ := session.MarketDataStore(s.Symbol)

View File

@ -3,9 +3,10 @@ package supertrend
import (
"context"
"fmt"
"github.com/c9s/bbgo/pkg/util"
"sync"
"github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -256,9 +257,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Sync position to redis on trade
s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
if err := s.Persistence.Sync(s); err != nil {
log.WithError(err).Errorf("can not sync state to persistence")
}
bbgo.Sync(s)
})
s.stopC = make(chan struct{})

View File

@ -352,7 +352,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.OnSuspend(func() {
// Cancel all order
_ = s.orderExecutor.GracefulCancel(ctx)
_ = s.Persistence.Sync(s)
bbgo.Sync(s)
})
s.OnEmergencyStop(func() {