0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-11-24 23:10:40 +00:00
wp-git-clone/plugin/utils.go

48 lines
1.2 KiB
Go
Raw Normal View History

2023-12-22 23:59:23 +00:00
package plugin
import (
"os"
"strings"
"time"
2023-12-22 23:59:23 +00:00
"github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/trace"
2023-12-22 23:59:23 +00:00
"golang.org/x/sys/execabs"
)
// shouldRetry returns true if the command should be re-executed. Currently
// this only returns true if the remote ref does not exist.
func shouldRetry(s string) bool {
return strings.Contains(s, "find remote ref")
}
func newBackoff(maxRetries uint64) backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = daemonBackoffInitialInterval
b.Multiplier = daemonBackoffMultiplier
return backoff.WithMaxRetries(b, maxRetries)
}
func retryCmd(cmd *execabs.Cmd) error {
backoffOps := func() error {
// copy the original command
//nolint:gosec
retry := execabs.Command(cmd.Args[0], cmd.Args[1:]...)
retry.Dir = cmd.Dir
retry.Env = cmd.Env
retry.Stdout = os.Stdout
retry.Stderr = os.Stderr
trace.Cmd(cmd)
return cmd.Run()
}
backoffLog := func(err error, delay time.Duration) {
log.Error().Msgf("failed to find remote ref: %v: retry in %s", err, delay.Truncate(time.Second))
}
return backoff.RetryNotify(backoffOps, newBackoff(daemonBackoffMaxRetries), backoffLog)
}