[GH-ISSUE #154] [QUESTION] Way to determine a task is a retry #2069

Closed
opened 2026-03-15 19:01:03 +03:00 by kerem · 3 comments
Owner

Originally created by @jimmy-ho on GitHub (May 26, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/154

Originally assigned to: @hibiken on GitHub.

Is there an easy way to determine if the current task being processed is a retry? It is not in the context I guess passed to ProcessTask, would be nice to have it in the Task perhaps.

This is useful if I have already completed some set of action, but then need to retry some aspect that failed - unless I need to store some relationship that a particular task failed and thus subsequent will be a retry?

I prefer I do not need to store this, as I see in redis we already are storing the number of retries which can be used as a determinant.

Maybe I have missed some means to be able to just get this information somehow?

Originally created by @jimmy-ho on GitHub (May 26, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/154 Originally assigned to: @hibiken on GitHub. Is there an easy way to determine if the current task being processed is a retry? It is not in the context I guess passed to ProcessTask, would be nice to have it in the Task perhaps. This is useful if I have already completed some set of action, but then need to retry some aspect that failed - unless I need to store some relationship that a particular task failed and thus subsequent will be a retry? I prefer I do not need to store this, as I see in redis we already are storing the number of retries which can be used as a determinant. Maybe I have missed some means to be able to just get this information somehow?
kerem 2026-03-15 19:01:03 +03:00
Author
Owner

@hibiken commented on GitHub (May 26, 2020):

Great question and thanks for opening this issue.

Current APIs don't offer an ability to determine whether a Task passed to your Handler is a retry task.

I think it makes total sense to put task metadata such as RetryCount in the context Value.
I'll do a little bit of research and update this thread with a proposal 👍

<!-- gh-comment-id:634325962 --> @hibiken commented on GitHub (May 26, 2020): Great question and thanks for opening this issue. Current APIs don't offer an ability to determine whether a `Task` passed to your `Handler` is a retry task. I think it makes total sense to put task metadata such as `RetryCount` in the `context` Value. I'll do a little bit of research and update this thread with a proposal 👍
Author
Owner

@hibiken commented on GitHub (May 27, 2020):

Proposal

Here's my initial proposal. Please let me know if you have any thoughts or feedback 😃

Export functions to extract task metadata from context:

GetTaskID(ctx context.Context) (id string, ok bool)

GetRetryCount(ctx context.Context) (n int, ok bool)

GetMaxRetry(ctx context.Context) (n int, ok bool)

Example

func (h *MyHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
    retried, ok := asynq.GetRetryCount(ctx)
    if ok {
        log.Printf("this task has been retried for %d times", retried)
    }
    
    // ... rest of the handler logic
}
<!-- gh-comment-id:634422025 --> @hibiken commented on GitHub (May 27, 2020): ## Proposal Here's my initial proposal. Please let me know if you have any thoughts or feedback 😃 Export functions to extract task metadata from context: `GetTaskID(ctx context.Context) (id string, ok bool)` `GetRetryCount(ctx context.Context) (n int, ok bool)` `GetMaxRetry(ctx context.Context) (n int, ok bool)` ### Example ```go func (h *MyHandler) ProcessTask(ctx context.Context, t *asynq.Task) error { retried, ok := asynq.GetRetryCount(ctx) if ok { log.Printf("this task has been retried for %d times", retried) } // ... rest of the handler logic } ```
Author
Owner

@jimmy-ho commented on GitHub (May 27, 2020):

@hibiken looks good to me, thanks for your time in looking into this.

<!-- gh-comment-id:634581132 --> @jimmy-ho commented on GitHub (May 27, 2020): @hibiken looks good to me, thanks for your time in looking into this.
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#2069
No description provided.