BeanstalkdJob.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Illuminate\Queue\Jobs;
  3. use Pheanstalk\Pheanstalk;
  4. use Illuminate\Container\Container;
  5. use Pheanstalk\Job as PheanstalkJob;
  6. use Illuminate\Contracts\Queue\Job as JobContract;
  7. class BeanstalkdJob extends Job implements JobContract
  8. {
  9. /**
  10. * The Pheanstalk instance.
  11. *
  12. * @var \Pheanstalk\Pheanstalk
  13. */
  14. protected $pheanstalk;
  15. /**
  16. * The Pheanstalk job instance.
  17. *
  18. * @var \Pheanstalk\Job
  19. */
  20. protected $job;
  21. /**
  22. * Create a new job instance.
  23. *
  24. * @param \Illuminate\Container\Container $container
  25. * @param \Pheanstalk\Pheanstalk $pheanstalk
  26. * @param \Pheanstalk\Job $job
  27. * @param string $connectionName
  28. * @param string $queue
  29. * @return void
  30. */
  31. public function __construct(Container $container, Pheanstalk $pheanstalk, PheanstalkJob $job, $connectionName, $queue)
  32. {
  33. $this->job = $job;
  34. $this->queue = $queue;
  35. $this->container = $container;
  36. $this->pheanstalk = $pheanstalk;
  37. $this->connectionName = $connectionName;
  38. }
  39. /**
  40. * Release the job back into the queue.
  41. *
  42. * @param int $delay
  43. * @return void
  44. */
  45. public function release($delay = 0)
  46. {
  47. parent::release($delay);
  48. $priority = Pheanstalk::DEFAULT_PRIORITY;
  49. $this->pheanstalk->release($this->job, $priority, $delay);
  50. }
  51. /**
  52. * Bury the job in the queue.
  53. *
  54. * @return void
  55. */
  56. public function bury()
  57. {
  58. parent::release();
  59. $this->pheanstalk->bury($this->job);
  60. }
  61. /**
  62. * Delete the job from the queue.
  63. *
  64. * @return void
  65. */
  66. public function delete()
  67. {
  68. parent::delete();
  69. $this->pheanstalk->delete($this->job);
  70. }
  71. /**
  72. * Get the number of times the job has been attempted.
  73. *
  74. * @return int
  75. */
  76. public function attempts()
  77. {
  78. $stats = $this->pheanstalk->statsJob($this->job);
  79. return (int) $stats->reserves;
  80. }
  81. /**
  82. * Get the job identifier.
  83. *
  84. * @return int
  85. */
  86. public function getJobId()
  87. {
  88. return $this->job->getId();
  89. }
  90. /**
  91. * Get the raw body string for the job.
  92. *
  93. * @return string
  94. */
  95. public function getRawBody()
  96. {
  97. return $this->job->getData();
  98. }
  99. /**
  100. * Get the underlying Pheanstalk instance.
  101. *
  102. * @return \Pheanstalk\Pheanstalk
  103. */
  104. public function getPheanstalk()
  105. {
  106. return $this->pheanstalk;
  107. }
  108. /**
  109. * Get the underlying Pheanstalk job.
  110. *
  111. * @return \Pheanstalk\Job
  112. */
  113. public function getPheanstalkJob()
  114. {
  115. return $this->job;
  116. }
  117. }