Skip to content

Commit

Permalink
fix(torch): refactor extract to func - priv func
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 committed Nov 9, 2023
1 parent ecde58f commit e25c68e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
44 changes: 25 additions & 19 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down Expand Up @@ -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...")
Expand Down
6 changes: 3 additions & 3 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit e25c68e

Please sign in to comment.