From e25c68e9efb9c9b4c691338f55ee3d8b9952bf82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Ramon=20Ma=C3=B1es?= Date: Thu, 9 Nov 2023 12:40:07 +0100 Subject: [PATCH] fix(torch): refactor extract to func - priv func MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jose Ramon MaƱes --- pkg/http/server.go | 44 ++++++++++++++++++++++++------------------ pkg/metrics/metrics.go | 6 +++--- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/pkg/http/server.go b/pkg/http/server.go index 2fd134c..ab53b57 100644 --- a/pkg/http/server.go +++ b/pkg/http/server.go @@ -78,25 +78,7 @@ func Run(cfg config.MutualPeersConfig) { log.Info("Server Started...") log.Info("Listening on port: " + httpPort) - // Check if the config has the consensusNode field defined to generate the metric from the Genesis Hash data. - if cfg.MutualPeers[0].ConsensusNode != "" { - // Initialise the goroutine to generate the metric in the background, only if we specify the node in the config. - go func() { - log.Info("Consensus node defined to get the first block") - for { - err := GenerateHashMetrics(cfg) - // check if err is nil, if so, that means that Torch was able to generate the metric. - if err == nil { - log.Info("Metric generated for the first block...") - // The metric was successfully generated, stop the retries. - break - } - - // Wait for the retry interval before the next execution - time.Sleep(retryInterval) - } - }() - } + BackgroundGenerateHashMetric(cfg) // Initialize the goroutine to check the nodes in the queue. log.Info("Initializing queues to process the nodes...") @@ -144,6 +126,30 @@ func Run(cfg config.MutualPeersConfig) { log.Info("Server Exited Properly") } +// BackgroundGenerateHashMetric check if we have defined the consensus in the config, if so, it creates a goroutine +// to generate the metric +func BackgroundGenerateHashMetric(cfg config.MutualPeersConfig) { + // Check if the config has the consensusNode field defined to generate the metric from the Genesis Hash data. + if cfg.MutualPeers[0].ConsensusNode != "" { + // Initialise the goroutine to generate the metric in the background, only if we specify the node in the config. + go func() { + log.Info("Consensus node defined to get the first block") + for { + err := GenerateHashMetrics(cfg) + // check if err is nil, if so, that means that Torch was able to generate the metric. + if err == nil { + log.Info("Metric generated for the first block...") + // The metric was successfully generated, stop the retries. + break + } + + // Wait for the retry interval before the next execution + time.Sleep(retryInterval) + } + }() + } +} + // GenerateHashMetrics generates the metric by getting the first block and calculating the days. func GenerateHashMetrics(cfg config.MutualPeersConfig) error { log.Info("Generating the metric for the first block generated...") diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 409b4e0..4bd0826 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -79,7 +79,7 @@ func WithMetricsBlockHeight(blockHeight, earliestBlockTime, serviceName, namespa } // Calculate the days that the chain is live. - daysRunning, err := CalculateDaysDifference(earliestBlockTime) + daysRunning, err := calculateDaysDifference(earliestBlockTime) if err != nil { log.Error("ERROR: ", err) return err @@ -106,8 +106,8 @@ func WithMetricsBlockHeight(blockHeight, earliestBlockTime, serviceName, namespa return err } -// CalculateDaysDifference based on the date received, returns the number of days since this day. -func CalculateDaysDifference(inputTimeString string) (int, error) { +// calculateDaysDifference based on the date received, returns the number of days since this day. +func calculateDaysDifference(inputTimeString string) (int, error) { layout := "2006-01-02T15:04:05.999999999Z" inputTime, err := time.Parse(layout, inputTimeString) if err != nil {