Skip to content

Commit

Permalink
Merge pull request #62 from NodeFactoryIo/mmuftic/add-ws-check
Browse files Browse the repository at this point in the history
Add ws endpoint check on start
  • Loading branch information
mpetrunic committed Jan 26, 2021
2 parents 1000367 + cf3ef93 commit 90d9487
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.3.2
version=0.3.3
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

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

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

### Fixed

### Added
- Add ws endpoint check on start [\#62](https://github.com/NodeFactoryIo/vedran-daemon/pull/62) ([MakMuftic](https://github.com/MakMuftic))

### Changed

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

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.3.1...v0.3.2)
Expand Down
16 changes: 10 additions & 6 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cmd

import (
"fmt"
"net/url"
"os"

"github.com/NodeFactoryIo/vedran-daemon/internal/lb"
"github.com/NodeFactoryIo/vedran-daemon/internal/node"
"github.com/NodeFactoryIo/vedran-daemon/internal/run"
Expand All @@ -13,6 +10,8 @@ import (
"github.com/NodeFactoryIo/vedran-daemon/pkg/logger"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
"os"
)

var (
Expand Down Expand Up @@ -75,7 +74,7 @@ var startCmd = &cobra.Command{

func init() {
startCmd.Flags().StringVar(&nodeRPCURL, "node-rpc", "http://localhost:9933", "Polkadot node rpc url")
startCmd.Flags().StringVar(&nodeWSURL, "node-ws", "http://localhost:9944", "Polkadot node websocket url")
startCmd.Flags().StringVar(&nodeWSURL, "node-ws", "ws://localhost:9944", "Polkadot node websocket url")
startCmd.Flags().StringVar(&nodeMetricsURL, "node-metrics", "http://localhost:9615", "Polkadot node metrics url")
startCmd.Flags().StringVar(&id, "id", "", "Vedran-daemon id string (required)")
startCmd.Flags().StringVar(&lbBaseURL, "lb", "", "Target load balancer url (required)")
Expand All @@ -91,6 +90,11 @@ func init() {
func start(cmd *cobra.Command, _ []string) error {
DisplayBanner()

err := checkIfWsAvailable(wsURL)
if err != nil {
return err
}

lbClient := lb.NewClient(lbURL)
nodeClient := node.NewClient(rpcURL, metricsURL)
telemetry := telemetry.NewTelemetry()
Expand All @@ -99,9 +103,9 @@ func start(cmd *cobra.Command, _ []string) error {
NodeWSURL: wsURL,
}

err := run.Start(tunnel, lbClient, nodeClient, telemetry, id, payoutAddress)
err = run.Start(tunnel, lbClient, nodeClient, telemetry, id, payoutAddress)
if err != nil {
return fmt.Errorf("Failed starting vedran daemon because: %v", err)
return fmt.Errorf("Failed starting vedran daemon, because of %v", err)
}

return nil
Expand Down
23 changes: 23 additions & 0 deletions cmd/ws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"
"github.com/gorilla/websocket"
"net/url"
"time"
)

func checkIfWsAvailable(websocketUrl *url.URL) error {
// check if ws available on given port
dialer := websocket.DefaultDialer
dialer.HandshakeTimeout = 2 * time.Second
c, _, err := dialer.Dial(websocketUrl.String(), nil)
if err != nil {
return fmt.Errorf("unable to connect to ws endpoint, because of %v", err)
}
err = c.Close()
if err != nil {
return err
}
return err
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/NodeFactoryIo/vedran v0.1.2-0.20201012103221-1832974f2e38
github.com/go-co-op/gocron v0.3.1
github.com/gorilla/websocket v1.4.0
github.com/gosuri/uitable v0.0.4
github.com/mitchellh/mapstructure v1.3.3
github.com/prometheus/common v0.13.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
Expand Down
13 changes: 11 additions & 2 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import (
var sleep = time.Sleep

// Start registers to load balancer and starts sending telemetry
func Start(tunnel tunnel.Tunneler, lbClient *lb.Client, nodeClient node.Client, telemetry telemetry.Telemetry, id string, payoutAddress string) error {
func Start(
tunnel tunnel.Tunneler,
lbClient *lb.Client,
nodeClient node.Client,
telemetry telemetry.Telemetry,
id string,
payoutAddress string,
) error {
var configHash hash.Hash32
for {
var err error
Expand All @@ -29,7 +36,9 @@ func Start(tunnel tunnel.Tunneler, lbClient *lb.Client, nodeClient node.Client,
sleep(time.Second * 5)
}

registerResponse, err := lbClient.Register(id, payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]))
registerResponse, err := lbClient.Register(
id, payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]),
)
if err != nil {
return err
}
Expand Down

0 comments on commit 90d9487

Please sign in to comment.