/**
  * @return MessageReceivedInterface[]
  * @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#receivemessage
  */
 public function receiveMessage(array $options = [])
 {
     $this->params = $this->mergeParams([], $options);
     $sqsResult = $this->awsSqsClient->receiveMessage($this->params);
     $sqsMessages = empty($sqsResult['Messages']) ? [] : $sqsResult['Messages'];
     return $this->createMessageObject($sqsMessages);
 }
 /**
  * Pop the next job off of the queue.
  *
  * @param  string  $queue
  * @return \Illuminate\Contracts\Queue\Job|null
  */
 public function pop($queue = null)
 {
     $queue = $this->getQueue($queue);
     $response = $this->sqs->receiveMessage(['QueueUrl' => $queue, 'AttributeNames' => ['ApproximateReceiveCount']]);
     if (count($response['Messages']) > 0) {
         return new SqsJob($this->container, $this->sqs, $queue, $response['Messages'][0]);
     }
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function dequeue(SerializerInterface $serializer)
 {
     $message = $this->client->receiveMessage(['QueueUrl' => $this->url])->get('Messages');
     if (count($message)) {
         return new SqsJob($this->client, $this->url, $message[0]['ReceiptHandle'], $serializer->unserialize($message[0]['Body']));
     }
     return null;
 }
 /**
  * Pop the next job off of the queue.
  *
  * @param  string  $queue
  * @return \Illuminate\Queue\Jobs\Job|null
  */
 public function pop($queue = null)
 {
     $queue = $this->getQueue($queue);
     $response = $this->sqs->receiveMessage(array('QueueUrl' => $queue));
     if (count($response['Messages']) > 0) {
         return new SqsJob($this->container, $this->sqs, $queue, $response['Messages'][0]);
     }
 }
Exemple #5
0
 public function dequeue()
 {
     $result = $this->client->receiveMessage(array('QueueUrl' => $this->queueUrl));
     $messages = $result->get('Messages');
     if (!$messages) {
         return false;
     }
     return array($messages[0]['Body'], $messages[0]['ReceiptHandle'], []);
 }
Exemple #6
0
 /**
  * @inheritdoc
  */
 public function pop($queue)
 {
     $response = $this->sqs->receiveMessage(['QueueUrl' => $queue]);
     if (empty($response['Messages'])) {
         return false;
     }
     $data = reset($response['Messages']);
     return ['id' => $data['MessageId'], 'body' => $data['Body'], 'queue' => $queue, 'receipt-handle' => $data['ReceiptHandle']];
 }
Exemple #7
0
 /**
  * @throws \RuntimeException
  *
  * @return MessageInterface
  */
 public function receive()
 {
     $packer = $this->getPacker();
     if (!$packer) {
         throw new \RuntimeException('Packed not defined');
     }
     $message = $this->client->receiveMessage(array('QueueUrl' => $this->queueUrl));
     return $packer->decode($message);
 }
 /**
  * Return next job from the queue.
  * @return Job|boolean the job or false if not found.
  */
 public function fetchJob()
 {
     $message = $this->_client->receiveMessage(['QueueUrl' => $this->url, 'AttributeNames' => ['ApproximateReceiveCount'], 'MaxNumberOfMessages' => 1]);
     if (isset($message['Messages']) && count($message['Messages']) > 0) {
         return $this->createJobFromMessage($message['Messages'][0]);
     } else {
         return false;
     }
 }
 /**
  * @param mixed[] $args
  * @return Result
  * @throws ExceptionInterface
  */
 private function receiveSqsMessage(array $args = [])
 {
     $result = $this->sqsClient->receiveMessage($args);
     $messages = array();
     foreach ($result->search('Messages[]') as $i => $message) {
         $messages[$i] = $this->decodeSqsMessage($message);
     }
     $result->offsetSet('Messages', $messages);
     return $result;
 }
Exemple #10
0
 /**
  * {@inheritDoc}
  */
 public function receive()
 {
     $result = $this->client->receiveMessage(array('QueueUrl' => $this->queueUrl));
     if (null === $result->get('Messages')) {
         return;
     }
     if ($result) {
         return new Message($result['Messages'][0]['Body'], array('ReceiptHandle' => $result['Messages'][0]['ReceiptHandle']));
     }
 }
 /**
  * Wait and get job from a queue
  *
  * @access public
  * @return Job|null
  */
 public function pull()
 {
     $result = $this->sqsClient->receiveMessage(array('QueueUrl' => $this->sqsUrl, 'WaitTimeSeconds' => empty($this->config['LongPollingTime']) ? 0 : (int) $this->config['LongPollingTime']));
     if ($result['Messages'] == null) {
         return null;
     }
     $resultMessage = array_pop($result['Messages']);
     $job = new Job();
     $job->setId($resultMessage['ReceiptHandle']);
     $job->unserialize($resultMessage['Body']);
     return $job;
 }
Exemple #12
0
 /**
  * Receive messages
  *
  * @param bool $priority
  * @param int $num
  * @param int $wait
  * @return \Guzzle\Service\Resource\Model
  */
 public function receive($priority = false, $num = 10, $wait = 20)
 {
     if (empty($this->url)) {
         return false;
     }
     $receiveData = ['QueueUrl' => $this->url, 'MaxNumberOfMessages' => $num, 'WaitTimeSeconds' => $wait];
     if ($priority) {
         $receiveData['MessageAttributeNames'] = ["Priority"];
     }
     $result = $this->client->receiveMessage($receiveData);
     return $result->toArray();
 }
Exemple #13
0
 /**
  * @param int $messagesCount
  * @return array of QueueMessage
  */
 public function receive($messagesCount = 1)
 {
     $result = $this->client->receiveMessage(['QueueUrl' => $this->queueUrl, 'WaitTimeSeconds' => 20, 'VisibilityTimeout' => 3600, 'MaxNumberOfMessages' => $messagesCount]);
     $queueUrl = $this->queueUrl;
     return array_map(function ($message) use($queueUrl) {
         $body = json_decode($message['Body']);
         if (!is_object($body)) {
             $this->client->deleteMessage(['QueueUrl' => $queueUrl, 'ReceiptHandle' => $message['ReceiptHandle']]);
             throw new ApplicationException("Corrupted message {$message['MessageId']} received. Message was deleted from SQS.", null, ['message' => $message]);
         }
         return new QueueMessage($message['MessageId'], $body, $message['ReceiptHandle'], $queueUrl);
     }, (array) $result['Messages']);
 }
 /**
  * @inheritDoc
  */
 public function dequeue($queueName)
 {
     $queueUrl = $this->getQueueUrl($queueName);
     $result = $this->client->receiveMessage(array('QueueUrl' => $queueUrl, 'MaxNumberOfMessages' => 1, 'AttributeNames' => ['ApproximateReceiveCount']));
     if (!$result || !($messages = $result['Messages'])) {
         return null;
     }
     $message = array_shift($messages);
     $wrapped = $this->unserialize($message['Body']);
     $msg = new DefaultEnvelope($wrapped->unwrap(), $message['Attributes']['ApproximateReceiveCount']);
     $env = new SqsEnvelope($message['MessageId'], $msg, $message['ReceiptHandle']);
     return $env;
 }
 /**
  * @param int   $max_count
  * @param int   $wait
  * @param int   $visibility_timeout
  * @param array $metas
  * @param array $message_attributes
  *
  * @return SqsReceivedMessage[]
  */
 protected function receiveMessageBatch($max_count = 1, $wait = null, $visibility_timeout = null, $metas = [], $message_attributes = [])
 {
     if ($max_count > 10 || $max_count < 1) {
         throw new \InvalidArgumentException("Max count for SQS message receiving is 10");
     }
     $args = ["QueueUrl" => $this->getQueueUrl(), "MaxNumberOfMessages" => $max_count];
     if ($wait !== null && is_int($wait)) {
         $args['WaitTimeSeconds'] = $wait;
     }
     if ($visibility_timeout !== null && is_int($visibility_timeout)) {
         $args['VisibilityTimeout'] = $visibility_timeout;
     }
     if ($metas && is_array($metas)) {
         $args['AttributeNames'] = $metas;
     }
     if ($message_attributes && is_array($message_attributes)) {
         $args['MessageAttributeNames'] = $message_attributes;
     }
     $result = $this->client->receiveMessage($args);
     $messages = $result['Messages'];
     if (!$messages) {
         return [];
     }
     $ret = [];
     foreach ($messages as $data) {
         $msg = new SqsReceivedMessage($data);
         $ret[] = $msg;
     }
     return $ret;
 }
Exemple #16
0
 /**
  * {@inheritdoc}
  */
 public function wait(Closure $callback)
 {
     while (true) {
         $result = $this->client->receiveMessage(array('QueueUrl' => $this->queueUrl, 'WaitTimeSeconds' => 20));
         $messages = $result['Messages'];
         if ($messages) {
             foreach ($messages as $message) {
                 $hermesMessage = $this->serializer->unserialize($message['Body']);
                 $callback($hermesMessage);
                 $this->client->deleteMessage(array('QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $message['ReceiptHandle']));
             }
         }
         if ($this->sleepInterval) {
             sleep($this->sleepInterval);
         }
     }
 }
Exemple #17
0
 /**
  * Executes the current command
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $queueName = $input->getOption('queue');
     try {
         $queueUrl = $this->sqsClient->getQueueUrl(['QueueName' => $queueName])['QueueUrl'];
     } catch (SqsException $exception) {
         $output->writeln(sprintf('<error>Impossible to retrieve URL for queue "%s". Reason: %s</error>', $queueName, $exception->getMessage()));
         return;
     }
     $uri = rtrim($input->getOption('server'), '/') . '/' . ltrim($input->getOption('path'), '/');
     while (true) {
         $messages = $this->sqsClient->receiveMessage(['QueueUrl' => $queueUrl, 'AttributeNames' => ['All'], 'MaxNumberOfMessages' => 1, 'WaitTimeSeconds' => 20]);
         if (!$messages->hasKey('Messages')) {
             continue;
         }
         $this->processMessage($messages['Messages'][0], $uri, $queueName, $queueUrl, $output);
     }
 }
Exemple #18
0
 /**
  * {@inheritDoc}
  */
 public function receiveMessage($queueId = null, $waitTime = null)
 {
     if (empty($queueId)) {
         // have to do this here as we need the ID later in this method
         $queueId = $this->getQueueId();
     }
     $queueUrl = $this->getQueueUrl($queueId);
     $message = $this->queueClient->receiveMessage(["QueueUrl" => $queueUrl, "WaitTimeSeconds" => isset($waitTime) ? $waitTime : $this->waitTime]);
     return $this->messageFactory->createMessage($message->toArray(), $queueId);
 }
 /**
  * @inheritdoc
  *
  * @throws \InvalidArgumentException
  * @throws MalformedMessageException
  * @throws QueueAccessException
  */
 public function getMessages($queueName, $nbMsg = 1, Priority $priority = null)
 {
     if (null === $priority) {
         $priorities = $this->priorityHandler->getAll();
         $messages = [];
         foreach ($priorities as $priority) {
             $messagesPriority = $this->getMessages($queueName, $nbMsg, $priority);
             $nbMsg -= count($messagesPriority);
             $messages = array_merge($messages, $messagesPriority);
             if ($nbMsg <= 0) {
                 return $messages;
             }
         }
         return $messages;
     }
     if (empty($queueName)) {
         throw new \InvalidArgumentException('Queue name empty or not defined.');
     }
     if (!is_numeric($nbMsg)) {
         throw new \InvalidArgumentException('Number of messages must be numeric.');
     }
     if ($nbMsg <= 0 || $nbMsg > self::MAX_NB_MESSAGES) {
         throw new \InvalidArgumentException('Number of messages not valid.');
     }
     try {
         $queueUrl = $this->sqsClient->getQueueUrl(['QueueName' => $this->getQueueNameWithPrioritySuffix($queueName, $priority)])->get('QueueUrl');
         $results = $this->sqsClient->receiveMessage(['QueueUrl' => $queueUrl, 'MaxNumberOfMessages' => $nbMsg]);
         $messages = $results->get('Messages');
     } catch (SqsException $e) {
         throw new QueueAccessException('Cannot get messages from queue.', 0, $e);
     }
     if (is_null($messages)) {
         return [];
     }
     foreach ($messages as $messageId => $message) {
         try {
             $messages[$messageId]['Body'] = @unserialize($message['Body']);
             // for php 7 compatibility
             if (false === $messages[$messageId]['Body']) {
                 $message['priority'] = $priority->getLevel();
                 throw new MalformedMessageException($message, 'Message seems to be malformed.');
             }
         } catch (\Exception $e) {
             $message['priority'] = $priority->getLevel();
             throw new MalformedMessageException($message, 'Message seems to be malformed.');
         }
         $messages[$messageId]['priority'] = $priority->getLevel();
     }
     return $messages;
 }
Exemple #20
0
 /**
  * {@inheritDoc}
  */
 public function dequeue()
 {
     $message = null;
     $options = $this->config;
     $response = $this->client->receiveMessage(['QueueUrl' => $this->getUrl(), 'WaitTimeSeconds' => $options->getReceiveMessageWaitTimeSeconds()]);
     if ($response instanceof Model) {
         $data = $response->getPath('Messages/0');
         if ($data) {
             $message = $this->getEncoder()->decode($data['Body']);
             $message->setId($data['MessageId']);
             $message->setMetadata(static::RECEIPT_HANDLE_KEY, $data['ReceiptHandle']);
         }
     }
     return $message;
 }
 public function receiveMessages($queueUrl, $maximalMessages = 1)
 {
     if ($maximalMessages > 10) {
         throw new Exception('Messagenumber should be not more than 10');
     }
     $result = $this->service->receiveMessage(array('QueueUrl' => $queueUrl, 'MaxNumberOfMessages' => $maximalMessages));
     $returnMessages = array();
     $resultMessages = $result->getPath('Messages/*/Body');
     if ($resultMessages != null) {
         foreach ($resultMessages as $messageBody) {
             // Do something with the message
             //echo $messageBody;
             $returnMessages[] = $messageBody;
         }
         $entrys = array();
         $i = 0;
         foreach ($result->getPath('Messages/*/ReceiptHandle') as $receiptHandle) {
             $entrys[] = array('Id' => $i, 'ReceiptHandle' => $receiptHandle);
             $i++;
         }
         $result = $this->service->deleteMessageBatch(array('QueueUrl' => $queueUrl, 'Entries' => $entrys));
     }
     return $returnMessages;
 }
Exemple #22
0
 /**
  * Receives a message from the queue and puts it into a Message object
  *
  * @return bool|Message  Message object built from the queue, or false if there is a problem receiving message
  */
 public function receive()
 {
     try {
         // Receive a message from the queue
         $result = $this->sqs_client->receiveMessage(array('QueueUrl' => $this->url));
         if ($result['Messages'] == null) {
             // No message to process
             return false;
         }
         // Get the message and return it
         $result_message = array_pop($result['Messages']);
         return new Message($result_message['Body'], $result_message['ReceiptHandle']);
     } catch (Exception $e) {
         echo 'Error receiving message from queue ' . $e->getMessage();
         return false;
     }
 }
Exemple #23
0
 /**
  * Valid options are:
  *      - max_number_of_messages: maximum number of jobs to return. As of today, the max value can be 10. Please
  *                                remember that Amazon SQS does not guarantee that you will receive exactly
  *                                this number of messages, rather you can receive UP-TO n messages.
  *      - visibility_timeout: the duration (in seconds) that the received messages are hidden from subsequent
  *                            retrieve requests after being retrieved by a pop request
  *      - wait_time_seconds: by default, when we ask for a job, it will do a "short polling", it will
  *                           immediately return if no job was found. Amazon SQS also supports "long polling". This
  *                           value can be between 1 and 20 seconds. This allows to maintain the connection active
  *                           during this period of time, hence reducing the number of empty responses.
  *
  * {@inheritDoc}
  */
 public function batchPop(array $options = array())
 {
     $clientOptions = array('QueueUrl' => $this->queueOptions->getQueueUrl(), 'MaxNumberOfMessages' => isset($options['max_number_of_messages']) ? $options['max_number_of_messages'] : null);
     if (isset($options['visibility_timeout'])) {
         $clientOptions['VisibilityTimeout'] = $options['visibility_timeout'];
     }
     if (isset($options['wait_time_seconds'])) {
         $clientOptions['WaitTimeSeconds'] = $options['wait_time_seconds'];
     }
     $result = $this->sqsClient->receiveMessage($clientOptions);
     $messages = $result['Messages'];
     if (empty($messages)) {
         return array();
     }
     $jobs = array();
     foreach ($messages as $message) {
         $jobs[] = $this->unserializeJob($message['Body'], array('__id__' => $message['MessageId'], 'receiptHandle' => $message['ReceiptHandle'], 'md5' => $message['MD5OfBody']));
     }
     return $jobs;
 }
Exemple #24
0
 /**
  * {@inheritDoc}
  */
 public function receive(array $options = [])
 {
     $options = $this->mergeOptions($options);
     if (!$this->queueExists()) {
         $this->create();
     }
     $result = $this->sqs->receiveMessage(['QueueUrl' => $this->queueUrl, 'MaxNumberOfMessages' => $options['messages_to_receive'], 'WaitTimeSeconds' => $options['receive_wait_time']]);
     $messages = $result->get('Messages') ?: [];
     // Convert to Message Class
     foreach ($messages as &$message) {
         $id = $message['MessageId'];
         $metadata = ['ReceiptHandle' => $message['ReceiptHandle'], 'MD5OfBody' => $message['MD5OfBody']];
         // When using SNS, the SQS Body is the entire SNS Message
         if (is_array($body = json_decode($message['Body'], true)) && isset($body['Message'])) {
             $body = json_decode($body['Message'], true);
         }
         $message = new Message($id, $body, $metadata);
         $context = ['MessageId' => $id];
         $this->log(200, "Message fetched from SQS Queue", $context);
     }
     return $messages;
 }
Exemple #25
0
 /**
  * @inheritdoc
  *
  * @throws SqsException
  */
 public function getMessages($queueName, $nbMsg = 1, $priority = null)
 {
     if (null === $priority) {
         $priorities = $this->priorityHandler->getAll();
         $messages = [];
         foreach ($priorities as $priority) {
             $messagesPriority = $this->getMessages($queueName, $nbMsg, $priority);
             $nbMsg -= count($messagesPriority);
             $messages = array_merge($messages, $messagesPriority);
             if ($nbMsg <= 0) {
                 return $messages;
             }
         }
         return $messages;
     }
     if (empty($queueName)) {
         throw new InvalidArgumentException('Parameter queueName empty or not defined.');
     }
     if (!is_numeric($nbMsg)) {
         throw new InvalidArgumentException('Parameter number is not a number.');
     }
     if ($nbMsg <= 0 || $nbMsg > self::MAX_NB_MESSAGES) {
         throw new InvalidArgumentException('Parameter number is not valid.');
     }
     $queueUrl = $this->sqsClient->getQueueUrl(['QueueName' => $this->getQueueNameWithPrioritySuffix($queueName, $priority)])->get('QueueUrl');
     $results = $this->sqsClient->receiveMessage(['QueueUrl' => $queueUrl, 'MaxNumberOfMessages' => $nbMsg]);
     $messages = $results->get('Messages');
     if (is_null($messages)) {
         return [];
     }
     foreach ($messages as $messageId => $message) {
         $messages[$messageId]['Body'] = unserialize($message['Body']);
         $messages[$messageId]['priority'] = $priority;
     }
     return $messages;
 }
<?php

header("Content-type: text/html; charset=utf-8");
require 'config.php';
require '../vendor/autoload.php';
use Aws\Sqs\SqsClient;
use Aws\S3\S3Client;
$s3 = new S3Client(['version' => S3_VERSION, 'region' => S3_REGION]);
$sqs = new SqsClient(['version' => SQS_VERSION, 'region' => SQS_REGION]);
$messageResult = $sqs->receiveMessage(array('QueueUrl' => SQS_OUTBOX, 'MaxNumberOfMessages' => 10, 'MessageAttributeNames' => array('s3path', 's3bucket', 'filename', 'smallfilename')));
$message = "";
if ($messageResult->getPath('Messages') != '') {
    foreach ($messageResult->getPath('Messages') as $messages) {
        $attr = array();
        $receiptHandle = $messages['ReceiptHandle'];
        foreach ($messages['MessageAttributes'] as $key => $value) {
            $attr[$key] = $value['StringValue'];
            $message = $attr['filename'] . " Resize Done.<br/>";
        }
        $sqs->deleteMessage(array('QueueUrl' => SQS_OUTBOX, 'ReceiptHandle' => $receiptHandle));
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AWS EC2 S3 SQS</title>
<script	src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script	src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
Exemple #27
0
 function it_receive_and_handle_an_empty_message(\Aws\Sqs\SqsClient $client)
 {
     $responseMock = unserialize(file_get_contents(__DIR__ . "/../../resources/receiveMessageEmpty"));
     $client->receiveMessage(["QueueUrl" => "queue"])->willReturn($responseMock);
     $this->receive("queue")->shouldBeArray([null, null]);
 }
 /**
  * @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#receivemessage
  * Will throw an exception if $amount is > 10.000
  *
  * @param int $amount
  * @param int $timeout seconds
  * @return nothing
  */
 public function consume($amount, $timeout = 0)
 {
     $limit = $amount > 0 ? $amount : $this->requestBatchSize;
     if ($timeout > 0) {
         $startTime = time();
         $remaining = $timeout;
     }
     $receiveParams = array('QueueUrl' => $this->queueUrl, 'MaxNumberOfMessages' => $limit, 'AttributeNames' => array('All'), 'MessageAttributeNames' => array('All'));
     while (true) {
         $reqTime = microtime(true);
         if ($timeout > 0) {
             // according to the spec, this is maximum wait time. If messages are available sooner, they get delivered immediately
             $receiveParams['WaitTimeSeconds'] = $remaining;
         }
         $result = $this->client->receiveMessage($receiveParams);
         $messages = $result->get('Messages');
         if (is_array($messages)) {
             foreach ($messages as $message) {
                 // How we implement routing keys with SQS: since it is not supported natively, we check if the route
                 // matches after having downloaded the message. If it does not match, we just skip processing it.
                 // Since we will not call deleteMessage, SQS will requeue the message in a short time.
                 // This is far from optimal, but it might be better than nothing
                 if (!$this->matchRoutingKey($message)) {
                     continue;
                 }
                 // removing the message from the queue is manual with SQS
                 $this->client->deleteMessage(array('QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $message['ReceiptHandle']));
                 $data = $message['Body'];
                 unset($message['Body']);
                 $contentType = isset($message['MessageAttributes'][$this->contentTypeAttribute]['StringValue']) ? $message['MessageAttributes'][$this->contentTypeAttribute]['StringValue'] : '';
                 if ($contentType != '') {
                     $this->callback->receive(new Message($data, $message, $contentType, $this->queueName));
                 } else {
                     if ($this->logger) {
                         $this->logger->warning('The SQS Consumer received a message with no content-type attribute. Assuming default');
                     }
                     $this->callback->receive(new Message($data, $message, null, $this->queueName));
                 }
             }
         }
         $this->maybeStopConsumer();
         if ($amount > 0) {
             return;
         }
         if ($timeout > 0 && ($remaining = $startTime + $timeout - time()) <= 0) {
             return;
         }
         /// @todo use a parameter to decide the polling interval
         // observe MAX 5 requests per sec per queue: sleep for 0.2 secs in between requests
         $passedMs = (microtime(true) - $reqTime) * 1000000;
         if ($passedMs < 200000) {
             usleep(200000 - $passedMs);
         }
     }
 }