Skip to content

Reconnection guidelines - graceful dropped connection handling on consumer #444

Closed
@aaronpeterson

Description

@aaronpeterson

Hoping to clarify best practices in reconnecting a consumer after a dropped connection, service outage, etc. Following this basic pattern:

class MyConsumer {

    protected $amqpChannel;
    protected $amqpConnection;

    public function connect()
    {
        // ...set $this->amqpConnection, etc
    }

    public function consume() {
        $this->amqpChannel->basic_consume('my-queue', 'my-tag', false, false, false, false, function ($msg) {
               $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        });
        
        while (count($this->amqpChannel->callbacks)) {
            try {
                $this->amqpChannel->wait();
            } catch (\Exception $e) {
                // Suppose $e is \ErrorException with "errno=32" or "Broken pipe" in message
                $this->amqpConnection->reconnect();

                 // through all of this, $this->amqpConnection->isConnected() remains true
                 // Errors with PhpAmqpLib\Exception\AMQPProtocolConnectionException: CHANNEL_ERROR - expected 'channel.open'
                $this->consume();
            }
        }
    }
}

I've tried a number of things and can't seem to get my AMQPChannel::wait() to resume. I'd be happy to add it to the examples if anyone can clarify.

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