Skip to content

Commit

Permalink
fix(worker): improve error message + fix retry on cdnDownloadItem (#5838
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sguiheux authored Jun 9, 2021
1 parent 65c6e95 commit 70a24b0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
18 changes: 12 additions & 6 deletions engine/worker/cmd_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ func cachePushCmd() func(cmd *cobra.Command, args []string) {
if resp.StatusCode >= 300 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
sdk.Exit("cache push HTTP error %v", err)
sdk.Exit("cache push HTTP %d error %v", resp.StatusCode, err)
}
cdsError := sdk.DecodeError(body)
sdk.Exit("Error: http code %d : %v", resp.StatusCode, cdsError)
var sdkErr sdk.Error
if json.Unmarshal(body, &sdkErr); err != nil {
sdk.Exit("unable to read error: %s: %v", string(body), err)
}
sdk.Exit("%v", sdkErr)
}

fmt.Printf("Worker cache push with success (tag: %s)\n", args[0])
Expand Down Expand Up @@ -230,10 +233,13 @@ func cachePullCmd() func(cmd *cobra.Command, args []string) {
if resp.StatusCode >= 300 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
sdk.Exit("cache pull HTTP error %v", err)
sdk.Exit("cache pull HTTP %d error %v", resp.StatusCode, err)
}
var sdkErr sdk.Error
if json.Unmarshal(body, &sdkErr); err != nil {
sdk.Exit("unable to read error: %s: %v", string(body), err)
}
cdsError := sdk.DecodeError(body)
sdk.Exit("Error: %v", cdsError)
sdk.Exit("%v", sdkErr)
}

fmt.Printf("Worker cache pull with success (tag: %s)\n", args[0])
Expand Down
2 changes: 1 addition & 1 deletion engine/worker/internal/handler_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func cachePullHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
r, err = wk.client.CDNItemDownload(ctx, wk.cdnHttpAddr, items.Items[0].APIRefHash, sdk.CDNTypeItemWorkerCache)
if err != nil {
err = sdk.Error{
Message: "worker cache pull > Cannot pull cache: " + err.Error(),
Message: "Cannot pull cache: " + err.Error(),
Status: http.StatusNotFound,
}
log.Error(ctx, "%v", err)
Expand Down
15 changes: 11 additions & 4 deletions sdk/cdsclient/client_cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cdsclient

import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -18,16 +19,22 @@ func (c *client) CDNItemDownload(ctx context.Context, cdnAddr string, hash strin
auth := "Bearer " + c.config.SessionToken
req.Header.Add("Authorization", auth)
})
if err != nil {
return nil, err
}
if code >= 400 {
var stringBody string
if reader != nil {
body, _ := ioutil.ReadAll(reader)
if err := sdk.DecodeError(body); err != nil {
return nil, err
var errSdk sdk.Error
if err := json.Unmarshal(body, &errSdk); err == nil && errSdk.Message != "" {
stringBody = errSdk.Error()
}
if stringBody == "" {
stringBody = string(body)
}
stringBody = string(body)
}
return nil, newAPIError(fmt.Errorf("HTTP %d: %s", code, stringBody))
return nil, newAPIError(fmt.Errorf("%s", stringBody))
}
return reader, err
}
Expand Down
9 changes: 1 addition & 8 deletions sdk/cdsclient/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,11 @@ func (c *client) Stream(ctx context.Context, httpClient HTTPClient, method strin
c.config.SessionToken = ""
}

if resp.StatusCode == 409 || resp.StatusCode > 500 {
if resp.StatusCode == 409 || resp.StatusCode >= 500 {
time.Sleep(250 * time.Millisecond)
savederror = extractBodyErrorFromResponse(resp)
continue
}

// if no request error by status > 500, check CDS error
// if there is a CDS errors, return it
if resp.StatusCode == 500 {
return nil, resp.Header, resp.StatusCode, extractBodyErrorFromResponse(resp)
}

return resp.Body, resp.Header, resp.StatusCode, nil
}

Expand Down
1 change: 0 additions & 1 deletion sdk/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ func ExtractHTTPError(source error) Error {
if httpError.Message == "" {
httpError.Message = httpError.Translate()
}

return httpError
}

Expand Down

0 comments on commit 70a24b0

Please sign in to comment.