Skip to content

Commit a4e9fb9

Browse files
vistaarjunejaHarness
authored andcommitted
wait for log downloading to complete before returning link (#19)
* make download logs call async
1 parent 55d47b8 commit a4e9fb9

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

client/logs.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/harness/harness-mcp/client/dto"
89
)
@@ -52,10 +53,39 @@ func (l *LogService) DownloadLogs(ctx context.Context, scope dto.Scope, planExec
5253
// Initialize the response object
5354
response := &dto.LogDownloadResponse{}
5455

55-
// Make the POST request
56-
err = l.Client.Post(ctx, logDownloadPath, params, nil, response)
57-
if err != nil {
58-
return "", fmt.Errorf("failed to fetch log download URL: %w", err)
56+
// Make the POST request with retry logic
57+
maxAttempts := 3
58+
timeBetweenAttempts := 3 * time.Second
59+
successObtained := false
60+
var lastErr error
61+
62+
for attempt := 1; attempt <= maxAttempts; attempt++ {
63+
response = &dto.LogDownloadResponse{}
64+
err = l.Client.Post(ctx, logDownloadPath, params, nil, response)
65+
if err != nil {
66+
lastErr = fmt.Errorf("failed to fetch log download URL (attempt %d): %w", attempt, err)
67+
if attempt == maxAttempts {
68+
break
69+
}
70+
time.Sleep(timeBetweenAttempts)
71+
continue
72+
}
73+
74+
if response.Status == "success" {
75+
successObtained = true
76+
break
77+
}
78+
79+
if attempt == maxAttempts {
80+
lastErr = fmt.Errorf("status did not become success after %d attempts, last status: %s", maxAttempts, response.Status)
81+
break
82+
}
83+
84+
time.Sleep(timeBetweenAttempts)
85+
}
86+
87+
if !successObtained {
88+
return "", lastErr
5989
}
6090

6191
return response.Link, nil

0 commit comments

Comments
 (0)