Skip to content

Commit

Permalink
fix(retries): add sleep between retries and improve error messages
Browse files Browse the repository at this point in the history
Signed-off-by: mudler <[email protected]>
  • Loading branch information
mudler committed Nov 22, 2024
1 parent c5e4c40 commit e315437
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -26,6 +25,10 @@ func NewClient(baseURL string) *Client {
}
}

// TODO: When submitting a job return a Job result
// JobResult represents a client to interact with the job server and get the result asyncronously
// The job result should be capable of waiting of the result, decrypting it and unmarshalling to a struct which is passed by (expected JSON response from the jobs)

// SubmitJob submits a new job to the server and returns the job UID.
func (c *Client) SubmitJob(job types.Job) (string, error) {
jobJSON, err := json.Marshal(job)
Expand Down Expand Up @@ -118,14 +121,15 @@ func (c *Client) WaitForResult(jobID string, maxRetries int, delay time.Duration

for {
if retries >= maxRetries {
return "", errors.New("max retries reached")
return "", fmt.Errorf("max retries reached: %w", err)
}
retries++

result, err = c.GetJobResult(jobID)
if err == nil {
break
}
time.Sleep(1 * time.Second)
}

return
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ var _ = Describe("Client", func() {
server.Close() // simulate unavailability
result, err := c.WaitForResult("job1", 3, time.Millisecond*10)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("max retries reached"))
Expect(err.Error()).To(ContainSubstring("max retries reached"))
Expect(result).To(BeEmpty())
})
})
Expand Down

0 comments on commit e315437

Please sign in to comment.