SqsQueue.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Aws\Sqs\SqsClient;
  4. use Illuminate\Queue\Jobs\SqsJob;
  5. use Illuminate\Contracts\Queue\Queue as QueueContract;
  6. class SqsQueue extends Queue implements QueueContract
  7. {
  8. /**
  9. * The Amazon SQS instance.
  10. *
  11. * @var \Aws\Sqs\SqsClient
  12. */
  13. protected $sqs;
  14. /**
  15. * The name of the default queue.
  16. *
  17. * @var string
  18. */
  19. protected $default;
  20. /**
  21. * The queue URL prefix.
  22. *
  23. * @var string
  24. */
  25. protected $prefix;
  26. /**
  27. * Create a new Amazon SQS queue instance.
  28. *
  29. * @param \Aws\Sqs\SqsClient $sqs
  30. * @param string $default
  31. * @param string $prefix
  32. * @return void
  33. */
  34. public function __construct(SqsClient $sqs, $default, $prefix = '')
  35. {
  36. $this->sqs = $sqs;
  37. $this->prefix = $prefix;
  38. $this->default = $default;
  39. }
  40. /**
  41. * Get the size of the queue.
  42. *
  43. * @param string $queue
  44. * @return int
  45. */
  46. public function size($queue = null)
  47. {
  48. $response = $this->sqs->getQueueAttributes([
  49. 'QueueUrl' => $this->getQueue($queue),
  50. 'AttributeNames' => ['ApproximateNumberOfMessages'],
  51. ]);
  52. $attributes = $response->get('Attributes');
  53. return (int) $attributes['ApproximateNumberOfMessages'];
  54. }
  55. /**
  56. * Push a new job onto the queue.
  57. *
  58. * @param string $job
  59. * @param mixed $data
  60. * @param string $queue
  61. * @return mixed
  62. */
  63. public function push($job, $data = '', $queue = null)
  64. {
  65. return $this->pushRaw($this->createPayload($job, $data), $queue);
  66. }
  67. /**
  68. * Push a raw payload onto the queue.
  69. *
  70. * @param string $payload
  71. * @param string $queue
  72. * @param array $options
  73. * @return mixed
  74. */
  75. public function pushRaw($payload, $queue = null, array $options = [])
  76. {
  77. return $this->sqs->sendMessage([
  78. 'QueueUrl' => $this->getQueue($queue), 'MessageBody' => $payload,
  79. ])->get('MessageId');
  80. }
  81. /**
  82. * Push a new job onto the queue after a delay.
  83. *
  84. * @param \DateTimeInterface|\DateInterval|int $delay
  85. * @param string $job
  86. * @param mixed $data
  87. * @param string $queue
  88. * @return mixed
  89. */
  90. public function later($delay, $job, $data = '', $queue = null)
  91. {
  92. return $this->sqs->sendMessage([
  93. 'QueueUrl' => $this->getQueue($queue),
  94. 'MessageBody' => $this->createPayload($job, $data),
  95. 'DelaySeconds' => $this->secondsUntil($delay),
  96. ])->get('MessageId');
  97. }
  98. /**
  99. * Pop the next job off of the queue.
  100. *
  101. * @param string $queue
  102. * @return \Illuminate\Contracts\Queue\Job|null
  103. */
  104. public function pop($queue = null)
  105. {
  106. $response = $this->sqs->receiveMessage([
  107. 'QueueUrl' => $queue = $this->getQueue($queue),
  108. 'AttributeNames' => ['ApproximateReceiveCount'],
  109. ]);
  110. if (! is_null($response['Messages']) && count($response['Messages']) > 0) {
  111. return new SqsJob(
  112. $this->container, $this->sqs, $response['Messages'][0],
  113. $this->connectionName, $queue
  114. );
  115. }
  116. }
  117. /**
  118. * Get the queue or return the default.
  119. *
  120. * @param string|null $queue
  121. * @return string
  122. */
  123. public function getQueue($queue)
  124. {
  125. $queue = $queue ?: $this->default;
  126. return filter_var($queue, FILTER_VALIDATE_URL) === false
  127. ? rtrim($this->prefix, '/').'/'.$queue : $queue;
  128. }
  129. /**
  130. * Get the underlying SQS instance.
  131. *
  132. * @return \Aws\Sqs\SqsClient
  133. */
  134. public function getSqs()
  135. {
  136. return $this->sqs;
  137. }
  138. }