Exemplo n.º 1
1
 /**
  * @inheritdoc
  *
  * @throws \InvalidArgumentException
  * @throws InvalidMessageException
  * @throws QueueAccessException
  */
 public function addMessages($queueName, $messages, Priority $priority = null)
 {
     if (empty($queueName)) {
         throw new \InvalidArgumentException('Queue name empty or not defined.');
     }
     if (null === $priority) {
         $priority = $this->priorityHandler->getDefault();
     }
     $i = 0;
     $batch = [];
     $totalMessagesCount = count($messages);
     foreach ($messages as $index => $message) {
         if (empty($message)) {
             throw new InvalidMessageException($message, 'Message empty or not defined.');
         }
         $messageData = ['Id' => (string) $index, 'MessageBody' => serialize($message)];
         $batch[] = $messageData;
         if (++$i < $totalMessagesCount && count($batch) < self::SENT_MESSAGES_BATCH_SIZE) {
             continue;
         }
         try {
             $queueUrl = $this->sqsClient->getQueueUrl(['QueueName' => $this->getQueueNameWithPrioritySuffix($queueName, $priority)])->get('QueueUrl');
             $this->sqsClient->sendMessageBatch(['QueueUrl' => $queueUrl, 'Entries' => $batch]);
         } catch (SqsException $e) {
             throw new QueueAccessException('Cannot add messages in queue.', 0, $e);
         }
         $batch = [];
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Valid option is:
  *      - delay_seconds: the duration (in seconds) the message has to be delayed
  *
  * Please note that for this to work, the index for the job AND the option must match
  *
  * {@inheritDoc}
  */
 public function batchPush(array $jobs, array $options = array())
 {
     // SQS can only handle up to 10 jobs, so if we have more jobs, we handle them in slices
     if (count($jobs) > 10) {
         do {
             $splicedJobs = array_splice($jobs, 0, 10);
             $this->batchPush($splicedJobs, $options);
         } while (count($splicedJobs) >= 10);
         return;
     }
     // SQS throws an exception if no jobs are inserted, which can happen due to the slicing method
     if (empty($jobs)) {
         return;
     }
     $parameters = array('QueueUrl' => $this->queueOptions->getQueueUrl(), 'Entries' => array());
     /** @var $job JobInterface */
     foreach ($jobs as $key => $job) {
         $jobParameters = array('Id' => $key, 'MessageBody' => $this->serializeJob($job), 'DelaySeconds' => isset($options[$key]['delay_seconds']) ? $options[$key]['delay_seconds'] : null);
         $parameters['Entries'][] = array_filter($jobParameters, function ($value) {
             return $value !== null;
         });
     }
     $result = $this->sqsClient->sendMessageBatch($parameters);
     $messages = $result['Successful'];
     foreach ($messages as $message) {
         $batchId = $message['Id'];
         $jobs[$batchId]->setMetadata(array('__id__' => $message['MessageId'], 'md5' => $message['MD5OfMessageBody']));
     }
 }
Exemplo n.º 3
0
 /**
  * This can be used to send up to 10 entries and up to a total of 264kb
  *
  * {@inheritDoc}
  */
 public function sendMessageBatch($messageBodies = [], $queueId = null)
 {
     if (!is_array($messageBodies)) {
         throw new \Exception("MessageBodies must be an array");
     }
     $queueUrl = $this->getQueueUrl($queueId);
     $entries = [];
     foreach ($messageBodies as $key => $body) {
         $entries[] = ["Id" => $key, "MessageBody" => json_encode($body)];
     }
     $this->queueClient->sendMessageBatch(["QueueUrl" => $queueUrl, "Entries" => $entries]);
 }
Exemplo n.º 4
0
 /**
  * @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#sendmessagebatch
  * @param array $messages
  */
 public function batchPublish(array $messages)
 {
     $j = 0;
     for ($i = 0; $i < count($messages); $i += 10) {
         $entries = array();
         $toSend = array_slice($messages, $i, 10);
         foreach ($toSend as $message) {
             $entries[] = array_merge(array('MessageBody' => $message['msgBody'], 'Id' => $j++), $this->getClientParams(@$message['routingKey'], @$message['additionalProperties']));
         }
         $result = $this->client->sendMessageBatch(array('QueueUrl' => $this->queueUrl, 'Entries' => $entries));
         if (($ok = count($result->get('Successful'))) != ($tot = count($toSend))) {
             throw new \RuntimeException("Batch sending of messages failed - {$ok} ok out of {$tot}");
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Message batch processing
  *
  * @param $messages
  * @return mixed
  */
 public function enqueueAll(MessageCollectionInterface $messages)
 {
     $serializer = $this->getEncoder();
     $request = ['QueueUrl' => $this->getUrl(), 'Entries' => []];
     $messages = $messages->getMessages();
     foreach (array_chunk($messages, $this->config->getBatchSize()) as $chunk) {
         foreach ($chunk as $id => $message) {
             $request['Entries'][$id] = ['Id' => $id, 'DelaySeconds' => $this->getMessageDelay($message), 'QueueUrl' => $this->getUrl(), 'MessageBody' => $serializer->encode($message)];
         }
         $result = $this->client->sendMessageBatch($request);
         $items = $result->getPath('Successful') ?: [];
         foreach ($items as $item) {
             $messages[$item['Id']]->setId($item['MessageId']);
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Flush a queue
  *
  * @param  bool $async
  * @return void
  */
 private function doFlush(bool $async)
 {
     // SQS only supports batch of 10, so we need to splice like that
     while (!empty($this->messages)) {
         $messagesToPush = array_splice($this->messages, 0, 10);
         $parameters = ['QueueUrl' => $this->url, 'Entries' => []];
         foreach ($messagesToPush as $key => $message) {
             $messageParameters = ['Id' => $key, 'MessageBody' => json_encode($message['body'], self::DEFAULT_JSON_FLAGS), 'DelaySeconds' => $message['options']['delay_seconds'] ?? null];
             $parameters['Entries'][] = array_filter($messageParameters, function ($value) {
                 return $value !== null;
             });
         }
         if ($async) {
             $this->sqsClient->sendMessageBatchAsync($parameters);
         } else {
             $this->sqsClient->sendMessageBatch($parameters);
         }
     }
 }
Exemplo n.º 7
0
 protected function sendMessageBatch(array $payrolls, array $attributes, $delay)
 {
     $entries = [];
     foreach ($payrolls as $idx => $payroll) {
         $entry = ["Id" => "buf_{$idx}", "MessageBody" => $payroll];
         if ($delay) {
             $entry['DelaySeconds'] = $delay;
         }
         if ($attributes) {
             $entry['MessageAttributes'] = $attributes[$idx];
         }
         $entries[] = $entry;
     }
     $args = ["QueueUrl" => $this->getQueueUrl(), "Entries" => $entries];
     $result = $this->client->sendMessageBatch($args);
     if ($result['Failed']) {
         foreach ($result['Failed'] as $failed) {
             mwarning(sprintf("Batch sending message failed, code = %s, id = %s, msg = %s, senderfault = %s", $failed['Code'], $failed['Id'], $failed['Message'], $failed['SenderFault']));
         }
         throw new \RuntimeException("Cannot send some messages, consult log for more info!");
     }
 }