Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Way to determine a task is a retry #154

Closed
jimmy-ho opened this issue May 26, 2020 · 3 comments · Fixed by #156
Closed

[QUESTION] Way to determine a task is a retry #154

jimmy-ho opened this issue May 26, 2020 · 3 comments · Fixed by #156
Assignees
Labels
enhancement New feature or request

Comments

@jimmy-ho
Copy link

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?

@hibiken
Copy link
Owner

hibiken commented 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 👍

@hibiken
Copy link
Owner

hibiken commented 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
}

@jimmy-ho
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants