From 724dad70bb0db7de79c392c520ac98aa1c76c8e9 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 22 Feb 2021 14:14:39 +0800 Subject: [PATCH] remove trade sync from environ init --- cmd/bbgo-webview/main.go | 4 +++- frontend/pages/_app.tsx | 20 ++++++++++++++++---- pkg/bbgo/environment.go | 30 ++++++++++++++++++++---------- pkg/bbgo/session.go | 5 ----- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/cmd/bbgo-webview/main.go b/cmd/bbgo-webview/main.go index ec0b068c8..c3b1f27d7 100644 --- a/cmd/bbgo-webview/main.go +++ b/cmd/bbgo-webview/main.go @@ -22,7 +22,9 @@ import ( func main() { noChangeDir := false + portNum := 0 flag.BoolVar(&noChangeDir, "no-chdir", false, "do not change directory") + flag.IntVar(&portNum, "port", 0, "server port") flag.Parse() if !noChangeDir { @@ -116,7 +118,7 @@ func main() { } // find a free port for binding the server - ln, err := net.Listen("tcp", "127.0.0.1:0") + ln, err := net.Listen("tcp", "127.0.0.1:" + strconv.Itoa(portNum)) if err != nil { log.WithError(err).Error("can not bind listener") return diff --git a/frontend/pages/_app.tsx b/frontend/pages/_app.tsx index 541f69e73..f1a4b0cc5 100644 --- a/frontend/pages/_app.tsx +++ b/frontend/pages/_app.tsx @@ -15,6 +15,11 @@ import CssBaseline from '@material-ui/core/CssBaseline'; import theme from '../src/theme'; import '../styles/globals.css' import {querySessions, querySyncStatus} from "../api/bbgo"; +import {Sync} from "@material-ui/icons"; + +const SyncNotStarted = 0 +const Syncing = 1 +const SyncDone = 2 export default function MyApp(props) { const {Component, pageProps} = props; @@ -37,10 +42,17 @@ export default function MyApp(props) { let poller = null const pollSyncStatus = () => { querySyncStatus((status) => { - if (status === false) { - setLoading(false) - setSyncing(false) - clearInterval(poller) + switch (status) { + case SyncNotStarted: + break + case Syncing: + setSyncing(true); + break; + case SyncDone: + setLoading(false); + setSyncing(false); + clearInterval(poller); + break; } }).catch((err) => { console.error(err) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 6a7f9ddff..a6d804425 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -50,6 +50,15 @@ func RegisterStrategy(key string, s interface{}) { var emptyTime time.Time + +type SyncStatus int + +const ( + SyncNotStarted SyncStatus = iota + Syncing + SyncDone +) + // Environment presents the real exchange data layer type Environment struct { // Notifiability here for environment is for the streaming data notification @@ -70,7 +79,7 @@ type Environment struct { syncMutex sync.Mutex syncStatusMutex sync.Mutex - syncing bool + syncStatus SyncStatus sessions map[string]*ExchangeSession } @@ -478,15 +487,16 @@ func (environ *Environment) Connect(ctx context.Context) error { return nil } -func (environ *Environment) IsSyncing() bool { +func (environ *Environment) IsSyncing() (status SyncStatus) { environ.syncStatusMutex.Lock() - defer environ.syncStatusMutex.Unlock() - return environ.syncing + status = environ.syncStatus + environ.syncStatusMutex.Unlock() + return status } -func (environ *Environment) setSyncing(syncing bool) { +func (environ *Environment) setSyncing(status SyncStatus) { environ.syncStatusMutex.Lock() - environ.syncing = syncing + environ.syncStatus = status environ.syncStatusMutex.Unlock() } @@ -495,8 +505,8 @@ func (environ *Environment) Sync(ctx context.Context) error { environ.syncMutex.Lock() defer environ.syncMutex.Unlock() - environ.setSyncing(true) - defer environ.setSyncing(false) + environ.setSyncing(Syncing) + defer environ.setSyncing(SyncDone) for _, session := range environ.sessions { if err := environ.syncSession(ctx, session); err != nil { @@ -511,8 +521,8 @@ func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSe environ.syncMutex.Lock() defer environ.syncMutex.Unlock() - environ.setSyncing(true) - defer environ.setSyncing(false) + environ.setSyncing(Syncing) + defer environ.setSyncing(SyncDone) return environ.syncSession(ctx, session, defaultSymbols...) } diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index be6c8d728..a7477a8b1 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -292,11 +292,6 @@ func (session *ExchangeSession) InitSymbol(ctx context.Context, environ *Environ var err error var trades []types.Trade if environ.TradeSync != nil { - log.Infof("syncing trades from %s for symbol %s...", session.Exchange.Name(), symbol) - if err := environ.TradeSync.SyncTrades(ctx, session.Exchange, symbol); err != nil { - return err - } - tradingFeeCurrency := session.Exchange.PlatformFeeCurrency() if strings.HasPrefix(symbol, tradingFeeCurrency) { trades, err = environ.TradeService.QueryForTradingFeeCurrency(session.Exchange.Name(), symbol, tradingFeeCurrency)