fix: pull out time.now variable

This commit is contained in:
c9s 2022-06-02 21:27:28 +08:00
parent 32095e2741
commit a7bd9239f2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 16 additions and 19 deletions

View File

@ -662,6 +662,7 @@ func sync(ctx context.Context, userConfig *bbgo.Config, backtestService *service
supportIntervals = types.SupportedIntervals supportIntervals = types.SupportedIntervals
} }
now := time.Now()
for interval := range supportIntervals { for interval := range supportIntervals {
// if err := s.SyncKLineByInterval(ctx, exchange, symbol, interval, startTime, endTime); err != nil { // if err := s.SyncKLineByInterval(ctx, exchange, symbol, interval, startTime, endTime); err != nil {
// return err // return err
@ -674,11 +675,13 @@ func sync(ctx context.Context, userConfig *bbgo.Config, backtestService *service
// if we don't have klines before the start time endpoint, the back-test will fail. // if we don't have klines before the start time endpoint, the back-test will fail.
// because the last price will be missing. // because the last price will be missing.
if firstKLine != nil { if firstKLine != nil {
if err := backtestService.SyncExist(ctx, sourceExchange, symbol, syncFromTime, time.Now(), interval); err != nil { log.Debugf("found existing kline data using partial sync...")
if err := backtestService.SyncExist(ctx, sourceExchange, symbol, syncFromTime, now, interval); err != nil {
return err return err
} }
} else { } else {
if err := backtestService.Sync(ctx, sourceExchange, symbol, syncFromTime, time.Now(), interval); err != nil { log.Debugf("starting a fresh kline data sync...")
if err := backtestService.Sync(ctx, sourceExchange, symbol, syncFromTime, now, interval); err != nil {
return err return err
} }
} }

View File

@ -289,25 +289,14 @@ func (s *BacktestService) scanRows(rows *sqlx.Rows) (klines []types.KLine, err e
} }
func (s *BacktestService) _targetKlineTable(exchangeName types.ExchangeName) string { func (s *BacktestService) _targetKlineTable(exchangeName types.ExchangeName) string {
switch exchangeName { return strings.ToLower(exchangeName.String()) + "_klines"
case types.ExchangeBinance:
return "binance_klines"
case types.ExchangeFTX:
return "ftx_klines"
case types.ExchangeMax:
return "max_klines"
case types.ExchangeOKEx:
return "okex_klines"
case types.ExchangeKucoin:
return "kucoin_klines"
default:
return "klines"
}
} }
var errExchangeFieldIsUnset = errors.New("kline.Exchange field should not be empty")
func (s *BacktestService) Insert(kline types.KLine) error { func (s *BacktestService) Insert(kline types.KLine) error {
if len(kline.Exchange) == 0 { if len(kline.Exchange) == 0 {
return errors.New("kline.Exchange field should not be empty") return errExchangeFieldIsUnset
} }
tableName := s._targetKlineTable(kline.Exchange) tableName := s._targetKlineTable(kline.Exchange)

View File

@ -19,12 +19,16 @@ type SyncTask struct {
Type interface{} Type interface{}
// ID is a function that returns the unique identity of the object // ID is a function that returns the unique identity of the object
// This function will be used for detecting duplicated objects.
ID func(obj interface{}) string ID func(obj interface{}) string
// Time is a function that returns the time of the object // Time is a function that returns the time of the object
// This function will be used for sorting records
Time func(obj interface{}) time.Time Time func(obj interface{}) time.Time
// Select is the select query builder for querying db records // Select is the select query builder for querying existing db records
// The built SQL will be used for querying existing db records.
// And then the ID function will be used for filtering duplicated object.
Select squirrel.SelectBuilder Select squirrel.SelectBuilder
// OnLoad is an optional field, which is called when the records are loaded from the database // OnLoad is an optional field, which is called when the records are loaded from the database
@ -79,7 +83,8 @@ func (sel SyncTask) execute(ctx context.Context, db *sqlx.DB, startTime time.Tim
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil logrus.Warnf("context is cancelled, stop syncing")
return ctx.Err()
case err := <-errC: case err := <-errC:
return err return err