Skip to content

Commit

Permalink
feat: add start/stop timeout options (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Jan 19, 2024
1 parent 19c4477 commit 1245caf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ GLOBAL OPTIONS:
--no-staking disable calls to staking module (useful for consumer chains) (default: false)
--denom value denom used in metrics label (eg. atom or uatom)
--denom-exponent value denom exponent (eg. 6 for atom, 1 for uatom) (default: 0)
--start-timeout value timeout to wait on startup for one node to be ready (default: 10s)
--stop-timeout value timeout to wait on stop (default: 10s)
--validator value [ --validator value ] validator address(es) to track (use :my-label to add a custom label in metrics & ouput)
--webhook-url value endpoint where to send upgrade webhooks
--x-gov value version of the gov module to use (v1|v1beta1) (default: "v1beta1")
--webhook-url value endpoint where to send upgrade webhooks (experimental)
--x-gov value version of the gov module to use (v1|v1beta1) (default: "v1")
--help, -h show help
--version, -v print the version
```
Expand Down
16 changes: 15 additions & 1 deletion pkg/app/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package app

import "github.com/urfave/cli/v2"
import (
"time"

"github.com/urfave/cli/v2"
)

var Flags = []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -47,6 +51,16 @@ var Flags = []cli.Flag{
Name: "denom-exponent",
Usage: "denom exponent (eg. 6 for atom, 1 for uatom)",
},
&cli.DurationFlag{
Name: "start-timeout",
Usage: "timeout to wait on startup for one node to be ready",
Value: 10 * time.Second,
},
&cli.DurationFlag{
Name: "stop-timeout",
Usage: "timeout to wait on stop",
Value: 10 * time.Second,
},
&cli.StringSliceFlag{
Name: "validator",
Usage: "validator address(es) to track (use :my-label to add a custom label in metrics & ouput)",
Expand Down
40 changes: 21 additions & 19 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -31,19 +30,21 @@ func RunFunc(cCtx *cli.Context) error {
ctx = cCtx.Context

// Config flags
chainID = cCtx.String("chain-id")
httpAddr = cCtx.String("http-addr")
logLevel = cCtx.String("log-level")
namespace = cCtx.String("namespace")
noColor = cCtx.Bool("no-color")
nodes = cCtx.StringSlice("node")
noGov = cCtx.Bool("no-gov")
noStaking = cCtx.Bool("no-staking")
denom = cCtx.String("denom")
denomExpon = cCtx.Uint("denom-exponent")
validators = cCtx.StringSlice("validator")
webhookURL = cCtx.String("webhook-url")
xGov = cCtx.String("x-gov")
chainID = cCtx.String("chain-id")
httpAddr = cCtx.String("http-addr")
logLevel = cCtx.String("log-level")
namespace = cCtx.String("namespace")
noColor = cCtx.Bool("no-color")
nodes = cCtx.StringSlice("node")
noGov = cCtx.Bool("no-gov")
noStaking = cCtx.Bool("no-staking")
denom = cCtx.String("denom")
denomExpon = cCtx.Uint("denom-exponent")
startTimeout = cCtx.Duration("start-timeout")
stopTimeout = cCtx.Duration("stop-timeout")
validators = cCtx.StringSlice("validator")
webhookURL = cCtx.String("webhook-url")
xGov = cCtx.String("x-gov")
)

//
Expand All @@ -64,8 +65,12 @@ func RunFunc(cCtx *cli.Context) error {
// Create errgroup to manage all goroutines
errg, ctx := errgroup.WithContext(ctx)

// Timeout to wait for at least one node to be ready
startCtx, cancel := context.WithTimeout(ctx, startTimeout)
defer cancel()

// Test connection to nodes
pool, err := createNodePool(ctx, nodes)
pool, err := createNodePool(startCtx, nodes)
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +179,7 @@ func RunFunc(cCtx *cli.Context) error {
//
// Stop all watchers and exporter
//
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel = context.WithTimeout(context.Background(), stopTimeout)
defer cancel()

if err := pool.Stop(ctx); err != nil {
Expand Down Expand Up @@ -206,9 +211,6 @@ func logLevelFromString(level string) zerolog.Level {
func createNodePool(ctx context.Context, nodes []string) (*rpc.Pool, error) {
rpcNodes := make([]*rpc.Node, len(nodes))
for i, endpoint := range nodes {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

client, err := http.New(endpoint, "/websocket")
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
Expand Down

0 comments on commit 1245caf

Please sign in to comment.