Skip to content

Commit 18bd7b7

Browse files
committed
Refactor the respawnDeadLetter and support pass with the context
1 parent bdd86f0 commit 18bd7b7

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

client/client.go

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -184,64 +184,9 @@ func (c *LmstfyClient) PeekDeadLetter(queue string) (deadLetterSize int, deadLet
184184
return c.peekDeadLetter(nil, queue)
185185
}
186186

187+
// RespawnDeadLetter respawns the jobs of the given queue's dead letter
187188
func (c *LmstfyClient) RespawnDeadLetter(queue string, limit, ttlSecond int64) (count int, e *APIError) {
188-
if limit <= 0 {
189-
return 0, &APIError{
190-
Type: RequestErr,
191-
Reason: "limit should be > 0",
192-
}
193-
}
194-
if ttlSecond < 0 {
195-
return 0, &APIError{
196-
Type: RequestErr,
197-
Reason: "TTL should be >= 0",
198-
}
199-
}
200-
query := url.Values{}
201-
query.Add("limit", strconv.FormatInt(limit, 10))
202-
query.Add("ttl", strconv.FormatInt(ttlSecond, 10))
203-
req, err := c.getReq(http.MethodPut, path.Join(queue, "deadletter"), query, nil)
204-
if err != nil {
205-
return 0, &APIError{
206-
Type: RequestErr,
207-
Reason: err.Error(),
208-
}
209-
}
210-
resp, err := c.httpCli.Do(req)
211-
if err != nil {
212-
return 0, &APIError{
213-
Type: RequestErr,
214-
Reason: err.Error(),
215-
}
216-
}
217-
defer resp.Body.Close()
218-
if resp.StatusCode != http.StatusOK {
219-
return 0, &APIError{
220-
Type: ResponseErr,
221-
Reason: parseResponseError(resp),
222-
RequestID: resp.Header.Get("X-Request-ID"),
223-
}
224-
}
225-
respBytes, err := ioutil.ReadAll(resp.Body)
226-
if err != nil {
227-
return 0, &APIError{
228-
Type: ResponseErr,
229-
Reason: err.Error(),
230-
RequestID: resp.Header.Get("X-Request-ID"),
231-
}
232-
}
233-
var respData struct {
234-
Count int `json:"count"`
235-
}
236-
err = json.Unmarshal(respBytes, &respData)
237-
if err != nil {
238-
return 0, &APIError{
239-
Type: ResponseErr,
240-
Reason: err.Error(),
241-
RequestID: resp.Header.Get("X-Request-ID"),
242-
}
243-
}
244-
return respData.Count, nil
189+
return c.respawnDeadLetter(nil, queue, limit, ttlSecond)
245190
}
246191

247192
func (c *LmstfyClient) DeleteDeadLetter(queue string, limit int64) *APIError {

client/client_impl.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,67 @@ func (c *LmstfyClient) peekDeadLetter(ctx context.Context, queue string) (
692692
return respData.DeadLetterSize, respData.DeadLetterHead, nil
693693
}
694694

695+
func (c *LmstfyClient) respawnDeadLetter(ctx context.Context, queue string, limit, ttlSecond int64) (
696+
count int, e *APIError) {
697+
if limit <= 0 {
698+
return 0, &APIError{
699+
Type: RequestErr,
700+
Reason: "limit should be > 0",
701+
}
702+
}
703+
if ttlSecond < 0 {
704+
return 0, &APIError{
705+
Type: RequestErr,
706+
Reason: "TTL should be >= 0",
707+
}
708+
}
709+
query := url.Values{}
710+
query.Add("limit", strconv.FormatInt(limit, 10))
711+
query.Add("ttl", strconv.FormatInt(ttlSecond, 10))
712+
req, err := c.getReq(ctx, http.MethodPut, path.Join(queue, "deadletter"), query, nil)
713+
if err != nil {
714+
return 0, &APIError{
715+
Type: RequestErr,
716+
Reason: err.Error(),
717+
}
718+
}
719+
resp, err := c.httpCli.Do(req)
720+
if err != nil {
721+
return 0, &APIError{
722+
Type: RequestErr,
723+
Reason: err.Error(),
724+
}
725+
}
726+
defer resp.Body.Close()
727+
if resp.StatusCode != http.StatusOK {
728+
return 0, &APIError{
729+
Type: ResponseErr,
730+
Reason: parseResponseError(resp),
731+
RequestID: resp.Header.Get("X-Request-ID"),
732+
}
733+
}
734+
respBytes, err := ioutil.ReadAll(resp.Body)
735+
if err != nil {
736+
return 0, &APIError{
737+
Type: ResponseErr,
738+
Reason: err.Error(),
739+
RequestID: resp.Header.Get("X-Request-ID"),
740+
}
741+
}
742+
var respData struct {
743+
Count int `json:"count"`
744+
}
745+
err = json.Unmarshal(respBytes, &respData)
746+
if err != nil {
747+
return 0, &APIError{
748+
Type: ResponseErr,
749+
Reason: err.Error(),
750+
RequestID: resp.Header.Get("X-Request-ID"),
751+
}
752+
}
753+
return respData.Count, nil
754+
}
755+
695756
func discardResponseBody(resp io.ReadCloser) {
696757
// discard response body, to make this connection reusable in the http connection pool
697758
_, _ = ioutil.ReadAll(resp)

0 commit comments

Comments
 (0)