mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-18 04:15:15 +00:00
Make retry count of config fetching form forge configure (#3699)
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
parent
c962e4495d
commit
378ec1a67a
7 changed files with 35 additions and 15 deletions
|
@ -273,6 +273,12 @@ var flags = append([]cli.Flag{
|
||||||
Usage: "how many seconds before timeout when fetching the Woodpecker configuration from a Forge",
|
Usage: "how many seconds before timeout when fetching the Woodpecker configuration from a Forge",
|
||||||
Value: time.Second * 3,
|
Value: time.Second * 3,
|
||||||
},
|
},
|
||||||
|
&cli.UintFlag{
|
||||||
|
EnvVars: []string{"WOODPECKER_FORGE_RETRY"},
|
||||||
|
Name: "forge-retry",
|
||||||
|
Usage: "How many retries of fetching the Woodpecker configuration from a forge are done before we fail",
|
||||||
|
Value: 3,
|
||||||
|
},
|
||||||
&cli.Int64Flag{
|
&cli.Int64Flag{
|
||||||
EnvVars: []string{"WOODPECKER_LIMIT_MEM_SWAP"},
|
EnvVars: []string{"WOODPECKER_LIMIT_MEM_SWAP"},
|
||||||
Name: "limit-mem-swap",
|
Name: "limit-mem-swap",
|
||||||
|
|
|
@ -525,6 +525,12 @@ Specify a configuration service endpoint, see [Configuration Extension](./100-ex
|
||||||
|
|
||||||
Specify timeout when fetching the Woodpecker configuration from forge. See <https://pkg.go.dev/time#ParseDuration> for syntax reference.
|
Specify timeout when fetching the Woodpecker configuration from forge. See <https://pkg.go.dev/time#ParseDuration> for syntax reference.
|
||||||
|
|
||||||
|
### `WOODPECKER_FORGE_RETRY`
|
||||||
|
|
||||||
|
> Default: 3
|
||||||
|
|
||||||
|
Specify how many retries of fetching the Woodpecker configuration from a forge are done before we fail.
|
||||||
|
|
||||||
### `WOODPECKER_ENABLE_SWAGGER`
|
### `WOODPECKER_ENABLE_SWAGGER`
|
||||||
|
|
||||||
> Default: true
|
> Default: true
|
||||||
|
|
|
@ -221,7 +221,7 @@ func TestFetchFromConfigService(t *testing.T) {
|
||||||
|
|
||||||
f.On("Netrc", mock.Anything, mock.Anything).Return(&model.Netrc{Machine: "mock", Login: "mock", Password: "mock"}, nil)
|
f.On("Netrc", mock.Anything, mock.Anything).Return(&model.Netrc{Machine: "mock", Login: "mock", Password: "mock"}, nil)
|
||||||
|
|
||||||
forgeFetcher := config.NewForge(time.Second * 3)
|
forgeFetcher := config.NewForge(time.Second*3, 3)
|
||||||
configFetcher := config.NewCombined(forgeFetcher, httpFetcher)
|
configFetcher := config.NewCombined(forgeFetcher, httpFetcher)
|
||||||
files, err := configFetcher.Fetch(
|
files, err := configFetcher.Fetch(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
|
|
|
@ -29,17 +29,15 @@ import (
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/shared/constant"
|
"go.woodpecker-ci.org/woodpecker/v2/shared/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
forgeFetchingRetryCount = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
type forgeFetcher struct {
|
type forgeFetcher struct {
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
retryCount uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewForge(timeout time.Duration) Service {
|
func NewForge(timeout time.Duration, retries uint) Service {
|
||||||
return &forgeFetcher{
|
return &forgeFetcher{
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
|
retryCount: retries,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +56,7 @@ func (f *forgeFetcher) Fetch(ctx context.Context, forge forge.Forge, user *model
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to fetch multiple times
|
// try to fetch multiple times
|
||||||
for i := 0; i < forgeFetchingRetryCount; i++ {
|
for i := 0; i < int(f.retryCount); i++ {
|
||||||
files, err = ffc.fetch(ctx, strings.TrimSpace(repo.Config))
|
files, err = ffc.fetch(ctx, strings.TrimSpace(repo.Config))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace().Err(err).Msgf("%d. try failed", i+1)
|
log.Trace().Err(err).Msgf("%d. try failed", i+1)
|
||||||
|
|
|
@ -306,7 +306,8 @@ func TestFetch(t *testing.T) {
|
||||||
f.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("directory not found"))
|
f.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("directory not found"))
|
||||||
|
|
||||||
configFetcher := config.NewForge(
|
configFetcher := config.NewForge(
|
||||||
time.Second * 3,
|
time.Second*3,
|
||||||
|
3,
|
||||||
)
|
)
|
||||||
files, err := configFetcher.Fetch(
|
files, err := configFetcher.Fetch(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
|
|
|
@ -72,13 +72,18 @@ func NewManager(c *cli.Context, store store.Store, setupForge SetupForge) (Manag
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configService, err := setupConfigService(c, signaturePrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &manager{
|
return &manager{
|
||||||
signaturePrivateKey: signaturePrivateKey,
|
signaturePrivateKey: signaturePrivateKey,
|
||||||
signaturePublicKey: signaturePublicKey,
|
signaturePublicKey: signaturePublicKey,
|
||||||
store: store,
|
store: store,
|
||||||
secret: setupSecretService(store),
|
secret: setupSecretService(store),
|
||||||
registry: setupRegistryService(store, c.String("docker-config")),
|
registry: setupRegistryService(store, c.String("docker-config")),
|
||||||
config: setupConfigService(c, signaturePrivateKey),
|
config: configService,
|
||||||
environment: environment.Parse(c.StringSlice("environment")),
|
environment: environment.Parse(c.StringSlice("environment")),
|
||||||
forgeCache: ttlcache.New(ttlcache.WithDisableTouchOnHit[int64, forge.Forge]()),
|
forgeCache: ttlcache.New(ttlcache.WithDisableTouchOnHit[int64, forge.Forge]()),
|
||||||
setupForge: setupForge,
|
setupForge: setupForge,
|
||||||
|
|
|
@ -56,16 +56,20 @@ func setupSecretService(store store.Store) secret.Service {
|
||||||
return secret.NewDB(store)
|
return secret.NewDB(store)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupConfigService(c *cli.Context, privateSignatureKey crypto.PrivateKey) config.Service {
|
func setupConfigService(c *cli.Context, privateSignatureKey crypto.PrivateKey) (config.Service, error) {
|
||||||
timeout := c.Duration("forge-timeout")
|
timeout := c.Duration("forge-timeout")
|
||||||
configFetcher := config.NewForge(timeout)
|
retries := c.Uint("forge-retry")
|
||||||
|
if retries == 0 {
|
||||||
|
return nil, fmt.Errorf("WOODPECKER_FORGE_RETRY can not be 0")
|
||||||
|
}
|
||||||
|
configFetcher := config.NewForge(timeout, retries)
|
||||||
|
|
||||||
if endpoint := c.String("config-service-endpoint"); endpoint != "" {
|
if endpoint := c.String("config-service-endpoint"); endpoint != "" {
|
||||||
httpFetcher := config.NewHTTP(endpoint, privateSignatureKey)
|
httpFetcher := config.NewHTTP(endpoint, privateSignatureKey)
|
||||||
return config.NewCombined(configFetcher, httpFetcher)
|
return config.NewCombined(configFetcher, httpFetcher), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return configFetcher
|
return configFetcher, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupSignatureKeys generate or load key pair to sign webhooks requests (i.e. used for service extensions)
|
// setupSignatureKeys generate or load key pair to sign webhooks requests (i.e. used for service extensions)
|
||||||
|
|
Loading…
Reference in a new issue