Skip to content

Commit

Permalink
Merge pull request #30 from NodeFactoryIo/mpetrun5/retry-fetching-con…
Browse files Browse the repository at this point in the history
…fig-hash

Retry fetching config hash if it fails
  • Loading branch information
mpetrunic committed Sep 24, 2020
2 parents eb3cb08 + ea58582 commit 2d4290c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [unreleased](https://github.com/NodeFactoryIo/vedran-daemon/tree/HEAD)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.1.1...v0.1.2)

### Fixed
- Retry config hash retrieval on fail [\#30](https://github.com/NodeFactoryIo/vedran-daemon/pull/30) ([mpetrun5](https://github.com/mpetrun5))

### Added

### Changed

## [v0.1.1](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.1.1)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.1.0...v0.1.1)
Expand Down
18 changes: 14 additions & 4 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package run

import (
"encoding/base64"
"hash"
"time"

"github.com/NodeFactoryIo/vedran-daemon/internal/lb"
Expand All @@ -11,14 +12,23 @@ import (
log "github.com/sirupsen/logrus"
)

var sleep = time.Sleep

// Start registers to load balancer and starts sending telemetry
func Start(lbClient *lb.Client, nodeClient node.Client, telemetry telemetry.Telemetry, id string, payoutAddress string) error {
configHash, err := nodeClient.GetConfigHash()
if err != nil {
return err
var configHash hash.Hash32
for {
var err error
configHash, err = nodeClient.GetConfigHash()
if err == nil {
break
}

log.Errorf("Failed retrieving node metrics because of %v. Retrying in 5 seconds...", err)
sleep(time.Second * 5)
}

err = lbClient.Register(id, nodeClient.GetRPCURL(), payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]))
err := lbClient.Register(id, nodeClient.GetRPCURL(), payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]))
if err != nil {
return err
}
Expand Down
34 changes: 17 additions & 17 deletions internal/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/NodeFactoryIo/vedran-daemon/internal/lb"
nodeMocks "github.com/NodeFactoryIo/vedran-daemon/mocks/node"
Expand All @@ -33,6 +34,8 @@ func teardown() {
}

func TestStart(t *testing.T) {
sleep = func(d time.Duration) {}

setup()
defer teardown()

Expand All @@ -53,36 +56,31 @@ func TestStart(t *testing.T) {
wantErr bool
handleFunc handleFnMock
startSendingTelemetryResult error
getConfigHashResult hash.Hash32
getConfigHashError error
firstGetConfigHashResult hash.Hash32
firstGetConfigHashError error
secondGetConfigHashResult hash.Hash32
secondGetConfigHashError error
}{
{
name: "Returns error if get config hash fails",
args: args{lbClient, "test-id", "0xtestpayoutaddress"},
wantErr: true,
handleFunc: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
},
getConfigHashError: fmt.Errorf("Error"),
getConfigHashResult: nil},
{
name: "Returns error if lb register fails",
name: "Retries get config hash if get config hash fails and returns error if register fails",
args: args{lbClient, "test-id", "0xtestpayoutaddress"},
wantErr: true,
handleFunc: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
},
getConfigHashError: nil,
getConfigHashResult: testHash},
firstGetConfigHashError: fmt.Errorf("Error"),
firstGetConfigHashResult: nil,
secondGetConfigHashError: nil,
secondGetConfigHashResult: testHash},
{
name: "Returns nil if startSendingTelemetry succeeds",
args: args{lbClient, "test-id", "0xtestpayoutaddress"},
wantErr: false,
handleFunc: func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"token": "test-token"}`)
},
getConfigHashError: nil,
getConfigHashResult: testHash},
firstGetConfigHashError: nil,
firstGetConfigHashResult: testHash},
}

for _, tt := range tests {
Expand All @@ -92,7 +90,9 @@ func TestStart(t *testing.T) {
telemetryMock := &telemetryMocks.Telemetry{}
telemetryMock.On("StartSendingTelemetry", mock.Anything, mock.Anything, mock.Anything).Return()
nodeClient.On("GetRPCURL").Return("http://localhost:9933")
nodeClient.On("GetConfigHash").Once().Return(tt.getConfigHashResult, tt.getConfigHashError)
nodeClient.On("GetConfigHash").Once().Return(tt.firstGetConfigHashResult, tt.firstGetConfigHashError)
nodeClient.On("GetConfigHash").Once().Return(tt.secondGetConfigHashResult, tt.secondGetConfigHashError)

url, _ := url.Parse(server.URL)
lbClient.BaseURL = url
mux.HandleFunc("/api/v1/nodes", tt.handleFunc)
Expand Down

0 comments on commit 2d4290c

Please sign in to comment.