Skip to content

Commit

Permalink
Refactor main.go to improve startup execution flow (thrasher-corp#334)
Browse files Browse the repository at this point in the history
Much easier to work on a project when you can clearly see its execution path.
  • Loading branch information
Luke Hamilton authored and thrasher- committed Aug 6, 2019
1 parent 20a5ff2 commit 6e70f06
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
3 changes: 3 additions & 0 deletions exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,7 @@ func SetupExchanges() {
)
}
wg.Wait()
if len(bot.exchanges) == 0 {
log.Fatalf("No exchanges were able to be loaded. Exiting")
}
}
97 changes: 54 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand All @@ -214,15 +196,44 @@ func main() {
} else {
log.Debugln("HTTP RESTful Webserver support disabled.")
}
}

go portfolio.StartPortfolioWatcher()

go TickerUpdaterRoutine()
go OrderbookUpdaterRoutine()
go WebsocketRoutine(*verbosity)
// 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)
}
}

<-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.
Expand Down

0 comments on commit 6e70f06

Please sign in to comment.