轮询Azure队列返回Broken管道

时间:2013-03-28 00:42:25

标签: php sockets azure long-polling azure-storage-queues

使用 azure-sdk-for-php 长时间轮询Azure队列存储时,如果我的请求相隔超过30秒,则库会因此错误而死亡:

PHP Notice:  fwrite(): send of 277 bytes failed with errno=32 Broken pipe in ..../vendor/pear-pear.php.net/HTTP_Request2/HTTP/Request2/SocketWrapper.php on line 188

如果我将睡眠功能设置为30秒,一切都很顺利,但我发出了很多我不需要的请求。

我的工人代码:

use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;

Class Worker_Task {

    public $queueRestProxy;

    public $servicesBuilder;

    public $connectionString;

    public function __construct() {
        $this->connectionString = Config::get('azure.connection_string');
        $this->servicesBuilder = ServicesBuilder::getInstance();
        $this->queueRestProxy = $this->servicesBuilder->createQueueService($this->connectionString);
    }
    public function emails() {

        $this->write('Processing mails...');

        while(true) {

            $this->queueRestProxy = $this->servicesBuilder->createQueueService($this->connectionString);

            // Get message.
            $listMessagesResult = $this->queueRestProxy->listMessages("emails");
            $messages = $listMessagesResult->getQueueMessages();

            foreach($messages as $message) {

                // Process Message
                $content = json_decode($message->getMessageText());

                // Dispatch Email

                // Delete Job
                $this->deleteJob('emails', $message->getMessageId(), $message->getPopReceipt());
            }

            sleep(30);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

这看起来像是过载或超时问题。 PHP库无法写入Azure套接字。可能您使用无限循环while(true) { ... }超过Azure负载限制。

不要在每个循环中创建queueService,因为你无限地关闭和打开pear socket。

尝试移动线:

$this->queueRestProxy = $this->servicesBuilder->createQueueService($this->connectionString);
while(true) {之前

$this->write('Processing mails...');
$this->queueRestProxy = $this->servicesBuilder->createQueueService($this->connectionString);    
while(true) {
        // Get message.

如果这没有用,那么azure-sdk-for-php库可能会错误地使用套接字机制 - 在https://github.com/WindowsAzure/azure-sdk-for-php创建新问题或使用此方法:AzurePHP - Polling an Azure Queue