From a8516edb98436ff22f376211407723b8cfca9d69 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 21 Feb 2021 16:55:45 +0800 Subject: [PATCH] add Get method to the persistence service facade --- pkg/bbgo/environment.go | 7 +------ pkg/service/persistence_facade.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 7ab10b72f..28b7df21f 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -536,12 +536,6 @@ func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]st } func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) error { - // Configure persistence service, by default we will use memory service - var persistence service.PersistenceService = environ.PersistenceServiceFacade.Memory - if environ.PersistenceServiceFacade.Redis != nil { - persistence = environ.PersistenceServiceFacade.Redis - } - environ.Notifiability = Notifiability{ SymbolChannelRouter: NewPatternChannelRouter(nil), SessionChannelRouter: NewPatternChannelRouter(nil), @@ -562,6 +556,7 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro } } + persistence := environ.PersistenceServiceFacade.Get() telegramBotToken := viper.GetString("telegram-bot-token") if len(telegramBotToken) > 0 { tt := strings.Split(telegramBotToken, ":") diff --git a/pkg/service/persistence_facade.go b/pkg/service/persistence_facade.go index cc8f8b2f1..2adce0eb2 100644 --- a/pkg/service/persistence_facade.go +++ b/pkg/service/persistence_facade.go @@ -5,3 +5,17 @@ type PersistenceServiceFacade struct { Json *JsonPersistenceService Memory *MemoryService } + +// Get returns the preferred persistence service by fallbacks +// Redis will be preferred at the first position. +func (facade *PersistenceServiceFacade) Get() PersistenceService { + if facade.Redis != nil { + return facade.Redis + } + + if facade.Json != nil { + return facade.Json + } + + return facade.Memory +}