Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved PollUpdateSignup function to common package #340

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions pkg/signup/service/signup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (s *ServiceImpl) DoGetSignup(ctx *gin.Context, provider ResourceProvider, u
var userSignup *toolchainv1alpha1.UserSignup
var err error

err = pollUpdateSignup(ctx, func() error {
err = signup.PollUpdateSignup(ctx, func() error {
// Retrieve UserSignup resource from the host cluster, using the specified UserID and username
var getError error
userSignup, getError = s.DoGetUserSignupFromIdentifier(provider, userID, username)
Expand Down Expand Up @@ -621,30 +621,3 @@ func getAppsURL(appRouteName string, signup signup.Signup) string {
appsURL := signup.ConsoleURL[index:]
return fmt.Sprintf("https://%s%s", appRouteName, appsURL)
}

func pollUpdateSignup(ctx *gin.Context, updater func() error) error {
// Attempt to execute an update function, retrying a number of times if the update fails
attempts := 0
for {
attempts++

// Attempt the update
updateErr := updater()

// If there was an error, then only log it for now
if updateErr != nil {
log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts))
} else {
// Otherwise if there was no error executing the update, then break here
break
}

// If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual
// error to the user here, as we've already logged it
if attempts > 4 {
return updateErr
}
}

return nil
}
35 changes: 35 additions & 0 deletions pkg/signup/signup.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package signup

import (
"fmt"
"github.com/codeready-toolchain/registration-service/pkg/log"
"github.com/gin-gonic/gin"
)

// Signup represents Signup resource which is a wrapper of K8s UserSignup
// and the corresponding MasterUserRecord resources.
type Signup struct {
Expand Down Expand Up @@ -49,3 +55,32 @@ type Status struct {
// Default value is false.
VerificationRequired bool `json:"verificationRequired"`
}

// PollUpdateSignup will attempt to execute the provided updater function, and if it fails
// will reattempt the update for a limited number of retries
func PollUpdateSignup(ctx *gin.Context, updater func() error) error {
// Attempt to execute an update function, retrying a number of times if the update fails
attempts := 0
for {
attempts++

// Attempt the update
updateErr := updater()

// If there was an error, then only log it for now
if updateErr != nil {
log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts))
} else {
// Otherwise if there was no error executing the update, then break here
break
}

// If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual
// error to the user here, as we've already logged it
if attempts > 4 {
return updateErr
}
}

return nil
}
36 changes: 4 additions & 32 deletions pkg/verification/service/verification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"errors"
"fmt"
signup2 "github.com/codeready-toolchain/registration-service/pkg/signup"
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -184,7 +185,7 @@ func (s *ServiceImpl) InitVerification(ctx *gin.Context, userID, username, e164P
return nil
}

updateErr := pollUpdateSignup(ctx, doUpdate)
updateErr := signup2.PollUpdateSignup(ctx, doUpdate)
if updateErr != nil {
return updateErr
}
Expand Down Expand Up @@ -311,7 +312,7 @@ func (s *ServiceImpl) VerifyPhoneCode(ctx *gin.Context, userID, username, code s
return nil
}

updateErr := pollUpdateSignup(ctx, doUpdate)
updateErr := signup2.PollUpdateSignup(ctx, doUpdate)
if updateErr != nil {
return updateErr
}
Expand Down Expand Up @@ -366,7 +367,7 @@ func (s *ServiceImpl) VerifyActivationCode(ctx *gin.Context, userID, username, c

return nil
}
if err := pollUpdateSignup(ctx, doUpdate); err != nil {
if err := signup2.PollUpdateSignup(ctx, doUpdate); err != nil {
log.Errorf(ctx, err, "unable to update user signup after validating activation code")
}
}()
Expand Down Expand Up @@ -422,32 +423,3 @@ func checkAttempts(signup *toolchainv1alpha1.UserSignup) (int, error) {
}
return attemptsMade, nil
}

func pollUpdateSignup(ctx *gin.Context, updater func() error) error {
// Attempt to execute an update function, retrying a number of times if the update fails
attempts := 0
for {
attempts++

// Attempt the update
updateErr := updater()

// If there was an error, then only log it for now
if updateErr != nil {
log.Error(ctx, updateErr, fmt.Sprintf("error while executing updating, attempt #%d", attempts))
} else {
// Otherwise if there was no error executing the update, then break here
break
}

// If we've exceeded the number of attempts, then return a useful error to the user. We won't return the actual
// error to the user here, as we've already logged it
if attempts > 4 {
return crterrors.NewInternalError(errors.New("there was an error while updating your account - please wait a moment before trying again."+
" If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com for assistance"),
"error while verifying phone code")
}
}

return nil
}
Loading