Skip to content

Handler receives the envelope instead of the message it was registered for #341

Open

Description

@KalimeroMK

Worker::process() passes the message into the consume pipeline exactly as it arrived, and ConsumeFinalHandler hands that to the handler. Nothing unwraps an envelope on the way, so a handler written the way the guide writes it breaks as soon as the message is wrapped.

The documented handler (docs/guide/en/messages-and-handlers.md:108):

public function handle(MessageInterface $message): void
{
    assert($message instanceof SendEmailMessage);
    $this->mailer->send($message->to, $message->subject, $message->body);
}

Called with an adapter-assigned id, which is the normal asynchronous path:

$worker->process(new IdEnvelope($message, 'id-42'), 'test');

With zend.assertions=1:

MessageFailureException: Processing of message #id-42 is stopped because of an exception:
assert($message instanceof SendEmailMessage).

With zend.assertions=-1, the production default:

Warning: Undefined property: Yiisoft\Queue\Message\IdEnvelope::$to
sending to: NULL

No error, and the work is done against empty values. DelayEnvelope and FailureEnvelope behave the same way. Call path: Worker.php:45 → ConsumeMiddlewareDispatcher → ConsumeFinalHandler.php:21.

Worth deciding which contract is meant:

  • unwrap in Worker before dispatching, so a handler always receives the message type it was registered for, and offer the envelope separately to handlers that want the metadata, or
  • keep passing the envelope and change every handler example in the guide to unwrap first ($message instanceof Envelope ? $message->getMessage() : $message), since assert() is not a guard in production.

InMemoryAdapter happens not to trigger this because it stores the unwrapped message, but any adapter that stores what push() returned does.

#338 reworks Worker and the pipeline around it, so this probably wants deciding before that lands.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions