From 912373a7d6c21d11e0b516c367ecf78e8163759a Mon Sep 17 00:00:00 2001 From: kyrylomiro Date: Tue, 5 Mar 2024 23:56:06 +0100 Subject: [PATCH] add retries for fetching logs from completed job --- pkg/actionsmetrics/event_reader.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/actionsmetrics/event_reader.go b/pkg/actionsmetrics/event_reader.go index 6789f14246..f72984d9c1 100644 --- a/pkg/actionsmetrics/event_reader.go +++ b/pkg/actionsmetrics/event_reader.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "fmt" + "io" "net/http" "regexp" "strings" @@ -232,7 +233,7 @@ func (reader *EventReader) fetchAndParseWorkflowJobLogs(ctx context.Context, e * if err != nil { return nil, err } - jobLogs, err := http.DefaultClient.Get(url.String()) + jobLogs, err := getWithRetry(url.String(), 3, time.Duration(3)*time.Second) if err != nil { return nil, err } @@ -290,3 +291,26 @@ func (reader *EventReader) fetchAndParseWorkflowJobLogs(ctx context.Context, e * RunTime: completedTime.Sub(startedTime), }, nil } + +func getWithRetry(url string, maxRetries int, retryDelay time.Duration) (*http.Response, error) { + for i := 0; i < maxRetries; i++ { + resp, err := http.DefaultClient.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if len(bodyBytes) != 0 { + return resp, nil + } + + time.Sleep(retryDelay) + } + + return nil, fmt.Errorf("empty response after max retries %d", maxRetries) +}