Skip to content

Commit

Permalink
Implement retry logic for RunBinary function (#5361)
Browse files Browse the repository at this point in the history
* Implement retry logic for RunBinary function

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Fix the import groupings

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Nov 22, 2024
1 parent 1edffa2 commit b8070a9
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions pkg/lifecycle/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package lifecycle

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -26,8 +27,12 @@ import (
"time"

"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/backoff"
)

const runBinaryRetryCount = 3

type Command struct {
cmd *exec.Cmd
stoppedCh chan struct{}
Expand Down Expand Up @@ -66,27 +71,35 @@ func (c *Command) GracefulStop(period time.Duration) error {
}

func RunBinary(execPath string, args []string) (*Command, error) {
cmd := exec.Command(execPath, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd, err := backoff.NewRetry(runBinaryRetryCount, backoff.NewConstant(5*time.Second)).Do(context.Background(), func() (interface{}, error) {
cmd := exec.Command(execPath, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Start(); err != nil {
return nil, err
}

if err := cmd.Start(); err != nil {
return nil, err
}
c := &Command{
cmd: cmd,
stoppedCh: make(chan struct{}),
result: atomic.Pointer[error]{},
}
go func() {
err := cmd.Wait()
c.result.Store(&err)
close(c.stoppedCh)
}()

return c, nil
})

c := &Command{
cmd: cmd,
stoppedCh: make(chan struct{}),
result: atomic.Pointer[error]{},
if err != nil {
return nil, err
}
go func() {
err := cmd.Wait()
c.result.Store(&err)
close(c.stoppedCh)
}()

return c, nil
return cmd.(*Command), nil // The return type is always *Command.
}

// DownloadBinary downloads a file from the given URL into the specified path
Expand Down

0 comments on commit b8070a9

Please sign in to comment.