From e33cd5b49160b486c30b23784f8346fcb8fedcf8 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Wed, 1 Jun 2022 11:48:48 -0700 Subject: [PATCH] fix retryable in Next poller --- client.go | 20 ++++++++++---------- consumer/consumer.go | 23 +---------------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/client.go b/client.go index 036d101..c15514b 100644 --- a/client.go +++ b/client.go @@ -236,18 +236,18 @@ func (r *Client[P, S]) Next(ctx context.Context, queue string, num_jobs uint32) return helpers, nil default: - if resp.StatusCode >= 400 && resp.StatusCode < 500 { - return nil, errors.Newf("invalid request, status code: %d", resp.StatusCode) + if resp.StatusCode == http.StatusNoContent || httpext.IsRetryableStatusCode(resp.StatusCode) { + // includes http.StatusNoContent and http.TooManyRequests + // no new jobs to process + if err := r.nextBo.Sleep(ctx, attempt); err != nil { + // only context.Cancel as error ever + return nil, err + } + attempt++ + continue } - // includes http.StatusNoContent and http.TooManyRequests - // no new jobs to process - if err := r.nextBo.Sleep(ctx, attempt); err != nil { - // only context.Cancel as error ever - return nil, err - } - attempt++ - continue + return nil, errors.Newf("invalid request, status code: %d", resp.StatusCode) } } } diff --git a/consumer/consumer.go b/consumer/consumer.go index e9492f8..a557ff9 100644 --- a/consumer/consumer.go +++ b/consumer/consumer.go @@ -206,28 +206,7 @@ func (c *Consumer[P, S, T]) process(ctx context.Context, helper *relay.JobHelper } if c.autoComplete { - return c.complete(ctx, helper) + return helper.CompleteWithRetry(ctx) } return nil } - -func (c *Consumer[P, S, T]) complete(ctx context.Context, helper *relay.JobHelper[P, S]) (err error) { - var attempt int - for { - if attempt > 0 { - if err = c.bo.Sleep(ctx, attempt); err != nil { - // can only happen if context cancelled or timed out - return err - } - } - err = helper.Complete(ctx) - if err != nil { - if _, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable { - attempt++ - continue - } - return errors.Wrap(err, "failed to complete Job") - } - return nil - } -}