Skip to content

Commit 05d34cb

Browse files
raghavharnessHarness
authored andcommitted
feat: [CI-19302]: Add retry method of setup request (#444)
* ae8d9e [CI-19302]: Add retry method of setup request
1 parent 0b01664 commit 05d34cb

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

cli/client/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
type Client interface {
2727
Setup(ctx context.Context, in *api.SetupRequest) (*api.SetupResponse, error)
28+
RetrySetup(ctx context.Context, in *api.SetupRequest, timeout time.Duration) (*api.SetupResponse, error)
2829
Destroy(ctx context.Context, in *api.DestroyRequest) (*api.DestroyResponse, error)
2930
RetryStartStep(ctx context.Context, in *api.StartStepRequest) (*api.StartStepResponse, error)
3031
StartStep(ctx context.Context, in *api.StartStepRequest) (*api.StartStepResponse, error)

cli/client/http.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ func (c *HTTPClient) Setup(ctx context.Context, in *api.SetupRequest) (*api.Setu
8787
return out, err
8888
}
8989

90+
// RetrySetup will retry the setup operation
91+
func (c *HTTPClient) RetrySetup(ctx context.Context, in *api.SetupRequest, timeout time.Duration) (*api.SetupResponse, error) {
92+
startTime := time.Now()
93+
retryCtx, cancel := context.WithTimeout(ctx, timeout)
94+
defer cancel()
95+
96+
var lastErr error
97+
for i := 0; ; i++ {
98+
select {
99+
case <-retryCtx.Done():
100+
return nil, retryCtx.Err()
101+
default:
102+
}
103+
if ret, err := c.Setup(retryCtx, in); err == nil {
104+
logger.FromContext(ctx).
105+
WithField("duration", time.Since(startTime)).
106+
Trace("RetrySetup: setup completed")
107+
return ret, err
108+
} else if lastErr == nil || (lastErr.Error() != err.Error()) {
109+
logger.FromContext(ctx).
110+
WithField("retry_num", i).WithError(err).Traceln("setup failed. Retrying")
111+
lastErr = err
112+
}
113+
time.Sleep(time.Millisecond * 1000) //nolint:gomnd
114+
}
115+
}
116+
90117
// Destroy will clean up the resources created
91118
func (c *HTTPClient) Destroy(ctx context.Context, in *api.DestroyRequest) (*api.DestroyResponse, error) {
92119
path := "destroy"

cli/client/noop.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (n *NoopClient) Setup(ctx context.Context, in *api.SetupRequest) (*api.Setu
3333
return &api.SetupResponse{}, nil
3434
}
3535

36+
func (n *NoopClient) RetrySetup(ctx context.Context, in *api.SetupRequest, timeout time.Duration) (*api.SetupResponse, error) {
37+
return &api.SetupResponse{}, nil
38+
}
39+
3640
func (n *NoopClient) Destroy(ctx context.Context, in *api.DestroyRequest) (*api.DestroyResponse, error) {
3741
time.Sleep(n.destroyTime)
3842
return &api.DestroyResponse{}, nil

livelog/livelog.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// that can be found in the LICENSE file.
44

55
package livelog
6+
67
import (
78
"bytes"
89
"context"
@@ -17,9 +18,9 @@ import (
1718

1819
"github.com/harness/lite-engine/internal/safego"
1920
"github.com/harness/lite-engine/logstream"
20-
"github.com/sirupsen/logrus"
2121
"github.com/harness/lite-engine/logstream/remote"
2222
"github.com/harness/lite-engine/osstats"
23+
"github.com/sirupsen/logrus"
2324
)
2425

2526
const (

0 commit comments

Comments
 (0)