From 20a5ff256e306ad0c205718559d5cb54d167e192 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 6 Aug 2019 15:19:57 +1000 Subject: [PATCH 1/2] Update codelingo.yaml (#335) * Update codelingo.yaml * Import comment-first-word-when-empty again --- codelingo.yaml | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/codelingo.yaml b/codelingo.yaml index 20a7931f..4e839223 100644 --- a/codelingo.yaml +++ b/codelingo.yaml @@ -8,33 +8,9 @@ tenets: - import: codelingo/effective-go/unnecessary-else - import: codelingo/code-review-comments/declare-empty-slice - import: codelingo/effective-go/defer-close-file - # Overwrite one tenet with custom logic - # - import: codelingo/effective-go/comment-first-word-when-empty - - name: comment-first-word-when-empty - flows: - codelingo/review: - comment: | - Every exported function in a program should have a doc comment. The first sentence should be a summary that starts with the name ({{funcName}}) being declared. - From [effective go](https://golang.org/doc/effective_go.html#commentary). - codelingo/rewrite: - place: holder - query: | - import codelingo/ast/go - @review comment - @rewrite --prepend --line "// {{funcName}} is a function." - go.func_decl(depth = any): - # Customisation to exclude test packages - exclude: - go.ident: - name as funcname - regex(/_test/, funcname) - exclude: - go.comment_group - go.ident: - name as funcName - public == "true" + - import: codelingo/effective-go/comment-first-word-when-empty - name: missing-stop-ticker - flows: + actions: codelingo/review: comment: Add `defer {{varName}}.Stop()` to stop the ticker and release associated resources. query: | From 6e70f0642a6523a4fb6c6da3a3e8da8402911553 Mon Sep 17 00:00:00 2001 From: Luke Hamilton Date: Tue, 6 Aug 2019 15:20:29 +1000 Subject: [PATCH 2/2] Refactor main.go to improve startup execution flow (#334) Much easier to work on a project when you can clearly see its execution path. --- exchange.go | 3 ++ main.go | 97 +++++++++++++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/exchange.go b/exchange.go index 6d34a38f..b0bc7b56 100644 --- a/exchange.go +++ b/exchange.go @@ -255,4 +255,7 @@ func SetupExchanges() { ) } wg.Wait() + if len(bot.exchanges) == 0 { + log.Fatalf("No exchanges were able to be loaded. Exiting") + } } diff --git a/main.go b/main.go index 8b43d399..323cfdd7 100644 --- a/main.go +++ b/main.go @@ -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.