[GH-ISSUE #362] [FEATURE REQUEST] On fail cleanup task #2183

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

Originally created by @sersh88 on GitHub (Dec 8, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/362

Originally assigned to: @hibiken on GitHub.

For some tasks I need to prepare some files and need to clean them if task failed after all defined attempts.
So, it will be very useful to define a cleanup task for a task to run after it's failed. It can be done as passing option to NewTask method.
So, after our original task is failed a new cleanup task will be automatically created with the same payload or maybe with custom payload.

Originally created by @sersh88 on GitHub (Dec 8, 2021). Original GitHub issue: https://github.com/hibiken/asynq/issues/362 Originally assigned to: @hibiken on GitHub. For some tasks I need to prepare some files and need to clean them if task failed after all defined attempts. So, it will be very useful to define a cleanup task for a task to run after it's failed. It can be done as passing option to NewTask method. So, after our original task is failed a new cleanup task will be automatically created with the same payload or maybe with custom payload.
kerem 2026-03-15 19:35:19 +03:00
Author
Owner

@crossworth commented on GitHub (Dec 8, 2021):

You can implement this in different ways:

  1. Using middlewares
  2. Using the error handler

You can define a custom error and check it on the middleware/error handler (or check the task type) and enqueue the cleanup task.

Maybe it will be possible with the workflow feature as well (https://github.com/hibiken/asynq/issues/244).

<!-- gh-comment-id:988745385 --> @crossworth commented on GitHub (Dec 8, 2021): You can implement this in different ways: 1. [Using middlewares](https://github.com/hibiken/asynq/wiki/Handler-Deep-Dive) 2. [Using the error handler](https://pkg.go.dev/github.com/hibiken/asynq?utm_source=godoc#Config.ErrorHandler) You can define a custom error and check it on the middleware/error handler (or check the task type) and enqueue the cleanup task. Maybe it will be possible with the workflow feature as well (https://github.com/hibiken/asynq/issues/244).
Author
Owner

@sersh88 commented on GitHub (Dec 8, 2021):

But error handler and middleware ProcessTask will react on every retry, right? So, I can't know if this was the last attempt to run the task or not?

<!-- gh-comment-id:988864875 --> @sersh88 commented on GitHub (Dec 8, 2021): But error handler and middleware ProcessTask will react on every retry, right? So, I can't know if this was the last attempt to run the task or not?
Author
Owner
<!-- gh-comment-id:988866055 --> @crossworth commented on GitHub (Dec 8, 2021): https://pkg.go.dev/github.com/hibiken/asynq#GetRetryCount and https://pkg.go.dev/github.com/hibiken/asynq#GetMaxRetry
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#2183
No description provided.