[GH-ISSUE #203] [FEATURE REQUEST] Is it possible to trigger a callback when status of a task is changed? #1084

Closed
opened 2026-03-07 22:05:25 +03:00 by kerem · 5 comments
Owner

Originally created by @sujit-baniya on GitHub (Oct 10, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/203

Originally assigned to: @hibiken on GitHub.

Is it possible to trigger a function whenever status of a task is changed?

For e.g. trigger a function when task A changed it's status from PENDING to ACTIVE

Similar stuff for queue and workers:
If queue and/or workers are idle for specific time period, stop or kill such queue and/or workers to restore the resources

For e.g.

type TaskMessage struct {
        ...
	Status string

	OnNew       func(i interface{}) error
	OnPending   func(i interface{}) error
	OnPause   func(i interface{}) error
	OnResume   func(i interface{}) error
	OnActive    func(i interface{}) error
	OnRetry     func(i interface{}) error
	OnScheduled func(i interface{}) error
	OnComplete  func(i interface{}) error
	OnKill      func(i interface{}) error
	OnDead      func(i interface{}) error
}

Is there something I could do?

Originally created by @sujit-baniya on GitHub (Oct 10, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/203 Originally assigned to: @hibiken on GitHub. Is it possible to trigger a function whenever status of a task is changed? For e.g. trigger a function when task A changed it's status from `PENDING` to `ACTIVE` Similar stuff for queue and workers: If queue and/or workers are idle for specific time period, stop or kill such queue and/or workers to restore the resources For e.g. ```go type TaskMessage struct { ... Status string OnNew func(i interface{}) error OnPending func(i interface{}) error OnPause func(i interface{}) error OnResume func(i interface{}) error OnActive func(i interface{}) error OnRetry func(i interface{}) error OnScheduled func(i interface{}) error OnComplete func(i interface{}) error OnKill func(i interface{}) error OnDead func(i interface{}) error } ``` Is there something I could do?
kerem 2026-03-07 22:05:25 +03:00
Author
Owner

@hibiken commented on GitHub (Oct 11, 2020):

Thanks for opening this issue.

It's an interesting feature request, but with the current implementation this change will most likely degrade the performance significantly (e.g. we would need to deserialize the task message whenever there's a status change). I'm hesitant to make this change at this moment, but you can fork the repo and make appropriate changes in the internal/rdb package where task messages are managed in redis.
I will keep this issue open to see if this is a popular feature request.

As for the second question:

If queue and/or workers are idle for specific time period, stop or kill such queue and/or workers to restore the resources

That's why we have Timeout and Deadline option. You can read about it in this wiki page.

<!-- gh-comment-id:706738709 --> @hibiken commented on GitHub (Oct 11, 2020): Thanks for opening this issue. It's an interesting feature request, but with the current implementation this change will most likely degrade the performance significantly (e.g. we would need to deserialize the task message whenever there's a status change). I'm hesitant to make this change at this moment, but you can fork the repo and make appropriate changes in the `internal/rdb` package where task messages are managed in redis. I will keep this issue open to see if this is a popular feature request. As for the second question: > If queue and/or workers are idle for specific time period, stop or kill such queue and/or workers to restore the resources That's why we have `Timeout` and `Deadline` option. You can read about it in [this wiki page](https://github.com/hibiken/asynq/wiki/Task-Timeout-and-Cancelation).
Author
Owner

@sujit-baniya commented on GitHub (Oct 12, 2020):

@hibiken Thanks for the response. I wonder those options are for Task and not Workers or Queues.

<!-- gh-comment-id:706807061 --> @sujit-baniya commented on GitHub (Oct 12, 2020): @hibiken Thanks for the response. I wonder those options are for Task and not Workers or Queues.
Author
Owner

@hibiken commented on GitHub (Oct 13, 2020):

Yes, the timeout and deadline are specific to each task. A worker has limited time to process a task, if the worker cannot complete its task within the timeout (or before the deadline), the task will be retried later and the worker is abandoned to reclaim resources.

I'm not quite sure what you mean by

I wonder those options are for Task and not Workers or Queues.

Could you explain this in more detail?

<!-- gh-comment-id:707747808 --> @hibiken commented on GitHub (Oct 13, 2020): Yes, the timeout and deadline are specific to each task. A worker has limited time to process a task, if the worker cannot complete its task within the timeout (or before the deadline), the task will be retried later and the worker is abandoned to reclaim resources. I'm not quite sure what you mean by > I wonder those options are for Task and not Workers or Queues. Could you explain this in more detail?
Author
Owner

@sujit-baniya commented on GitHub (Oct 13, 2020):

For e.g.

application created workerA on QueueA for 100K jobs. After 10 mins all jobs are completed. Now I need remove the workerA and QueueA from server after waiting or being idle for specific time period

<!-- gh-comment-id:707768526 --> @sujit-baniya commented on GitHub (Oct 13, 2020): For e.g. application created workerA on QueueA for 100K jobs. After 10 mins all jobs are completed. Now I need remove the workerA and QueueA from server after waiting or being idle for specific time period
Author
Owner

@hibiken commented on GitHub (Feb 1, 2021):

Currently I'm not intending to support this feature. I'll close this issue for now.

<!-- gh-comment-id:770611237 --> @hibiken commented on GitHub (Feb 1, 2021): Currently I'm not intending to support this feature. I'll close this issue for now.
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#1084
No description provided.