[GH-ISSUE #216] [Question] Support like release? How can I specify a retry delay? #1092

Closed
opened 2026-03-07 22:05:34 +03:00 by kerem · 2 comments
Owner

Originally created by @dishcheng on GitHub (Dec 11, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/216

Originally assigned to: @hibiken on GitHub.

In laravel queue

   public function handle()
    {
         if($condition){
            //do something
        }else{
            //push the message back to queue,and exec after 60 seconds
            $this->release(60);
        }
    }

How can I do like release?

Originally created by @dishcheng on GitHub (Dec 11, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/216 Originally assigned to: @hibiken on GitHub. In laravel queue ``` public function handle() { if($condition){ //do something }else{ //push the message back to queue,and exec after 60 seconds $this->release(60); } } ``` How can I do like release?
kerem 2026-03-07 22:05:34 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@hibiken commented on GitHub (Dec 11, 2020):

Hi @dishcheng, thank you for the question!

You can define a function to compute the retry delay and specify that in the Config.RetryDelayFunc.
The function has the following signature:

// n is the number of times the task has been retried.
// e is the error returned by the task handler.
// t is the task in question.
func(n int, e error, t *Task) time.Duration

One way to specify the delay from the Handler is to include this data in the error itself.
Example:

type ErrRetryLater struct {
      delay time.Duration
}

// In Handler
func myHandler(ctx context.Context, task *asynq.Task) error {
    if /* does not meet precondition */ {
         return ErrRelayLater{delay: 60 * time.Second}
    }
   //  do something with the task
}

// and define your RetryDelayFunc
func computeRetryDelay(n int, err error, t *asynq.Task) time.Duration {
     var retryLater ErrRetryLater
     if errors.As(err, &retryLater) {
         return retryLater.delay
     }
    return 1 * time.Minute // otherwise retry in 1 min
}

// ....

// Tell the server to use a custom RetryDelayFunc
srv := asynq.NewServer(redisConnOpt, asynq.Config{
    RetryDelayFunc: computeRetryDelay,
    // ... other configs
})

if err := srv.Run(handler); err != nil {
    log.Fatal(err)
}

Let me know if it's not clear, or if you have suggestions around the API.

<!-- gh-comment-id:743233836 --> @hibiken commented on GitHub (Dec 11, 2020): Hi @dishcheng, thank you for the question! You can define a function to compute the retry delay and specify that in the [`Config.RetryDelayFunc`](https://pkg.go.dev/github.com/hibiken/asynq#Config). The function has the following signature: ```go // n is the number of times the task has been retried. // e is the error returned by the task handler. // t is the task in question. func(n int, e error, t *Task) time.Duration ``` --- One way to specify the delay from the Handler is to include this data in the error itself. Example: ```go type ErrRetryLater struct { delay time.Duration } // In Handler func myHandler(ctx context.Context, task *asynq.Task) error { if /* does not meet precondition */ { return ErrRelayLater{delay: 60 * time.Second} } // do something with the task } // and define your RetryDelayFunc func computeRetryDelay(n int, err error, t *asynq.Task) time.Duration { var retryLater ErrRetryLater if errors.As(err, &retryLater) { return retryLater.delay } return 1 * time.Minute // otherwise retry in 1 min } // .... // Tell the server to use a custom RetryDelayFunc srv := asynq.NewServer(redisConnOpt, asynq.Config{ RetryDelayFunc: computeRetryDelay, // ... other configs }) if err := srv.Run(handler); err != nil { log.Fatal(err) } ``` Let me know if it's not clear, or if you have suggestions around the API.
Author
Owner

@dishcheng commented on GitHub (Dec 11, 2020):

I got it , thank you so much!

<!-- gh-comment-id:743252663 --> @dishcheng commented on GitHub (Dec 11, 2020): I got it , thank you so much!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#1092
No description provided.