[GH-ISSUE #327] [FEATURE REQUEST] callback for archived tasks #1155

Closed
opened 2026-03-07 22:06:36 +03:00 by kerem · 3 comments
Owner

Originally created by @AlexeyBelezeko on GitHub (Sep 17, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/327

Originally assigned to: @hibiken on GitHub.

Right now asynq doesn't provide the functionality to notify go code about archived tasks. For example, I have a task for uploading images from one server to another. In case of failure, I want to retry this operation a few times and if the problem didn't fix I want to create a ticket or fire an alert.

I suggest adding some call back for archiving tasks to asynq server configuration.

Originally created by @AlexeyBelezeko on GitHub (Sep 17, 2021). Original GitHub issue: https://github.com/hibiken/asynq/issues/327 Originally assigned to: @hibiken on GitHub. Right now asynq doesn't provide the functionality to notify go code about archived tasks. For example, I have a task for uploading images from one server to another. In case of failure, I want to retry this operation a few times and if the problem didn't fix I want to create a ticket or fire an alert. I suggest adding some call back for archiving tasks to asynq server configuration.
kerem 2026-03-07 22:06:36 +03:00
Author
Owner

@hibiken commented on GitHub (Sep 17, 2021):

@AlexeyBelezeko Thank you for opening this issue!

I think this is a common need and we do have a hook ErrorHandler which gets invoked every time Handler.ProcessTask returns a non-nil error. We can use this hook to trigger alerts when task has reached its max-retry.

func handleTaskError(ctx context.Context, task *asynq.Task, err error) {
     // Note: Skip checking `ok` since we are sure that this context has these values.
     retried, _ := asynq.GetRetryCount(ctx)
     maxRetry, _ := asynq.GetMaxRetry(ctx)
     if retried >= maxRetry {
           // Task will be archived, trigger an alert
     }
     // handle errors (e.g. report error to error reporting service)
}

// ...

srv := asynq.NewServer(redisConnOpt, asynq.Config{
     ErrorHandler: asynq.ErrorHandlerFunc(handleTaskError),
     // ... other config options 
})

Let me know if this works!

<!-- gh-comment-id:921738861 --> @hibiken commented on GitHub (Sep 17, 2021): @AlexeyBelezeko Thank you for opening this issue! I think this is a common need and we do have a hook [`ErrorHandler`](https://pkg.go.dev/github.com/hibiken/asynq#Config.ErrorHandler) which gets invoked every time `Handler.ProcessTask` returns a non-nil error. We can use this hook to trigger alerts when task has reached its max-retry. ```go func handleTaskError(ctx context.Context, task *asynq.Task, err error) { // Note: Skip checking `ok` since we are sure that this context has these values. retried, _ := asynq.GetRetryCount(ctx) maxRetry, _ := asynq.GetMaxRetry(ctx) if retried >= maxRetry { // Task will be archived, trigger an alert } // handle errors (e.g. report error to error reporting service) } // ... srv := asynq.NewServer(redisConnOpt, asynq.Config{ ErrorHandler: asynq.ErrorHandlerFunc(handleTaskError), // ... other config options }) ``` Let me know if this works!
Author
Owner

@AlexeyBelezeko commented on GitHub (Sep 17, 2021):

@hibiken thank you for a quick answer. This is exactly what I've been looking for. Sorry for my blindness I found out that the same snippet contains in the ErrorHandler comment section.

<!-- gh-comment-id:921765956 --> @AlexeyBelezeko commented on GitHub (Sep 17, 2021): @hibiken thank you for a quick answer. This is exactly what I've been looking for. Sorry for my blindness I found out that the same snippet contains in the ErrorHandler comment section.
Author
Owner

@hibiken commented on GitHub (Sep 17, 2021):

Glad I could help!

No worries. I appreciate these issues because it's a signal that I can do a better job in documentation.

<!-- gh-comment-id:922110040 --> @hibiken commented on GitHub (Sep 17, 2021): Glad I could help! No worries. I appreciate these issues because it's a signal that I can do a better job in documentation.
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#1155
No description provided.