[GH-ISSUE #259] [FEATURE REQUEST] Can I return TaskID when calling asynq.NewTask ? #95

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

Originally created by @gopkg-dev on GitHub (Apr 2, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/259

Originally assigned to: @hibiken on GitHub.

Because the task is consumed soon after I am in Enqueue.
I want to save the taskid to the database before joining the queue.
Since Enqueue will only return after the call, it may be consumed before the taskid is saved to the database.

My English is not good, I hope you can understand what I mean.

Originally created by @gopkg-dev on GitHub (Apr 2, 2021). Original GitHub issue: https://github.com/hibiken/asynq/issues/259 Originally assigned to: @hibiken on GitHub. Because the task is consumed soon after I am in Enqueue. I want to save the taskid to the database before joining the queue. Since Enqueue will only return after the call, it may be consumed before the taskid is saved to the database. My English is not good, I hope you can understand what I mean.
kerem 2026-03-02 05:18:37 +03:00
Author
Owner

@hibiken commented on GitHub (Apr 3, 2021):

Thank you for opening this issue!

It's not difficult to make this change, but since it changes the behavior (i.e. breaking change) I want to understand the context in more detail.

Could you elaborate the issue in more detail?
Also, as a workaround, if you want the task not to be processed immediately after enqueue, can you use ProcessIn option to delay the processing?

Example:

res, err := client.Enqueue(task, asynq.ProcessIn(1 * time.Minute))
<!-- gh-comment-id:812905835 --> @hibiken commented on GitHub (Apr 3, 2021): Thank you for opening this issue! It's not difficult to make this change, but since it changes the behavior (i.e. breaking change) I want to understand the context in more detail. Could you elaborate the issue in more detail? Also, as a workaround, if you want the task not to be processed immediately after enqueue, can you use `ProcessIn` option to delay the processing? Example: ```go res, err := client.Enqueue(task, asynq.ProcessIn(1 * time.Minute)) ```
Author
Owner

@gopkg-dev commented on GitHub (Apr 4, 2021):

Obviously this is not a good solution.
I need to monitor and manage the status of a task in my database.
But I can only get the taskid after'client.Enqueue' and save it.

It's like this now
asynq.NewTask -> client.Enqueue -> save taskid to db -> ProcessTask -> asynq.GetTaskID -> update taskid status to db

I hope it is like this
asynq.NewTask -> save taskid to db -> client.Enqueue -> ProcessTask -> asynq.GetTaskID -> update taskid status to db

<!-- gh-comment-id:812996464 --> @gopkg-dev commented on GitHub (Apr 4, 2021): Obviously this is not a good solution. I need to monitor and manage the status of a task in my database. But I can only get the taskid after'client.Enqueue' and save it. It's like this now `asynq.NewTask -> client.Enqueue -> save taskid to db -> ProcessTask -> asynq.GetTaskID -> update taskid status to db` I hope it is like this `asynq.NewTask -> save taskid to db -> client.Enqueue -> ProcessTask -> asynq.GetTaskID -> update taskid status to db`
Author
Owner

@hibiken commented on GitHub (Apr 4, 2021):

I see. Actually I'm currently working on a feature to track task status but it'll only be available in the future release.

In the meantime, I have another suggestion which I think is a reasonable workaround.
Instead of relying on ID generated by asynq package, you could generate a unique ID for each task and store it as part of the task's payload.

Example using uuid package:

id := uuid.NewString()
task := asynq.NewTask("mytask", map[string]interface{}{"task_id": id})

// Save ID to your application database.

// Enqueue task

Also, I'll consider making task ID available after calling NewTask in the future release 👍

<!-- gh-comment-id:813086654 --> @hibiken commented on GitHub (Apr 4, 2021): I see. Actually I'm currently working on a feature to track task status but it'll only be available in the future release. In the meantime, I have another suggestion which I think is a reasonable workaround. Instead of relying on ID generated by `asynq` package, you could generate a unique ID for each task and store it as part of the task's payload. Example using [`uuid`](https://pkg.go.dev/github.com/google/uuid) package: ```go id := uuid.NewString() task := asynq.NewTask("mytask", map[string]interface{}{"task_id": id}) // Save ID to your application database. // Enqueue task ``` Also, I'll consider making task ID available after calling `NewTask` in the future release 👍
Author
Owner

@gopkg-dev commented on GitHub (Apr 5, 2021):

Thanks for your reply, thanks 👍

<!-- gh-comment-id:813222080 --> @gopkg-dev commented on GitHub (Apr 5, 2021): Thanks for your reply, thanks 👍
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#95
No description provided.