Skip to content

Commit

Permalink
add retries for fetching logs from completed job
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylomiro committed Mar 5, 2024
1 parent 9148840 commit 912373a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/actionsmetrics/event_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 912373a

Please sign in to comment.