Refactor main.go to improve startup execution flow (#334)

Much easier to work on a project when you can clearly see its execution path.
This commit is contained in:
Luke Hamilton
2019-08-06 15:20:29 +10:00
committed by Adrian Gallagher
parent 20a5ff256e
commit 6e70f0642a
2 changed files with 57 additions and 43 deletions

View File

@@ -255,4 +255,7 @@ func SetupExchanges() {
)
}
wg.Wait()
if len(bot.exchanges) == 0 {
log.Fatalf("No exchanges were able to be loaded. Exiting")
}
}

97
main.go
View File

@@ -109,39 +109,10 @@ func main() {
log.Errorf("Failed to setup logger reason: %s", err)
}
if bot.config.NTPClient.Level != -1 {
bot.config.CheckNTPConfig()
NTPTime, errNTP := ntpclient.NTPClient(bot.config.NTPClient.Pool)
currentTime := time.Now()
if errNTP != nil {
log.Warnf("NTPClient failed to create: %v", errNTP)
} else {
NTPcurrentTimeDifference := NTPTime.Sub(currentTime)
configNTPTime := *bot.config.NTPClient.AllowedDifference
configNTPNegativeTime := (*bot.config.NTPClient.AllowedNegativeDifference - (*bot.config.NTPClient.AllowedNegativeDifference * 2))
if NTPcurrentTimeDifference > configNTPTime || NTPcurrentTimeDifference < configNTPNegativeTime {
log.Warnf("Time out of sync (NTP): %v | (time.Now()): %v | (Difference): %v | (Allowed): +%v / %v", NTPTime, currentTime, NTPcurrentTimeDifference, configNTPTime, configNTPNegativeTime)
if bot.config.NTPClient.Level == 0 {
disable, errNTP := bot.config.DisableNTPCheck(os.Stdin)
if errNTP != nil {
log.Errorf("failed to disable ntp time check reason: %v", errNTP)
} else {
log.Info(disable)
}
}
}
}
}
// Sets up internet connectivity monitor
bot.connectivity, err = connchecker.New(bot.config.ConnectionMonitor.DNSList,
bot.config.ConnectionMonitor.PublicDomainList,
bot.config.ConnectionMonitor.CheckInterval)
if err != nil {
log.Fatalf("Connectivity checker failure: %s", err)
}
ActivateNTP()
ActivateConnectivityMonitor()
AdjustGoMaxProcs()
log.Debugf("Bot '%s' started.\n", bot.config.Name)
log.Debugf("Bot dry run mode: %v.\n", common.IsEnabled(bot.dryRun))
@@ -153,9 +124,6 @@ func main() {
log.Debugf("Global HTTP request timeout: %v.\n", common.HTTPClient.Timeout)
SetupExchanges()
if len(bot.exchanges) == 0 {
log.Fatalf("No exchanges were able to be loaded. Exiting")
}
log.Debugf("Starting communication mediums..")
cfg := bot.config.GetCommunicationsConfig()
@@ -193,6 +161,20 @@ func main() {
bot.portfolio.SeedPortfolio(bot.config.Portfolio)
SeedExchangeAccountInfo(GetAllEnabledExchangeAccountInfo().Data)
ActivateWebServer()
go portfolio.StartPortfolioWatcher()
go TickerUpdaterRoutine()
go OrderbookUpdaterRoutine()
go WebsocketRoutine(*verbosity)
<-bot.shutdown
Shutdown()
}
// ActivateWebServer Sets up a local web server
func ActivateWebServer() {
if bot.config.Webserver.Enabled {
listenAddr := bot.config.Webserver.ListenAddress
log.Debugf(
@@ -202,7 +184,7 @@ func main() {
router := NewRouter()
go func() {
err = http.ListenAndServe(listenAddr, router)
err := http.ListenAndServe(listenAddr, router)
if err != nil {
log.Fatal(err)
}
@@ -214,15 +196,44 @@ func main() {
} else {
log.Debugln("HTTP RESTful Webserver support disabled.")
}
}
go portfolio.StartPortfolioWatcher()
// ActivateConnectivityMonitor Sets up internet connectivity monitor
func ActivateConnectivityMonitor() {
var err error
bot.connectivity, err = connchecker.New(bot.config.ConnectionMonitor.DNSList,
bot.config.ConnectionMonitor.PublicDomainList,
bot.config.ConnectionMonitor.CheckInterval)
if err != nil {
log.Fatalf("Connectivity checker failure: %s", err)
}
}
go TickerUpdaterRoutine()
go OrderbookUpdaterRoutine()
go WebsocketRoutine(*verbosity)
<-bot.shutdown
Shutdown()
// ActivateNTP Sets up NTP client
func ActivateNTP() {
if bot.config.NTPClient.Level != -1 {
bot.config.CheckNTPConfig()
NTPTime, errNTP := ntpclient.NTPClient(bot.config.NTPClient.Pool)
currentTime := time.Now()
if errNTP != nil {
log.Warnf("NTPClient failed to create: %v", errNTP)
} else {
NTPcurrentTimeDifference := NTPTime.Sub(currentTime)
configNTPTime := *bot.config.NTPClient.AllowedDifference
configNTPNegativeTime := (*bot.config.NTPClient.AllowedNegativeDifference - (*bot.config.NTPClient.AllowedNegativeDifference * 2))
if NTPcurrentTimeDifference > configNTPTime || NTPcurrentTimeDifference < configNTPNegativeTime {
log.Warnf("Time out of sync (NTP): %v | (time.Now()): %v | (Difference): %v | (Allowed): +%v / %v", NTPTime, currentTime, NTPcurrentTimeDifference, configNTPTime, configNTPNegativeTime)
if bot.config.NTPClient.Level == 0 {
disable, errNTP := bot.config.DisableNTPCheck(os.Stdin)
if errNTP != nil {
log.Errorf("failed to disable ntp time check reason: %v", errNTP)
} else {
log.Info(disable)
}
}
}
}
}
}
// AdjustGoMaxProcs adjusts the maximum processes that the CPU can handle.