[GH-ISSUE #172] [FEATURE REQUEST] Support cancelation of scheduled tasks #52

Closed
opened 2026-03-02 05:18:16 +03:00 by kerem · 3 comments
Owner

Originally created by @kamatama41 on GitHub (Jun 15, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/172

Originally assigned to: @hibiken on GitHub.

Is your feature request related to a problem? Please describe.
We sometimes want to cancel specific tasks.

Describe the solution you'd like
Add a method to cancel them to the asynq client.
Maybe need a method to list all scheduled tasks as well in order to know their ID.
(ref #50)

Originally created by @kamatama41 on GitHub (Jun 15, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/172 Originally assigned to: @hibiken on GitHub. **Is your feature request related to a problem? Please describe.** We sometimes want to cancel specific tasks. **Describe the solution you'd like** Add a method to cancel them to the asynq client. Maybe need a method to list all scheduled tasks as well in order to know their ID. (ref #50)
kerem 2026-03-02 05:18:16 +03:00
Author
Owner

@hibiken commented on GitHub (Jun 15, 2020):

@kamatama41 Thank you for opening this issue!

As you said, #50 is related to this. When I opened #50 I was thinking about adding a public sub-package (e.g. "github.com/hibiken/asynq/x/inspect") and expose methods like

  • ListScheduledTasks
  • DeleteScheduledTask(id string)

from the sub-package. But I can see the benefit of adding directly to Client as you suggested. I'll consider both options when I get to work on #50 👍

In the meantime, you have a few options to cancel scheduled tasks.

Option 1 : Use asynq CLI to list and delete scheduled tasks

You can use the CLI ls and del commands.

asynq ls scheduled # this will output list of schedule tasks with IDs
asynq del <task id> # grab task ID from the previous command output

Option 2: Use Deadline option when enqueueing

If your tasks has specific deadline, you can use Deadline option when enqueueing the tasks.

client := asynq.NewClient(r)
client.Enqueue(mytask, asynq.Deadline(time.Date(/* deadline time */)))

With this approach, you need to make sure that your Handler checks the passed context Done channel and return an error if Done channel is closed. Please see an example here.


Please let me know if this workaround works for you. If not, I can prioritize #50. Thank you :)

<!-- gh-comment-id:644214466 --> @hibiken commented on GitHub (Jun 15, 2020): @kamatama41 Thank you for opening this issue! As you said, #50 is related to this. When I opened #50 I was thinking about adding a public sub-package (e.g. `"github.com/hibiken/asynq/x/inspect"`) and expose methods like - `ListScheduledTasks` - `DeleteScheduledTask(id string)` from the sub-package. But I can see the benefit of adding directly to `Client` as you suggested. I'll consider both options when I get to work on #50 👍 In the meantime, you have a few options to cancel scheduled tasks. **Option 1 : Use asynq CLI to list and delete scheduled tasks** You can use the CLI `ls` and `del` commands. ```bash asynq ls scheduled # this will output list of schedule tasks with IDs asynq del <task id> # grab task ID from the previous command output ``` **Option 2: Use `Deadline` option when enqueueing** If your tasks has specific deadline, you can use `Deadline` option when enqueueing the tasks. ```go client := asynq.NewClient(r) client.Enqueue(mytask, asynq.Deadline(time.Date(/* deadline time */))) ``` With this approach, you need to make sure that your `Handler` checks the passed context Done channel and return an error if Done channel is closed. Please see an example [here](https://github.com/hibiken/asynq/wiki/Task-Timeout-and-Cancelation). --- Please let me know if this workaround works for you. If not, I can prioritize #50. Thank you :)
Author
Owner

@kamatama41 commented on GitHub (Jun 15, 2020):

Thanks for the quick response and suggesting workarounds!

Option 2 is not for us because the reason for cancelation is that the task is no longer needed or with wrong parameter, so it doesn't have specific deadline.

About option 1, it would work for us. However, as we are using asynq client on a Go application, we eventually would like to cancel tasks on the app via client.

<!-- gh-comment-id:644378306 --> @kamatama41 commented on GitHub (Jun 15, 2020): Thanks for the quick response and suggesting workarounds! Option 2 is not for us because the reason for cancelation is that the task is no longer needed or with wrong parameter, so it doesn't have specific deadline. About option 1, it would work for us. However, as we are using asynq client on a Go application, we eventually would like to cancel tasks on the app via client.
Author
Owner

@hibiken commented on GitHub (Jun 17, 2020):

I see. Thanks for sharing context :)

What you are looking for is actually implemented but they are not part of public API yet. It's in one of our internal packages called "rdb" and inspect.go file includes all the methods to inspect and modify current state of queues and tasks.

Here's the link to the methods that may help you.

ListScheduledTask

DeleteScheduledTask

At this point I'm still a bit hesitant to make these public API from the core package.
My current thinking is to expose this via "experimental" package under github.com/hibiken/asynq/x so that we don't have to worry too much about making API changes in the experimental package.

I can probably get to that in the next few weeks, but in the meantime please feel free to fork and use these methods 👍

<!-- gh-comment-id:645117068 --> @hibiken commented on GitHub (Jun 17, 2020): I see. Thanks for sharing context :) What you are looking for is actually implemented but they are not part of public API yet. It's in one of our internal packages called `"rdb"` and `inspect.go` file includes all the methods to inspect and modify current state of queues and tasks. Here's the link to the methods that may help you. [`ListScheduledTask`](https://github.com/hibiken/asynq/blob/master/internal/rdb/inspect.go#L352) [`DeleteScheduledTask`](https://github.com/hibiken/asynq/blob/master/internal/rdb/inspect.go#L677) At this point I'm still a bit hesitant to make these public API from the core package. My current thinking is to expose this via "experimental" package under `github.com/hibiken/asynq/x` so that we don't have to worry too much about making API changes in the experimental package. I can probably get to that in the next few weeks, but in the meantime please feel free to fork and use these methods 👍
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#52
No description provided.