split environment start and init

This commit is contained in:
c9s 2021-05-08 00:45:24 +08:00
parent 14eedecd0e
commit 6bbd66a4f9
3 changed files with 16 additions and 13 deletions

View File

@ -198,6 +198,7 @@ func (e Exchange) QueryKLines(ctx context.Context, symbol string, interval types
if options.EndTime != nil {
return e.srv.QueryKLinesBackward(e.sourceName, symbol, interval, *options.EndTime)
}
if options.StartTime != nil {
return e.srv.QueryKLinesForward(e.sourceName, symbol, interval, *options.StartTime)
}

View File

@ -274,27 +274,33 @@ func (environ *Environment) AddExchangesFromSessionConfig(sessions map[string]*E
return nil
}
// Init prepares the data that will be used by the strategies
func (environ *Environment) Init(ctx context.Context) (err error) {
for n := range environ.sessions {
var session = environ.sessions[n]
if err := session.Init(ctx, environ); err != nil {
if err = session.Init(ctx, environ); err != nil {
// we can skip initialized sessions
if err != ErrSessionAlreadyInitialized {
return err
}
}
if err := session.InitSymbols(ctx, environ); err != nil {
return err
}
}
return nil
return
}
func (environ *Environment) Start(ctx context.Context) (err error) {
for n := range environ.sessions {
var session = environ.sessions[n]
if err = session.InitSymbols(ctx, environ); err != nil {
return err
}
}
return
}
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
if conf.Redis != nil {
if err := env.Set(conf.Redis); err != nil {
@ -552,10 +558,6 @@ func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSe
}
func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error {
if err := session.Init(ctx, environ); err != nil {
return err
}
symbols, err := getSessionSymbols(session, defaultSymbols...)
if err != nil {
return err

View File

@ -295,7 +295,7 @@ func (trader *Trader) RunAllSingleExchangeStrategy(ctx context.Context) error {
func (trader *Trader) Run(ctx context.Context) error {
trader.Subscribe()
if err := trader.environment.Init(ctx); err != nil {
if err := trader.environment.Start(ctx); err != nil {
return err
}