Example #1
0
 /**
  * {@inheritDoc}
  */
 public function update(QueueAdapterInterface $queue)
 {
     if (!$queue instanceof SqsQueue) {
         throw new InvalidArgumentException('expecting an instance of SqsQueue');
     }
     // Update queue attributes if required
     $Attributes = $queue->getConfig()->toAttributes();
     $QueueUrl = $queue->getUrl();
     $this->client->setQueueAttributes(compact('QueueUrl', 'Attributes'));
 }
Example #2
0
 /**
  * Creates an SQS Queue and returns the Queue Url
  *
  * The create method for SQS Queues is idempotent - if the queue already
  * exists, this method will return the Queue Url of the existing Queue.
  *
  * @return string
  */
 public function createQueue()
 {
     $result = $this->sqs->createQueue(['QueueName' => $this->getNameWithPrefix(), 'Attributes' => ['VisibilityTimeout' => $this->options['message_timeout'], 'MessageRetentionPeriod' => $this->options['message_expiration'], 'ReceiveMessageWaitTimeSeconds' => $this->options['receive_wait_time']]]);
     $this->queueUrl = $result->get('QueueUrl');
     $key = $this->getNameWithPrefix() . '_url';
     $this->cache->save($key, $this->queueUrl);
     $this->log(200, "Created SQS Queue", ['QueueUrl' => $this->queueUrl]);
     if ($this->options['push_notifications']) {
         $policy = $this->createSqsPolicy();
         $this->sqs->setQueueAttributes(['QueueUrl' => $this->queueUrl, 'Attributes' => ['Policy' => $policy]]);
         $this->log(200, "Created Updated SQS Policy");
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $noWatch = $input->getOption('no-watch');
     $queueId = $this->getQueueId();
     $snsConfig = $this->getContainer()->getParameter('sns');
     $region = isset($snsConfig['region']) ? $snsConfig['region'] : 'us-east-1';
     /** @var QueueFactory $queueFactory */
     $queueFactory = $this->getContainer()->get('syrup.queue_factory');
     $sqs = $queueFactory->create($queueId, $region, $snsConfig['key'], $snsConfig['secret']);
     /** @var Connection $conn */
     $conn = $this->getContainer()->get('doctrine.dbal.syrup_connection');
     $stmt = $conn->query("SELECT * FROM queues WHERE id='{$queueId}'");
     $res = $stmt->fetchAll();
     if (empty($res)) {
         $conn->insert('queues', ['id' => $queueId, 'access_key' => $snsConfig['key'], 'secret_key' => $snsConfig['secret'], 'region' => $region, 'url' => $sqs->get('QueueUrl')]);
     }
     $sqsClient = new SqsClient(['region' => $region, 'version' => '2012-11-05', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
     $sqsArn = $sqsClient->getQueueArn($sqs->get('QueueUrl'));
     // subscribe SQS to SNS
     $snsClient = new SnsClient(['region' => $region, 'version' => '2010-03-31', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
     $snsClient->subscribe(['TopicArn' => $snsConfig['topic_arn'], 'Protocol' => 'sqs', 'Endpoint' => $sqsArn]);
     // add policy to SQS to allow SNS sending messages to it
     $sqsPolicy = '{
       "Version": "2008-10-17",
       "Id": "' . $sqsArn . '/SQSDefaultPolicy",
       "Statement": [
         {
           "Sid": "sqs-sns",
           "Effect": "Allow",
           "Principal": {
             "AWS": "*"
           },
           "Action": "SQS:SendMessage",
           "Resource": "' . $sqsArn . '",
           "Condition": {
             "ArnEquals": {
               "aws:SourceArn": "' . $snsConfig['topic_arn'] . '"
             }
           }
         }
       ]
     }';
     $sqsClient->setQueueAttributes(['QueueUrl' => $sqs->get('QueueUrl'), 'Attributes' => ['Policy' => $sqsPolicy]]);
     $output->writeln("SQS created and registered to SNS");
     // Add Cloudwatch alarm
     if (!$noWatch) {
         $cwClient = new CloudWatchClient(['region' => $region, 'version' => '2010-08-01', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
         $cwClient->putMetricAlarm(['AlarmName' => sprintf('Syrup %s queue is full', $queueId), 'ActionsEnabled' => true, 'AlarmActions' => [$snsConfig['alarm_topic_arn']], 'MetricName' => 'ApproximateNumberOfMessagesVisible', 'Namespace' => 'AWS/SQS', 'Statistic' => 'Average', 'Dimensions' => [['Name' => 'QueueName', 'Value' => $queueId]], 'Period' => 300, 'EvaluationPeriods' => 1, 'Threshold' => 5, 'ComparisonOperator' => 'GreaterThanOrEqualToThreshold']);
         $output->writeln("Cloudwatch alarm created");
     }
 }
Example #4
0
 public function setAttributes(array $attributes)
 {
     $args = ['QueueUrl' => $this->getQueueUrl()];
     if ($attributes) {
         /** @noinspection PhpUnusedLocalVariableInspection */
         foreach ($attributes as $k => &$v) {
             if (!in_array($k, self::MUTABLE_ATTRIBUTES)) {
                 throw new \InvalidArgumentException("Unknown attribute {$k}");
             }
         }
         $args['Attributes'] = $attributes;
     } else {
         throw new \InvalidArgumentException("You must specify some attributes");
     }
     $this->client->setQueueAttributes($args);
 }
Example #5
0
 private function createQueue()
 {
     $result = $this->client->createQueue(array('QueueName' => $this->queueName));
     $this->queueUrl = $result->get('QueueUrl');
     $this->client->setQueueAttributes(array('QueueUrl' => $this->queueUrl, 'Attributes' => array('VisibilityTimeout' => $this->visibilityTimeout)));
 }