2023-12-22 23:59:23 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-12-23 15:12:56 +00:00
|
|
|
"time"
|
2023-12-22 23:59:23 +00:00
|
|
|
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
2023-12-23 15:12:56 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-02 21:58:05 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2023-12-23 15:12:56 +00:00
|
|
|
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
|
|
|
|
|
2024-01-02 21:58:05 +00:00
|
|
|
trace.Cmd(cmd)
|
2023-12-23 15:12:56 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|