Skip to content
Ken Hibino edited this page Apr 27, 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 queueing a task.

Example:

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

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

Alternatively, if you want to specify maximum retry count for some task type, you can set it as a default option.

client.SetDefaultOptions("feed:import", asynq.MaxRetry(5))

task := asynq.NewTask("feed:import, nil)
client.Enqueue(task) // MaxRetry is set to 5

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.