Queue.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Illuminate\Queue;
  3. use DateTimeInterface;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Support\InteractsWithTime;
  6. abstract class Queue
  7. {
  8. use InteractsWithTime;
  9. /**
  10. * The IoC container instance.
  11. *
  12. * @var \Illuminate\Container\Container
  13. */
  14. protected $container;
  15. /**
  16. * The connection name for the queue.
  17. *
  18. * @var string
  19. */
  20. protected $connectionName;
  21. /**
  22. * Push a new job onto the queue.
  23. *
  24. * @param string $queue
  25. * @param string $job
  26. * @param mixed $data
  27. * @return mixed
  28. */
  29. public function pushOn($queue, $job, $data = '')
  30. {
  31. return $this->push($job, $data, $queue);
  32. }
  33. /**
  34. * Push a new job onto the queue after a delay.
  35. *
  36. * @param string $queue
  37. * @param \DateTimeInterface|\DateInterval|int $delay
  38. * @param string $job
  39. * @param mixed $data
  40. * @return mixed
  41. */
  42. public function laterOn($queue, $delay, $job, $data = '')
  43. {
  44. return $this->later($delay, $job, $data, $queue);
  45. }
  46. /**
  47. * Push an array of jobs onto the queue.
  48. *
  49. * @param array $jobs
  50. * @param mixed $data
  51. * @param string $queue
  52. * @return mixed
  53. */
  54. public function bulk($jobs, $data = '', $queue = null)
  55. {
  56. foreach ((array) $jobs as $job) {
  57. $this->push($job, $data, $queue);
  58. }
  59. }
  60. /**
  61. * Create a payload string from the given job and data.
  62. *
  63. * @param string $job
  64. * @param mixed $data
  65. * @return string
  66. *
  67. * @throws \Illuminate\Queue\InvalidPayloadException
  68. */
  69. protected function createPayload($job, $data = '')
  70. {
  71. $payload = json_encode($this->createPayloadArray($job, $data));
  72. if (JSON_ERROR_NONE !== json_last_error()) {
  73. throw new InvalidPayloadException(
  74. 'Unable to JSON encode payload. Error code: '.json_last_error()
  75. );
  76. }
  77. return $payload;
  78. }
  79. /**
  80. * Create a payload array from the given job and data.
  81. *
  82. * @param string $job
  83. * @param mixed $data
  84. * @return array
  85. */
  86. protected function createPayloadArray($job, $data = '')
  87. {
  88. return is_object($job)
  89. ? $this->createObjectPayload($job)
  90. : $this->createStringPayload($job, $data);
  91. }
  92. /**
  93. * Create a payload for an object-based queue handler.
  94. *
  95. * @param mixed $job
  96. * @return array
  97. */
  98. protected function createObjectPayload($job)
  99. {
  100. return [
  101. 'displayName' => $this->getDisplayName($job),
  102. 'job' => 'Illuminate\Queue\CallQueuedHandler@call',
  103. 'maxTries' => $job->tries ?? null,
  104. 'timeout' => $job->timeout ?? null,
  105. 'timeoutAt' => $this->getJobExpiration($job),
  106. 'data' => [
  107. 'commandName' => get_class($job),
  108. 'command' => serialize(clone $job),
  109. ],
  110. ];
  111. }
  112. /**
  113. * Get the display name for the given job.
  114. *
  115. * @param mixed $job
  116. * @return string
  117. */
  118. protected function getDisplayName($job)
  119. {
  120. return method_exists($job, 'displayName')
  121. ? $job->displayName() : get_class($job);
  122. }
  123. /**
  124. * Get the expiration timestamp for an object-based queue handler.
  125. *
  126. * @param mixed $job
  127. * @return mixed
  128. */
  129. public function getJobExpiration($job)
  130. {
  131. if (! method_exists($job, 'retryUntil') && ! isset($job->timeoutAt)) {
  132. return;
  133. }
  134. $expiration = $job->timeoutAt ?? $job->retryUntil();
  135. return $expiration instanceof DateTimeInterface
  136. ? $expiration->getTimestamp() : $expiration;
  137. }
  138. /**
  139. * Create a typical, string based queue payload array.
  140. *
  141. * @param string $job
  142. * @param mixed $data
  143. * @return array
  144. */
  145. protected function createStringPayload($job, $data)
  146. {
  147. return [
  148. 'displayName' => is_string($job) ? explode('@', $job)[0] : null,
  149. 'job' => $job, 'maxTries' => null,
  150. 'timeout' => null, 'data' => $data,
  151. ];
  152. }
  153. /**
  154. * Get the connection name for the queue.
  155. *
  156. * @return string
  157. */
  158. public function getConnectionName()
  159. {
  160. return $this->connectionName;
  161. }
  162. /**
  163. * Set the connection name for the queue.
  164. *
  165. * @param string $name
  166. * @return $this
  167. */
  168. public function setConnectionName($name)
  169. {
  170. $this->connectionName = $name;
  171. return $this;
  172. }
  173. /**
  174. * Set the IoC container instance.
  175. *
  176. * @param \Illuminate\Container\Container $container
  177. * @return void
  178. */
  179. public function setContainer(Container $container)
  180. {
  181. $this->container = $container;
  182. }
  183. }