RedisJob.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Illuminate\Queue\Jobs;
  3. use Illuminate\Queue\RedisQueue;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Queue\Job as JobContract;
  6. class RedisJob extends Job implements JobContract
  7. {
  8. /**
  9. * The Redis queue instance.
  10. *
  11. * @var \Illuminate\Queue\RedisQueue
  12. */
  13. protected $redis;
  14. /**
  15. * The Redis raw job payload.
  16. *
  17. * @var string
  18. */
  19. protected $job;
  20. /**
  21. * The JSON decoded version of "$job".
  22. *
  23. * @var array
  24. */
  25. protected $decoded;
  26. /**
  27. * The Redis job payload inside the reserved queue.
  28. *
  29. * @var string
  30. */
  31. protected $reserved;
  32. /**
  33. * Create a new job instance.
  34. *
  35. * @param \Illuminate\Container\Container $container
  36. * @param \Illuminate\Queue\RedisQueue $redis
  37. * @param string $job
  38. * @param string $reserved
  39. * @param string $connectionName
  40. * @param string $queue
  41. * @return void
  42. */
  43. public function __construct(Container $container, RedisQueue $redis, $job, $reserved, $connectionName, $queue)
  44. {
  45. // The $job variable is the original job JSON as it existed in the ready queue while
  46. // the $reserved variable is the raw JSON in the reserved queue. The exact format
  47. // of the reserved job is required in order for us to properly delete its data.
  48. $this->job = $job;
  49. $this->redis = $redis;
  50. $this->queue = $queue;
  51. $this->reserved = $reserved;
  52. $this->container = $container;
  53. $this->connectionName = $connectionName;
  54. $this->decoded = $this->payload();
  55. }
  56. /**
  57. * Get the raw body string for the job.
  58. *
  59. * @return string
  60. */
  61. public function getRawBody()
  62. {
  63. return $this->job;
  64. }
  65. /**
  66. * Delete the job from the queue.
  67. *
  68. * @return void
  69. */
  70. public function delete()
  71. {
  72. parent::delete();
  73. $this->redis->deleteReserved($this->queue, $this);
  74. }
  75. /**
  76. * Release the job back into the queue.
  77. *
  78. * @param int $delay
  79. * @return void
  80. */
  81. public function release($delay = 0)
  82. {
  83. parent::release($delay);
  84. $this->redis->deleteAndRelease($this->queue, $this, $delay);
  85. }
  86. /**
  87. * Get the number of times the job has been attempted.
  88. *
  89. * @return int
  90. */
  91. public function attempts()
  92. {
  93. return ($this->decoded['attempts'] ?? null) + 1;
  94. }
  95. /**
  96. * Get the job identifier.
  97. *
  98. * @return string
  99. */
  100. public function getJobId()
  101. {
  102. return $this->decoded['id'] ?? null;
  103. }
  104. /**
  105. * Get the underlying Redis factory implementation.
  106. *
  107. * @return \Illuminate\Contracts\Redis\Factory
  108. */
  109. public function getRedisQueue()
  110. {
  111. return $this->redis;
  112. }
  113. /**
  114. * Get the underlying reserved Redis job.
  115. *
  116. * @return string
  117. */
  118. public function getReservedJob()
  119. {
  120. return $this->reserved;
  121. }
  122. }