R
R
romaaa322019-12-29 23:26:46
PHP
romaaa32, 2019-12-29 23:26:46

How to fix error in php-amqlib (RabbitMQ)?

PHP Fatal error:  Uncaught PhpAmqpLib\Exception\AMQPTimeoutException: The connection timed out after 3 sec while awaiting incoming data in /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php:141
Stack trace:
#0 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(163): PhpAmqpLib\Wire\AMQPReader->wait()
#1 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(106): PhpAmqpLib\Wire\AMQPReader->rawread(7)
#2 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(562): PhpAmqpLib\Wire\AMQPReader->read(7)
#3 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(619): PhpAmqpLib\Connection\AbstractConnection->wait_frame(3)
#4 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(234): PhpAmqpLib\Connection\AbstractConnection->wait_channel(0, 3)
#5 /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(352): PhpAmqpLib\Channel\AbstractCh in /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php on line 141

This error appears when starting a task for a worker.
The error appears rarely, hundreds of tasks can be executed, after which an error appears and the task is not executed.
You need to fix the error itself, or, in extreme cases, have another RabbitMQ worker perform the job.
The code:
<?php

define('PROJECT_DIR', __DIR__ . '/../');

require_once __DIR__ . '/../vendor/autoload.php';

use Modules\RabbitMQ\RabbitMQ;
use Modules\Wekings\Task;

$rabbitMQ = new RabbitMQ('Run');

$callback = function ($msg) {
    $runBot = new Task($msg->body);

    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$rabbitMQ->channel->basic_qos(null, 1, null);
$rabbitMQ->channel->basic_consume(
    'Run',
    '',
    false,
    false,
    false,
    false,
    $callback
);

while ($rabbitMQ->channel->is_consuming()) {
    $rabbitMQ->channel->wait();
}

$rabbitMQ->channel->close();
$rabbitMQ->connection->close();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question