mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-23 09:08:26 +01:00
Add retry for getting configs from admin server when doing job conetxt initialization
This commit is contained in:
parent
f7bc467c99
commit
4db708096b
@ -6,7 +6,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/vmware/harbor/src/adminserver/client"
|
||||
"github.com/vmware/harbor/src/common"
|
||||
@ -19,6 +21,10 @@ import (
|
||||
"github.com/vmware/harbor/src/jobservice/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
maxRetryTimes = 5
|
||||
)
|
||||
|
||||
//Context ...
|
||||
type Context struct {
|
||||
//System context
|
||||
@ -51,9 +57,25 @@ func NewContext(sysCtx context.Context, adminClient client.Client) *Context {
|
||||
|
||||
//Init ...
|
||||
func (c *Context) Init() error {
|
||||
configs, err := c.adminClient.GetCfgs()
|
||||
if err != nil {
|
||||
return err
|
||||
var (
|
||||
counter = 0
|
||||
err error
|
||||
configs map[string]interface{}
|
||||
)
|
||||
|
||||
for counter == 0 || err != nil {
|
||||
counter++
|
||||
configs, err = c.adminClient.GetCfgs()
|
||||
if err != nil {
|
||||
logger.Errorf("Job context initialization error: %s\n", err.Error())
|
||||
if counter < maxRetryTimes {
|
||||
backoff := (int)(math.Pow(2, (float64)(counter))) + 2*counter + 5
|
||||
logger.Infof("Retry in %d seconds", backoff)
|
||||
time.Sleep(time.Duration(backoff) * time.Second)
|
||||
} else {
|
||||
return fmt.Errorf("job context initialization error: %s (%d times tried)", err.Error(), counter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db := getDBFromConfig(configs)
|
||||
|
Loading…
Reference in New Issue
Block a user