示例#1
0
 public function test()
 {
     $delayedMessagesTopicName = 'test-delayed-' . rand();
     $this->manager = $manager = new Manager($delayedMessagesTopicName);
     $topicName = 'test-' . rand();
     $manager->setDelayedMessagesTopic(array('publish_to' => 'localhost', 'requeue_strategy' => array('max_attempts' => 10, 'delays' => array(50))));
     $manager->setTopic($topicName, array('publish_to' => array('localhost'), 'requeue_strategy' => array('max_attempts' => 3, 'delays' => $this->rsDelays)));
     $pid = pcntl_fork();
     if ($pid == -1) {
         throw new \Exception('fork');
     }
     if ($pid == 0) {
         $manager->setTopicConsumer($delayedMessagesTopicName, 'default', new DelayedMessagesConsumer($manager));
         $manager->consumeDelayedMessagesTopic(5);
         exit;
     }
     $manager->setTopicConsumer($topicName, 'default', $this);
     $this->messages = array(array('data' => 'data_' . rand(), 'delay' => 2), array('data' => 'data_' . rand(), 'delay' => 1), array('data' => 'data_' . rand(), 'delay' => 0, 'retries' => 2), array('data' => 'data_' . rand(), 'delay' => 3, 'retries' => 1));
     foreach ($this->messages as $k => $message) {
         $manager->getTopic($topicName)->publish($message['data'], $message['delay']);
         $this->messages[$k]['ts'] = time();
         $this->messages[$k]['attempts'] = 0;
     }
     $this->loop = $this->readAttribute($this->readAttribute($manager->getTopic($topicName), 'nsq'), 'loop');
     $manager->getTopic($topicName)->consume(array(), 7);
 }
示例#2
0
 public function consume($topic, $channel, $payload)
 {
     $message = Message::factory($payload);
     $remaining = $message->getScheduledAt() - time();
     if ($remaining > 0) {
         throw new RequeueMessageException(min(3600, $remaining) * 1000);
     }
     $target = $this->topicManager->getTopic($message->getTargetTopic());
     if (!$target) {
         throw new \Exception(sprintf("Unknown target topic '%s' for message '%s'", $message->getTargetTopic(), $message->getUserPayload()));
     }
     $target->publish($message->getUserPayload());
 }