[GH-ISSUE #592] Question about task ID #287

Closed
opened 2026-03-02 05:20:12 +03:00 by kerem · 2 comments
Owner

Originally created by @VolunPotest on GitHub (Dec 26, 2022).
Original GitHub issue: https://github.com/hibiken/asynq/issues/592

Hi!
I have a question about Task ID.

We can receive it when we create a task and send it to the Enqueue.

But I don't see the ID of the task in handling function, we only have *asynq.Task.
My question is: Is it possible to retrieve taskID in the handle function?

I think it will be really helpful (especially for a logging)
Thank you!

Originally created by @VolunPotest on GitHub (Dec 26, 2022). Original GitHub issue: https://github.com/hibiken/asynq/issues/592 Hi! I have a question about Task ID. We can receive it when we create a task and send it to the Enqueue. But I don't see the ID of the task in handling function, we only have *asynq.Task. My question is: Is it possible to retrieve taskID in the handle function? I think it will be really helpful (especially for a logging) Thank you!
kerem closed this issue 2026-03-02 05:20:12 +03:00
Author
Owner

@hibiken commented on GitHub (Dec 27, 2022):

You can get it by calling GetTaskID passing in the context.

func MyHandler(ctx context.Context, task *asynq.Task) error {
     id, ok := asynq.GetTaskID(ctx)
     // ... rest of the handler code omitted.
}
<!-- gh-comment-id:1365646921 --> @hibiken commented on GitHub (Dec 27, 2022): You can get it by calling [`GetTaskID`](https://pkg.go.dev/github.com/hibiken/asynq#GetTaskID) passing in the context. ```go func MyHandler(ctx context.Context, task *asynq.Task) error { id, ok := asynq.GetTaskID(ctx) // ... rest of the handler code omitted. } ```
Author
Owner

@VolunPotest commented on GitHub (Dec 29, 2022):

Hi. Thank you for the response.
Do you consider returning the whole metadata struct?
Something like this?

// GetMetadata extracts metadata from a context, if any.
func GetMetadata(ctx context.Context) (metadata taskMetadata, ok bool) {
	metadata, ok = ctx.Value(metadataCtxKey).(taskMetadata)
	return
}
<!-- gh-comment-id:1367522519 --> @VolunPotest commented on GitHub (Dec 29, 2022): Hi. Thank you for the response. Do you consider returning the whole metadata struct? Something like this? ``` // GetMetadata extracts metadata from a context, if any. func GetMetadata(ctx context.Context) (metadata taskMetadata, ok bool) { metadata, ok = ctx.Value(metadataCtxKey).(taskMetadata) return } ```
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#287
No description provided.