asynq
asynq copied to clipboard
[BUG] Serializing/deserializing objects (eg: asynq.Client) into a task doesn't work as expected
Describe the bug
For testing purposes, I have a task obj that stores a reference to the asynq.Client
object (I have tested w/ both pointer and non-pointer). When the task is deserialized and processed by the worker, the field for async.Client
is not set.
To Reproduce Steps to reproduce the behavior (Code snippets if applicable):
- Run redis
- Enqueue task that looks like this:
type emailTaskPayload struct { // ID for the email recipient. UserID int Client asynq.Client // *asynq.Client }
- Handler looks like this (slightly modified from docs):
func HandleWelcomeEmailTask(ctx context.Context, t *asynq.Task) error { var p emailTaskPayload if err := json.Unmarshal(t.Payload(), &p); err != nil { return err } log.Printf(" [*] Send Welcome Email to User %d", p.UserID) log.Printf("actual deserialized client: %+v", p.Client) info, err := p.Client.Enqueue(asynq.NewTask(TypeWelcomeEmail, t.Payload())) if err != nil { log.Fatal(err) } log.Printf(" [*] Successfully enqueued task: %+v", info) return nil }
- See Error
2023/09/22 01:17:25 [*] Send Welcome Email to User 42 2023/09/22 01:17:25 actual deserialized client: {broker:<nil>} asynq: pid=4171748 2023/09/22 01:17:25.372155 ERROR: recovering from panic. See the stack trace below for details: goroutine 24 [running]: runtime/debug.Stack() /home/devzero/.goenv/versions/1.20.3/src/runtime/debug/stack.go:24 +0x65 ....... more stuff
Expected behavior Worker is able to deserialize the client object and use it to publish a new task.
Screenshots ~~If applicable, add screenshots to help explain your problem.~~
Environment (please complete the following information):
- OS: Linux
- Version of
asynq
package: v0.24.1
Additional context Add any other context about the problem here.
The workaround when people face issues like these:
- initializing the relevant clients as part of the worker process
- running the worker process as part of the main process, maybe using goroutines etc