Skip to content
Ken Hibino edited this page Apr 19, 2020 · 10 revisions

This page explains how to configure task retries.

Default behavior

By default, asynq will retry a given task up to 25 times. Every time a task is retried it uses an exponential backoff algorithm to calculate the retry delay. If a task exhausts all of its retry count (default: 25), then the task will transition to the dead state and won't be retried.

Customize Task Retry

You can specify how many times a task can be retried using asynq.MaxRetry option when scheduling a task.

Example:

client.Enqueue(task, asynq.MaxRetry(5))

This specifies that the task should be retired up to five times.

Customize Retry Delay

You can specify how to calculate retry delay using RetryDelayFunc option in Config.

Signature of RetryDelayFunc:

// 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
RetryDelayFunc func(n int, e error, t *asynq.Task) time.Duration

Example:

srv := asynq.NewServer(redis, asynq.Config{
    Concurrency: 20,
    RetryDelayFunc: func(n int, e error, t *asynq.Task) time.Duration {
        return 2 * time.Second
    },
})

This specifies that all failed task will wait two seconds before being processed again.