Skip to content

Commit 06c0ec0

Browse files
Change caught error string (#127)
* add retries
1 parent 6b137dd commit 06c0ec0

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

cli/cmd/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var logsCmd = &cobra.Command{
2929
ctx := context.Background()
3030
if streamLogs {
3131
// This is a _very_ simple approach to streaming.
32-
cobra.CheckErr(apiCl.StreamLogs(ctx, os.Stdout, workflowName, loggingStartByte))
32+
cobra.CheckErr(apiCl.StreamLogs(ctx, os.Stdout, workflowName))
3333
} else {
3434
resp, err := apiCl.GetLogs(ctx, workflowName)
3535
if err != nil {

cli/internal/api/api.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,24 @@ func (c *Client) GetLogs(ctx context.Context, workflowName string) (responses.Ge
7878
}
7979

8080
// StreamLogs streams the logs of a workflow starting after loggedBytes.
81-
func (c *Client) StreamLogs(ctx context.Context, w io.Writer, workflowName string, skippedLogBytes int64) error {
81+
func (c *Client) StreamLogs(ctx context.Context, w io.Writer, workflowName string) error {
82+
var loggingCursorByte int64
83+
// When we receive a stream error we continue and retry processing up to 5 times keeping track of the byte we were logging.
84+
for i := 0; i < 5; i++ {
85+
err := c.streamLogsToWriterAtCursor(ctx, w, workflowName, &loggingCursorByte)
86+
if err != nil {
87+
if strings.Contains(err.Error(), "stream error: stream ID 1; INTERNAL_ERROR") {
88+
time.Sleep(time.Second * 10)
89+
continue
90+
}
91+
return err
92+
}
93+
break
94+
}
95+
return nil
96+
}
97+
98+
func (c *Client) streamLogsToWriterAtCursor(ctx context.Context, w io.Writer, workflowName string, loggingCursorByte *int64) error {
8299
url := fmt.Sprintf("%s/workflows/%s/logstream", c.endpoint, workflowName)
83100

84101
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
@@ -96,26 +113,17 @@ func (c *Client) StreamLogs(ctx context.Context, w io.Writer, workflowName strin
96113
return fmt.Errorf("received unexpected status code: %d", resp.StatusCode)
97114
}
98115

99-
// discard reader bytes already logged
100-
if _, err := io.CopyN(ioutil.Discard, resp.Body, skippedLogBytes); err != nil {
116+
// discard reader bytes till cursor byte number
117+
if _, err := io.CopyN(ioutil.Discard, resp.Body, *loggingCursorByte); err != nil {
101118
return err
102119
}
103-
skippedLogBytes, err = io.Copy(w, resp.Body)
120+
writtenBytes, err := io.Copy(w, resp.Body)
104121
if err != nil {
105-
// retry call if we receive the stream error
106-
if strings.Contains(err.Error(), "INTERNAL_ERROR") {
107-
// temporary debug message
108-
_, err := fmt.Fprintf(w, "internal error found while copying: '%v'\n", err.Error())
109-
if err != nil {
110-
return err
111-
}
112-
time.Sleep(time.Second * 10)
113-
// Restart log streaming
114-
return c.StreamLogs(ctx, w, workflowName, skippedLogBytes)
115-
}
122+
// retry call if we receive the stream error, increase the logging cursor byte by the amount we logged.
123+
*loggingCursorByte += writtenBytes
124+
116125
return fmt.Errorf("error reading response body. status code: %d, error: %w", resp.StatusCode, err)
117126
}
118-
119127
return nil
120128
}
121129

cli/internal/api/api_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func TestGetLogs(t *testing.T) {
124124
} else {
125125
assert.Nil(t, err)
126126
assert.Equal(t, output, tt.want)
127+
127128
}
128129
})
129130
}
@@ -134,8 +135,7 @@ func TestStreamLogs(t *testing.T) {
134135
name string
135136
apiRespBody []byte
136137
apiRespStatusCode int
137-
endpoint string // Used to create new request error.
138-
loggedBytes int64
138+
endpoint string // Used to create new request error.
139139
mockHTTPClient *mockHTTPClient // Only used when needed.
140140
writeBadContentLength bool // Used to create response body error.
141141
want []byte
@@ -147,13 +147,6 @@ func TestStreamLogs(t *testing.T) {
147147
apiRespStatusCode: http.StatusOK,
148148
want: readFile(t, "stream_logs_good.txt"),
149149
},
150-
{
151-
name: "Log after 8 bytes",
152-
apiRespBody: readFile(t, "stream_logs_good.txt"),
153-
apiRespStatusCode: http.StatusOK,
154-
loggedBytes: 8,
155-
want: readFile(t, "stream_logs_good.txt")[8:],
156-
},
157150
{
158151
name: "error non-200 response",
159152
apiRespBody: []byte("boom"),
@@ -215,7 +208,7 @@ func TestStreamLogs(t *testing.T) {
215208
}
216209

217210
var b bytes.Buffer
218-
err := client.StreamLogs(context.Background(), &b, "workflow1", tt.loggedBytes)
211+
err := client.StreamLogs(context.Background(), &b, "workflow1")
219212

220213
if tt.wantErr != nil {
221214
assert.EqualError(t, err, tt.wantErr.Error())

0 commit comments

Comments
 (0)