hibiken / asynq

Simple, reliable, and efficient distributed task queue in Go
MIT License
9k stars 659 forks source link

[BUG] EnqueueContext not the original context #866

Closed cexll closed 2 months ago

cexll commented 2 months ago

Describe the bug unable to retrieve the original context in the handler To Reproduce


j := asynq.NewTask("product",
        payload,
        "product",
        asynq.Retention(time.Hour*24*30),
    )
                                    // ⬇️
_, err = h.asyncClient.EnqueueContext(ctx, j) // this ctx it origin ctx 
if err != nil {
    return nil, err
}

// handler 

return job.NewHandlerFunc("product", func(ctx context.Context, task *asynq.Task) error {
                   // ⬇️ 
    log.Infof("%+v", ctx) // This ctx is not the initial ctx passed in

    return nil
})
type Handler struct {
    Pattern string
    asynq.Handler
}

func NewHandler(pattern string, handler asynq.Handler) *Handler {
    return &Handler{Pattern: pattern, Handler: handler}
}

func NewHandlerFunc(pattern string, handler asynq.HandlerFunc) *Handler {
    return &Handler{Pattern: pattern, Handler: handler}
}

Expected behavior get the original context

Screenshots

Environment (please complete the following information):

Additional context Add any other context about the problem here.

kamikazechaser commented 2 months ago

Not really a bug. That is how the library is written. EnqueueContext only overrides the context for saving the payload to Redis. The handler receives an AsynqContext which is base context (settable) + task metadata value added to it.

cexll commented 2 months ago

hmm, ok Thanks
that's how it's done now