0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-09-20 01:22:47 +02:00
wp-git-clone/plugin/utils.go

52 lines
1.2 KiB
Go
Raw Normal View History

2023-12-23 00:59:23 +01:00
package plugin
import (
"fmt"
"os"
"strings"
"time"
2023-12-23 00:59:23 +01:00
"github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog/log"
2023-12-23 00:59:23 +01: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 trace(cmd *execabs.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}
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)
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)
}