Skip to content

Commit

Permalink
remove trade sync from environ init
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 22, 2021
1 parent ed99d5c commit 4e8e86c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
4 changes: 3 additions & 1 deletion cmd/bbgo-webview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
30 changes: 20 additions & 10 deletions pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,7 +79,7 @@ type Environment struct {
syncMutex sync.Mutex

syncStatusMutex sync.Mutex
syncing bool
syncStatus SyncStatus

sessions map[string]*ExchangeSession
}
Expand Down Expand Up @@ -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()
}

Expand All @@ -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 {
Expand All @@ -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...)
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/bbgo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4e8e86c

Please sign in to comment.