SqsJob.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Illuminate\Queue\Jobs;
  3. use Aws\Sqs\SqsClient;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Queue\Job as JobContract;
  6. class SqsJob extends Job implements JobContract
  7. {
  8. /**
  9. * The Amazon SQS client instance.
  10. *
  11. * @var \Aws\Sqs\SqsClient
  12. */
  13. protected $sqs;
  14. /**
  15. * The Amazon SQS job instance.
  16. *
  17. * @var array
  18. */
  19. protected $job;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @param \Illuminate\Container\Container $container
  24. * @param \Aws\Sqs\SqsClient $sqs
  25. * @param array $job
  26. * @param string $connectionName
  27. * @param string $queue
  28. * @return void
  29. */
  30. public function __construct(Container $container, SqsClient $sqs, array $job, $connectionName, $queue)
  31. {
  32. $this->sqs = $sqs;
  33. $this->job = $job;
  34. $this->queue = $queue;
  35. $this->container = $container;
  36. $this->connectionName = $connectionName;
  37. }
  38. /**
  39. * Release the job back into the queue.
  40. *
  41. * @param int $delay
  42. * @return void
  43. */
  44. public function release($delay = 0)
  45. {
  46. parent::release($delay);
  47. $this->sqs->changeMessageVisibility([
  48. 'QueueUrl' => $this->queue,
  49. 'ReceiptHandle' => $this->job['ReceiptHandle'],
  50. 'VisibilityTimeout' => $delay,
  51. ]);
  52. }
  53. /**
  54. * Delete the job from the queue.
  55. *
  56. * @return void
  57. */
  58. public function delete()
  59. {
  60. parent::delete();
  61. $this->sqs->deleteMessage([
  62. 'QueueUrl' => $this->queue, 'ReceiptHandle' => $this->job['ReceiptHandle'],
  63. ]);
  64. }
  65. /**
  66. * Get the number of times the job has been attempted.
  67. *
  68. * @return int
  69. */
  70. public function attempts()
  71. {
  72. return (int) $this->job['Attributes']['ApproximateReceiveCount'];
  73. }
  74. /**
  75. * Get the job identifier.
  76. *
  77. * @return string
  78. */
  79. public function getJobId()
  80. {
  81. return $this->job['MessageId'];
  82. }
  83. /**
  84. * Get the raw body string for the job.
  85. *
  86. * @return string
  87. */
  88. public function getRawBody()
  89. {
  90. return $this->job['Body'];
  91. }
  92. /**
  93. * Get the underlying SQS client instance.
  94. *
  95. * @return \Aws\Sqs\SqsClient
  96. */
  97. public function getSqs()
  98. {
  99. return $this->sqs;
  100. }
  101. /**
  102. * Get the underlying raw SQS job.
  103. *
  104. * @return array
  105. */
  106. public function getSqsJob()
  107. {
  108. return $this->job;
  109. }
  110. }