asynq icon indicating copy to clipboard operation
asynq copied to clipboard

[BUG] Serializing/deserializing objects (eg: asynq.Client) into a task doesn't work as expected

Open dray92 opened this issue 1 year ago • 1 comments

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):

  1. Run redis
  2. Enqueue task that looks like this:
    type emailTaskPayload struct {
       // ID for the email recipient.
       UserID int
       Client asynq.Client // *asynq.Client
    }
    
  3. 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
    } 
    
  4. 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.

dray92 avatar Sep 22 '23 01:09 dray92

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

dray92 avatar Sep 22 '23 01:09 dray92