Skip to content

Commit 4ebecb1

Browse files
dwarren-devHarness
authored andcommitted
feat: [IAC-4932]: make tail logic more robust (#120)
* 35eb04 IAC-4932: make tail logic more robust
1 parent fd803c8 commit 4ebecb1

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

client/log-client.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func (c *LogClient) Tail(ctx context.Context, key string) error {
8585
})
8686
err = conn.Connect()
8787
if err != nil {
88-
return nil
88+
return err
8989
}
9090
return nil
9191
}
9292

93-
func (c *LogClient) Blob(ctx context.Context, key string) error {
93+
func (c *LogClient) Blob(ctx context.Context, key string) (int, error) {
9494
url, err := url.Parse(c.endpoint)
9595
if err != nil {
96-
return err
96+
return 0, err
9797
}
9898
url.Path = blobEndpoint
9999
query := url.Query()
@@ -102,42 +102,44 @@ func (c *LogClient) Blob(ctx context.Context, key string) error {
102102
url.RawQuery = query.Encode()
103103
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
104104
if err != nil {
105-
return err
105+
return 0, err
106106
}
107107
req.Header.Set(authHeaderKey, c.token)
108108
resp, err := c.client.HTTPClient.Do(req)
109109
if err != nil {
110-
return err
110+
return 0, err
111111
}
112112
body, err := io.ReadAll(resp.Body)
113113
if err != nil {
114-
return err
114+
return 0, err
115115
}
116116
if resp.StatusCode != 200 {
117117
logErr := &LogError{}
118118
if err := json.Unmarshal(body, logErr); err != nil {
119-
return err
119+
return 0, err
120120
}
121-
return logErr
121+
return 0, logErr
122122
}
123123

124-
err = readLines(string(body))
124+
lineCount, err := readLines(string(body))
125125
if err != nil {
126-
return err
126+
return 0, err
127127
}
128-
return nil
128+
return lineCount, nil
129129
}
130130

131-
func readLines(lines string) error {
131+
func readLines(lines string) (int, error) {
132+
lineCount := 0
132133
scanner := bufio.NewScanner(strings.NewReader(lines))
133134
for scanner.Scan() {
135+
lineCount += 1
134136
line, err := formatLogs(scanner.Text())
135137
if err != nil {
136-
return err
138+
return lineCount, err
137139
}
138140
fmt.Println(line)
139141
}
140-
return nil
142+
return lineCount, nil
141143
}
142144

143145
func formatLogs(line string) (string, error) {

iacm-plan.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type IacmClient interface {
5050

5151
type LogClient interface {
5252
Tail(ctx context.Context, key string) error
53-
Blob(ctx context.Context, key string) error
53+
Blob(ctx context.Context, key string) (int, error)
5454
}
5555

5656
type WorkspaceInfo struct {
@@ -384,8 +384,8 @@ func (c *IacmCommand) walkStage(ctx context.Context, executionID string, stageNo
384384
// some steps finish so quickly that the stream is closed by the time we try and
385385
// tail it. To mitigate this we try and use the blob endpoint and fall back to tail
386386
// if it throws an error
387-
err := c.logClient.Blob(ctx, getLogKeyFromStepNode(stepNode))
388-
if err != nil {
387+
lineCount, err := c.logClient.Blob(ctx, getLogKeyFromStepNode(stepNode))
388+
if err != nil || lineCount < 1 {
389389
err := c.logClient.Tail(ctx, getLogKeyFromStepNode(stepNode))
390390
if err != nil {
391391
fmt.Println(err)
@@ -404,7 +404,7 @@ func (c *IacmCommand) walkStage(ctx context.Context, executionID string, stageNo
404404
if lastStepNodeID != "" && !ok {
405405
go func() {
406406
fmt.Printf(startingStepMsg, stepNode.Name)
407-
err := c.logClient.Blob(ctx, getLogKeyFromStepNode(stepNode))
407+
_, err := c.logClient.Blob(ctx, getLogKeyFromStepNode(stepNode))
408408
if err != nil {
409409
fmt.Println(err)
410410
}

0 commit comments

Comments
 (0)