/vendor/laravel/framework/src/Illuminate/Queue/Queue.php

https://gitlab.com/madwanz64/laravel · PHP · 280 lines · 129 code · 30 blank · 121 comment · 8 complexity · 60868dff5c110f7c955be526ed06e4f4 MD5 · raw file

  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. * The create payload callbacks.
  23. *
  24. * @var callable[]
  25. */
  26. protected static $createPayloadCallbacks = [];
  27. /**
  28. * Push a new job onto the queue.
  29. *
  30. * @param string $queue
  31. * @param string $job
  32. * @param mixed $data
  33. * @return mixed
  34. */
  35. public function pushOn($queue, $job, $data = '')
  36. {
  37. return $this->push($job, $data, $queue);
  38. }
  39. /**
  40. * Push a new job onto the queue after a delay.
  41. *
  42. * @param string $queue
  43. * @param \DateTimeInterface|\DateInterval|int $delay
  44. * @param string $job
  45. * @param mixed $data
  46. * @return mixed
  47. */
  48. public function laterOn($queue, $delay, $job, $data = '')
  49. {
  50. return $this->later($delay, $job, $data, $queue);
  51. }
  52. /**
  53. * Push an array of jobs onto the queue.
  54. *
  55. * @param array $jobs
  56. * @param mixed $data
  57. * @param string|null $queue
  58. * @return void
  59. */
  60. public function bulk($jobs, $data = '', $queue = null)
  61. {
  62. foreach ((array) $jobs as $job) {
  63. $this->push($job, $data, $queue);
  64. }
  65. }
  66. /**
  67. * Create a payload string from the given job and data.
  68. *
  69. * @param string|object $job
  70. * @param string $queue
  71. * @param mixed $data
  72. * @return string
  73. *
  74. * @throws \Illuminate\Queue\InvalidPayloadException
  75. */
  76. protected function createPayload($job, $queue, $data = '')
  77. {
  78. $payload = json_encode($this->createPayloadArray($job, $queue, $data));
  79. if (JSON_ERROR_NONE !== json_last_error()) {
  80. throw new InvalidPayloadException(
  81. 'Unable to JSON encode payload. Error code: '.json_last_error()
  82. );
  83. }
  84. return $payload;
  85. }
  86. /**
  87. * Create a payload array from the given job and data.
  88. *
  89. * @param string|object $job
  90. * @param string $queue
  91. * @param mixed $data
  92. * @return array
  93. */
  94. protected function createPayloadArray($job, $queue, $data = '')
  95. {
  96. return is_object($job)
  97. ? $this->createObjectPayload($job, $queue)
  98. : $this->createStringPayload($job, $queue, $data);
  99. }
  100. /**
  101. * Create a payload for an object-based queue handler.
  102. *
  103. * @param object $job
  104. * @param string $queue
  105. * @return array
  106. */
  107. protected function createObjectPayload($job, $queue)
  108. {
  109. $payload = $this->withCreatePayloadHooks($queue, [
  110. 'displayName' => $this->getDisplayName($job),
  111. 'job' => 'Illuminate\Queue\CallQueuedHandler@call',
  112. 'maxTries' => $job->tries ?? null,
  113. 'delay' => $this->getJobRetryDelay($job),
  114. 'timeout' => $job->timeout ?? null,
  115. 'timeoutAt' => $this->getJobExpiration($job),
  116. 'data' => [
  117. 'commandName' => $job,
  118. 'command' => $job,
  119. ],
  120. ]);
  121. return array_merge($payload, [
  122. 'data' => [
  123. 'commandName' => get_class($job),
  124. 'command' => serialize(clone $job),
  125. ],
  126. ]);
  127. }
  128. /**
  129. * Get the display name for the given job.
  130. *
  131. * @param object $job
  132. * @return string
  133. */
  134. protected function getDisplayName($job)
  135. {
  136. return method_exists($job, 'displayName')
  137. ? $job->displayName() : get_class($job);
  138. }
  139. /**
  140. * Get the retry delay for an object-based queue handler.
  141. *
  142. * @param mixed $job
  143. * @return mixed
  144. */
  145. public function getJobRetryDelay($job)
  146. {
  147. if (! method_exists($job, 'retryAfter') && ! isset($job->retryAfter)) {
  148. return;
  149. }
  150. $delay = $job->retryAfter ?? $job->retryAfter();
  151. return $delay instanceof DateTimeInterface
  152. ? $this->secondsUntil($delay) : $delay;
  153. }
  154. /**
  155. * Get the expiration timestamp for an object-based queue handler.
  156. *
  157. * @param mixed $job
  158. * @return mixed
  159. */
  160. public function getJobExpiration($job)
  161. {
  162. if (! method_exists($job, 'retryUntil') && ! isset($job->timeoutAt)) {
  163. return;
  164. }
  165. $expiration = $job->timeoutAt ?? $job->retryUntil();
  166. return $expiration instanceof DateTimeInterface
  167. ? $expiration->getTimestamp() : $expiration;
  168. }
  169. /**
  170. * Create a typical, string based queue payload array.
  171. *
  172. * @param string $job
  173. * @param string $queue
  174. * @param mixed $data
  175. * @return array
  176. */
  177. protected function createStringPayload($job, $queue, $data)
  178. {
  179. return $this->withCreatePayloadHooks($queue, [
  180. 'displayName' => is_string($job) ? explode('@', $job)[0] : null,
  181. 'job' => $job,
  182. 'maxTries' => null,
  183. 'delay' => null,
  184. 'timeout' => null,
  185. 'data' => $data,
  186. ]);
  187. }
  188. /**
  189. * Register a callback to be executed when creating job payloads.
  190. *
  191. * @param callable $callback
  192. * @return void
  193. */
  194. public static function createPayloadUsing($callback)
  195. {
  196. if (is_null($callback)) {
  197. static::$createPayloadCallbacks = [];
  198. } else {
  199. static::$createPayloadCallbacks[] = $callback;
  200. }
  201. }
  202. /**
  203. * Create the given payload using any registered payload hooks.
  204. *
  205. * @param string $queue
  206. * @param array $payload
  207. * @return array
  208. */
  209. protected function withCreatePayloadHooks($queue, array $payload)
  210. {
  211. if (! empty(static::$createPayloadCallbacks)) {
  212. foreach (static::$createPayloadCallbacks as $callback) {
  213. $payload = array_merge($payload, call_user_func(
  214. $callback, $this->getConnectionName(), $queue, $payload
  215. ));
  216. }
  217. }
  218. return $payload;
  219. }
  220. /**
  221. * Get the connection name for the queue.
  222. *
  223. * @return string
  224. */
  225. public function getConnectionName()
  226. {
  227. return $this->connectionName;
  228. }
  229. /**
  230. * Set the connection name for the queue.
  231. *
  232. * @param string $name
  233. * @return $this
  234. */
  235. public function setConnectionName($name)
  236. {
  237. $this->connectionName = $name;
  238. return $this;
  239. }
  240. /**
  241. * Set the IoC container instance.
  242. *
  243. * @param \Illuminate\Container\Container $container
  244. * @return void
  245. */
  246. public function setContainer(Container $container)
  247. {
  248. $this->container = $container;
  249. }
  250. }