0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-11-28 18:20:35 +00:00

feat: add backoff retry to docker version to avoid transient failure (#19)

This commit is contained in:
Robert Kaussow 2023-09-04 20:56:19 +02:00 committed by GitHub
parent 3edc76494a
commit b419036322
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 8 deletions

View File

@ -95,3 +95,7 @@ linters-settings:
gofumpt: gofumpt:
extra-rules: true extra-rules: true
lang-version: "1.21" lang-version: "1.21"
ireturn:
allow:
- error
- backoff.BackOff

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/thegeeklab/wp-docker-buildx
go 1.21 go 1.21
require ( require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/coreos/go-semver v0.3.1 github.com/coreos/go-semver v0.3.1
github.com/rs/zerolog v1.30.0 github.com/rs/zerolog v1.30.0
github.com/thegeeklab/wp-plugin-go v1.0.1 github.com/thegeeklab/wp-plugin-go v1.0.1

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=

View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/cenkalti/backoff"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/types" "github.com/thegeeklab/wp-plugin-go/types"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -16,7 +17,12 @@ import (
var ErrTypeAssertionFailed = errors.New("type assertion failed") var ErrTypeAssertionFailed = errors.New("type assertion failed")
const strictFilePerm = 0o600 const (
strictFilePerm = 0o600
daemonBackoffMaxRetries = 3
daemonBackoffInitialInterval = 2 * time.Second
daemonBackoffMultiplier = 3.5
)
//nolint:revive //nolint:revive
func (p *Plugin) run(ctx context.Context) error { func (p *Plugin) run(ctx context.Context) error {
@ -149,16 +155,31 @@ func (p *Plugin) Execute() error {
// add proxy build args // add proxy build args
addProxyBuildArgs(&p.Settings.Build) addProxyBuildArgs(&p.Settings.Build)
var cmds []*execabs.Cmd versionCmd := commandVersion() // docker version
cmds = append(cmds, commandVersion()) // docker version
cmds = append(cmds, commandInfo()) // docker info
cmds = append(cmds, commandBuilder(p.Settings.Daemon))
cmds = append(cmds, commandBuildx())
cmds = append(cmds, commandBuild(p.Settings.Build, p.Settings.Dryrun)) // docker build versionCmd.Stdout = os.Stdout
versionCmd.Stderr = os.Stderr
trace(versionCmd)
backoffOps := func() error {
return versionCmd.Run()
}
backoffLog := func(err error, delay time.Duration) {
log.Error().Msgf("failed to exec docker version: %v: retry in %s", err, delay.Truncate(time.Second))
}
if err := backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog); err != nil {
return err
}
var batchCmd []*execabs.Cmd
batchCmd = append(batchCmd, commandInfo()) // docker info
batchCmd = append(batchCmd, commandBuilder(p.Settings.Daemon))
batchCmd = append(batchCmd, commandBuildx())
batchCmd = append(batchCmd, commandBuild(p.Settings.Build, p.Settings.Dryrun)) // docker build
// execute all commands in batch mode. // execute all commands in batch mode.
for _, cmd := range cmds { for _, cmd := range batchCmd {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
trace(cmd) trace(cmd)
@ -189,3 +210,11 @@ func (p *Plugin) FlagsFromContext() error {
return nil return nil
} }
func newBackoff(maxRetries uint64) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = daemonBackoffInitialInterval
b.Multiplier = daemonBackoffMultiplier
return backoff.WithMaxRetries(b, maxRetries)
}