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

refactor: retry logic for deployment key #223

Merged
33 changes: 32 additions & 1 deletion services/monkey/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"fmt"
"io"
"reflect"
"time"

"github.com/taubyte/tau/core/services/auth"
)

func ToNumber(in interface{}) int {
func toNumber(in interface{}) int {
i := reflect.ValueOf(in)
switch i.Kind() {
case reflect.Int64:
Expand Down Expand Up @@ -39,3 +42,31 @@ func (m *Monkey) storeLogs(r io.ReadSeeker) (string, error) {

return cid, nil
}

var (
GetGitRepoMaxRetries = 3
GetGitRepoWaitBeforeRetry = 5 * time.Second
)

func (m *Monkey) tryGetGitRepo(
ac auth.Client,
repoID int,
) (gitRepo auth.GithubRepository, err error) {
for i := 0; i < GetGitRepoMaxRetries; i++ {
gitRepo, err = ac.Repositories().Github().Get(repoID)
if err != nil {
return gitRepo, fmt.Errorf("fetching repository %d from auth failed with %w", repoID, err)
}

deployKey := gitRepo.PrivateKey()
if len(deployKey) != 0 {
break
}

if i < GetGitRepoMaxRetries-1 {
time.Sleep(GetGitRepoWaitBeforeRetry)
}
}

return gitRepo, nil
}
53 changes: 15 additions & 38 deletions services/monkey/job.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package monkey

import (
"errors"
"fmt"
"strings"
"time"

authClient "github.com/taubyte/tau/clients/p2p/auth"
"github.com/taubyte/tau/core/services/auth"
Expand Down Expand Up @@ -38,17 +36,19 @@ func (m *Monkey) RunJob() (err error) {
var p *auth.Project
repoType := compilerCommon.UnknownRepository

gitRepo, _ := ac.Repositories().Github().Get(repo.ID)
if gitRepo != nil {
projectId = gitRepo.Project()
if projectId != "" {
p = ac.Projects().Get(projectId)
switch repo.ID {
case p.Git.Code.Id():
repoType = compilerCommon.CodeRepository
case p.Git.Config.Id():
repoType = compilerCommon.ConfigRepository
}
gitRepo, err := m.tryGetGitRepo(ac, repo.ID)
if err != nil {
return fmt.Errorf("run job failed during fetching with %w", err)
}

projectId = gitRepo.Project()
if projectId != "" {
p = ac.Projects().Get(projectId)
switch repo.ID {
case p.Git.Code.Id():
repoType = compilerCommon.CodeRepository
case p.Git.Config.Id():
repoType = compilerCommon.ConfigRepository
}
}

Expand Down Expand Up @@ -77,31 +77,7 @@ func (m *Monkey) RunJob() (err error) {
return fmt.Errorf("fetch project failed with: %w", err)
}

repoType = compilerCommon.RepositoryType(ToNumber(repoTypeResponse.Interface()))
gitRepo, err = ac.Repositories().Github().Get(repo.ID)
if err != nil {
return fmt.Errorf("auth github get failed with: %w", err)
}

}

// TODO: This is tempororary, let mechanism retry this, if len() == 0 fail
var deployKey string
for i := 1; i < 3; i++ {
deployKey = gitRepo.PrivateKey()
if len(deployKey) != 0 {
break
}

logger.Debug("Deploy key is empty, retrying")
time.Sleep(5 * time.Second)
gitRepo, err = ac.Repositories().Github().Get(repo.ID)
if err != nil {
return fmt.Errorf("auth github get failed with: %w", err)
}
}
if len(deployKey) < 1 {
return errors.New("getting deploy key failed")
repoType = compilerCommon.RepositoryType(toNumber(repoTypeResponse.Interface()))
}

c := jobs.Context{
Expand All @@ -118,6 +94,7 @@ func (m *Monkey) RunJob() (err error) {
DVPublicKey: m.Service.dvPublicKey, // For Domain Validation
GeneratedDomainRegExp: m.generatedDomainRegExp,
}

if repoType == compilerCommon.CodeRepository {
c.ConfigRepoId = p.Git.Config.Id()

Expand Down
Loading