[GH-ISSUE #269] [FEATURE REQUEST] Expose raw data of payload #2129

Closed
opened 2026-03-15 19:18:31 +03:00 by kerem · 2 comments
Owner

Originally created by @kamatama41 on GitHub (May 3, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/269

Originally assigned to: @hibiken on GitHub.

Is your feature request related to a problem? Please describe.
Encoding / decoding task parameters is too much to write especially when task has many parameters, so I'd like to convert task parameters into a struct via another library (assuming mitchellh/mapstructure). In order to do that, I need the raw data of Payload when decoding params.

example

type TaskParameter struct {
	Param1 int
	Param2 string
}

var client *asynq.Client

func EnqueueTask(param TaskParameter) error {
	payload := map[string]interface{}{}
	if err := mapstructure.Decode(param, &payload); err != nil {
		return err
	}
	if _, err := client.Enqueue(asynq.NewTask("task", payload)); err != nil {
		return err
	}
	return nil
}

func ExecuteTask(ctx context.Context, task *asynq.Task) error {
	var data map[string]interface{} = task.Payload.Data() // I need this

	var param TaskParameter
	if err := mapstructure.Decode(data, &param); err != nil {
		return err
	}

	// Process the task
	// ...
	return nil
}

Describe the solution you'd like

Describe alternatives you've considered

Additional context

Originally created by @kamatama41 on GitHub (May 3, 2021). Original GitHub issue: https://github.com/hibiken/asynq/issues/269 Originally assigned to: @hibiken on GitHub. **Is your feature request related to a problem? Please describe.** Encoding / decoding task parameters is too much to write especially when task has many parameters, so I'd like to convert task parameters into a struct via another library (assuming [mitchellh/mapstructure](https://github.com/mitchellh/mapstructure)). In order to do that, I need the raw data of Payload when decoding params. **example** ```go type TaskParameter struct { Param1 int Param2 string } var client *asynq.Client func EnqueueTask(param TaskParameter) error { payload := map[string]interface{}{} if err := mapstructure.Decode(param, &payload); err != nil { return err } if _, err := client.Enqueue(asynq.NewTask("task", payload)); err != nil { return err } return nil } func ExecuteTask(ctx context.Context, task *asynq.Task) error { var data map[string]interface{} = task.Payload.Data() // I need this var param TaskParameter if err := mapstructure.Decode(data, &param); err != nil { return err } // Process the task // ... return nil } ``` **Describe the solution you'd like** **Describe alternatives you've considered** **Additional context**
kerem 2026-03-15 19:18:31 +03:00
Author
Owner

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

@kamatama41, thank you for opening this issue!

I'm actually updating the API in the next version:
New API takes arbitrary array of bytes as payload.

func NewTask(tasktype string, payload []byte) *Task

// Type returns the task type.
func (t *Task) Type() string 

// Payload returns the payload data.
func (t *Task) Payload() []byte

What this means is that you can write code like this:

func EnqueueTask(param TaskParameter) error {
        // You can serialize your data however you want, it doesn't need to be JSON.
	b, err := json.Marshal(param)
        if err != nil {
		return err
	}
	_, err := client.Enqueue(asynq.NewTask("task", b))
        return err
}

func ExecuteTask(ctx context.Context, task *asynq.Task) error {
        var params TaskParameter 
        if err := json.Unmarshal(task.Payload(), &params); err != nil {
              return err
        }
        //  Process the task ...
}

I'm actively working on the feature, and should be able to release it in a few more weeks 👍

<!-- gh-comment-id:831530441 --> @hibiken commented on GitHub (May 3, 2021): @kamatama41, thank you for opening this issue! I'm actually updating the API in the next version: New API takes arbitrary array of bytes as payload. ```go func NewTask(tasktype string, payload []byte) *Task // Type returns the task type. func (t *Task) Type() string // Payload returns the payload data. func (t *Task) Payload() []byte ``` What this means is that you can write code like this: ```go func EnqueueTask(param TaskParameter) error { // You can serialize your data however you want, it doesn't need to be JSON. b, err := json.Marshal(param) if err != nil { return err } _, err := client.Enqueue(asynq.NewTask("task", b)) return err } func ExecuteTask(ctx context.Context, task *asynq.Task) error { var params TaskParameter if err := json.Unmarshal(task.Payload(), &params); err != nil { return err } // Process the task ... } ``` I'm actively working on the feature, and should be able to release it in a few more weeks 👍
Author
Owner

@kamatama41 commented on GitHub (May 4, 2021):

Got it, I'm looking forward to that 😃

<!-- gh-comment-id:831738187 --> @kamatama41 commented on GitHub (May 4, 2021): Got it, I'm looking forward to that 😃
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#2129
No description provided.