使用Redis实施消息队列时出错,使用BLPOP时出错 [英] Error in implementing message queue using redis, error in using BLPOP

查看:679
本文介绍了使用Redis实施消息队列时出错,使用BLPOP时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Redis构建消息队列. 每当客户端发送新数据时,它们就会被添加到列表中.

I am trying to build a message queue using Redis. Whenever client sends new data, they are added to a list.

这里是代码

$client->lPush("my_queue", $data);

现在有一个单独的脚本 slave.php ,它会弹出新到达的数据并进行处理. slave.php的代码

Now there is a separate script slave.php which pops the newly arrived data and process it. The code for slave.php

while (true) {
   list($queue, $message)  = $client->brPop(["my_queue"], 0);

    /*
    Logic to process the data
    */
}

我已经修改了Apache启动脚本,以便slave.php应该启动&用阿帕奇停止.它运作良好.但是等了几分钟后,brPop停止监听并显示以下错误消息:

I have modified the apache startup script so that slave.php should start & stop with apache. It works well. But after waiting for few minutes the brPop stops listening with the error message like this :

Uncaught exception 'Predis\Connection\ConnectionException' with message 'Error while reading line from the server [tcp://127.0.0.1:6379]' in /var/www/api/lib/predis-0.8/lib/Predis/Connection/AbstractConnection.php:139
Stack trace:
#0 /var/www/api/lib/predis-0.8/lib/Predis/Connection/StreamConnection.php(205): Predis\Connection\AbstractConnection->onConnectionError('Error while rea...')
#1 /var/www/api/lib/predis-0.8/lib/Predis/Connection/AbstractConnection.php(128): Predis\Connection\StreamConnection->read()
#2 /var/www/api/lib/predis-0.8/lib/Predis/Connection/AbstractConnection.php(120): Predis\Connection\AbstractConnection->readResponse(Object(Predis\Command\ListPopLastBlocking))
#3 /var/www/api/lib/predis-0.8/lib/Predis/Client.php(227): Predis\Connection\AbstractConnection->executeCommand(Object(Predis\Command\ListPopLastBlocking))
#4 /var/www/api/lib/slave.php(7): Predis\Client->__call('brPop', Array)
#5 /var/www/api/lib/slave.php(7): Predis\Client->brPop(Array, 0)
#6 {main}
 thrown in /var/www/api/lib/predis-0.8/lib/Predis/Connection/AbstractConnection.php on line 139

根据文档,如果list为空,则BLPOP/BRPOP会阻止连接,直到另一个客户端对其中一个键执行LPUSH或RPUSH操作. 但这不是我的情况. 就我而言,一旦brpop阻止了连接,即使新数据到达列表中,它也不会再次侦听.

According to the documentation, if list is empty then, BLPOP/BRPOP blocks the connection until another client performs an LPUSH or RPUSH operation against one of the keys. But this is not happening in my case. In my case once the brpop blocks the connection, it doesn't listen again even when new data arrives in the list.

要使它正常工作,我应该做些什么改变?

What changes I should make to get this working?

推荐答案

它现在对我有用,但是我不确定这样做是否正确.现在,我正在捕获错误并在连接失败的情况下递归调用该函数.我的新slave.php看起来像这样:

Its working for me now but I am not sure whether it is the right method to do this. Now I am catching the error and recursively calling the function in case of connection failure. My new slave.php looks like this :

function process_data()
{
    try {
        $client = new \Predis\Client();

        require_once("logger.php");

        while (true) {
            list($queue, $message) = $client->brPop(["bookmark_queue"], 0);
            // logic
        }
    } catch (Exception $ex) {
        $error = $ex->getMessage();
        log_error($error, "slave.php");
        process_data(); // call the function recursively if connection fails
    }
}
process_data(); // call the function

这篇关于使用Redis实施消息队列时出错,使用BLPOP时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆