Skip to content

Commit

Permalink
feat: add orb-cli log update retries
Browse files Browse the repository at this point in the history
closes #1355

Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat committed Aug 4, 2022
1 parent c7a4b2c commit cc8a9d8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/orb-cli/followcmd/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func createCmd() *cobra.Command { //nolint:funlen,gocyclo,cyclop,gocognit
return fmt.Errorf("failed to send http request: %w", err)
}

for i := 0; i < maxRetry; i++ {
for i := 1; i <= maxRetry; i++ {
resp, err := common.SendRequest(httpClient, nil, headers, http.MethodGet,
fmt.Sprintf("%s/following?page=true", actor))
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/orb-cli/logcmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ package logcmd

import (
"errors"
"time"

"github.com/spf13/cobra"
)

const (
defaultMaxRetry = 10
defaultWaitTime = 1 * time.Second
)

const (
urlFlagName = "url"
urlEnvKey = "ORB_CLI_URL"
Expand All @@ -22,6 +28,16 @@ const (
typeEnvKey = "ORB_CLI_LOG"
logFlagUsage = `The domain log. For example "https://vct.com/log".` +
" Alternatively, this can be set with the following environment variable: " + typeEnvKey

maxRetryFlagName = "max-retry"
maxRetryEnvKey = "ORB_CLI_MAX_RETRY"
maxRetryFlagUsage = "max retry to check if follow cmd is succeed default value is 10" +
" Alternatively, this can be set with the following environment variable: " + maxRetryEnvKey

waitTimeFlagName = "wait-time"
waitTimeEnvKey = "ORB_CLI_WAIT_TIME"
waitTimeFlagUsage = "wait time between retries default value is 1s" +
" Alternatively, this can be set with the following environment variable: " + waitTimeEnvKey
)

// GetCmd returns the Cobra log command.
Expand Down
73 changes: 52 additions & 21 deletions cmd/orb-cli/logcmd/updatecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/spf13/cobra"
cmdutils "github.com/trustbloc/edge-core/pkg/utils/cmd"
Expand All @@ -35,16 +37,62 @@ func newUpdateCmd() *cobra.Command {
}

func executeUpdate(cmd *cobra.Command) error {
u, log, err := getUpdateArgs(cmd)
u, err := cmdutils.GetUserSetVarFromString(cmd, urlFlagName, urlEnvKey, false)
if err != nil {
return err
}

_, err = common.SendHTTPRequest(cmd, []byte(log), http.MethodPost, u)
_, err = url.Parse(u)
if err != nil {
return fmt.Errorf("invalid URL %s: %w", u, err)
}

log, err := cmdutils.GetUserSetVarFromString(cmd, logFlagName, typeEnvKey, false)
if err != nil {
return err
}

maxRetry := defaultMaxRetry

maxRetryString := cmdutils.GetUserSetOptionalVarFromString(cmd, maxRetryFlagName,
maxRetryEnvKey)

if maxRetryString != "" {
maxRetry, err = strconv.Atoi(maxRetryString)
if err != nil {
return fmt.Errorf("failed to convert max retry string to an integer: %w", err)
}
}

waitTime, err := common.GetDuration(cmd, waitTimeFlagName,
waitTimeEnvKey, defaultWaitTime)
if err != nil {
return err
}

for i := 1; i <= maxRetry; i++ {
_, err = common.SendHTTPRequest(cmd, []byte(log), http.MethodPost, u)
if err != nil {
return err
}

resp, err := common.SendHTTPRequest(cmd, nil, http.MethodGet, u)
if err != nil {
return err
}

if string(resp) == log {
break
}

if i == maxRetry {
return fmt.Errorf("update log failed max retries exhausted check server logs for more info")
}

time.Sleep(waitTime)

}

fmt.Println("Domain log has successfully been updated.")

return nil
Expand All @@ -55,23 +103,6 @@ func addUpdateFlags(cmd *cobra.Command) {

cmd.Flags().StringP(urlFlagName, "", "", urlFlagUsage)
cmd.Flags().StringP(logFlagName, "", "", logFlagUsage)
}

func getUpdateArgs(cmd *cobra.Command) (u, log string, err error) {
u, err = cmdutils.GetUserSetVarFromString(cmd, urlFlagName, urlEnvKey, false)
if err != nil {
return "", "", err
}

_, err = url.Parse(u)
if err != nil {
return "", "", fmt.Errorf("invalid URL %s: %w", u, err)
}

log, err = cmdutils.GetUserSetVarFromString(cmd, logFlagName, typeEnvKey, false)
if err != nil {
return "", "", err
}

return u, log, nil
cmd.Flags().StringP(maxRetryFlagName, "", "", maxRetryFlagUsage)
cmd.Flags().StringP(waitTimeFlagName, "", "", waitTimeFlagUsage)
}
30 changes: 29 additions & 1 deletion cmd/orb-cli/logcmd/updatecmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestUpdateCmd(t *testing.T) {

t.Run("update -> success", func(t *testing.T) {
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "d1")
_, err := fmt.Fprint(w, "https://vct.com/log")
require.NoError(t, err)
}))

Expand All @@ -80,6 +80,30 @@ func TestUpdateCmd(t *testing.T) {

require.NoError(t, err)
})

t.Run("update -> failed", func(t *testing.T) {
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "https://vct.com/log1")
require.NoError(t, err)
}))

cmd := GetCmd()

args := []string{"update"}
args = append(args, urlArg(serv.URL)...)
args = append(args, logArg("https://vct.com/log")...)
args = append(args, authTokenArg("ADMIN_TOKEN")...)
args = append(args, maxRetryArg("2")...)
cmd.SetArgs(args)

cmd.SetArgs(args)
err := cmd.Execute()

require.Error(t, err)
require.Equal(t,
"update log failed max retries exhausted check server logs for more info",
err.Error())
})
}

func urlArg(value string) []string {
Expand All @@ -93,3 +117,7 @@ func logArg(value string) []string {
func authTokenArg(value string) []string {
return []string{flag + common.AuthTokenFlagName, value}
}

func maxRetryArg(value string) []string {
return []string{flag + maxRetryFlagName, value}
}
2 changes: 1 addition & 1 deletion cmd/orb-cli/witnesscmd/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func cmd() *cobra.Command { //nolint:funlen,gocyclo,cyclop,gocognit
return fmt.Errorf("failed to send http request: %w", err)
}

for i := 0; i < maxRetry; i++ {
for i := 1; i <= maxRetry; i++ {
resp, err := common.SendRequest(httpClient, nil, headers, http.MethodGet,
fmt.Sprintf("%s/witnesses?page=true", actor))
if err != nil {
Expand Down

0 comments on commit cc8a9d8

Please sign in to comment.