Queueable.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Bus;
  3. trait Queueable
  4. {
  5. /**
  6. * The name of the connection the job should be sent to.
  7. *
  8. * @var string|null
  9. */
  10. public $connection;
  11. /**
  12. * The name of the queue the job should be sent to.
  13. *
  14. * @var string|null
  15. */
  16. public $queue;
  17. /**
  18. * The name of the connection the chain should be sent to.
  19. *
  20. * @var string|null
  21. */
  22. public $chainConnection;
  23. /**
  24. * The name of the queue the chain should be sent to.
  25. *
  26. * @var string|null
  27. */
  28. public $chainQueue;
  29. /**
  30. * The number of seconds before the job should be made available.
  31. *
  32. * @var \DateTimeInterface|\DateInterval|int|null
  33. */
  34. public $delay;
  35. /**
  36. * The jobs that should run if this job is successful.
  37. *
  38. * @var array
  39. */
  40. public $chained = [];
  41. /**
  42. * Set the desired connection for the job.
  43. *
  44. * @param string|null $connection
  45. * @return $this
  46. */
  47. public function onConnection($connection)
  48. {
  49. $this->connection = $connection;
  50. return $this;
  51. }
  52. /**
  53. * Set the desired queue for the job.
  54. *
  55. * @param string|null $queue
  56. * @return $this
  57. */
  58. public function onQueue($queue)
  59. {
  60. $this->queue = $queue;
  61. return $this;
  62. }
  63. /**
  64. * Set the desired connection for the chain.
  65. *
  66. * @param string|null $connection
  67. * @return $this
  68. */
  69. public function allOnConnection($connection)
  70. {
  71. $this->chainConnection = $connection;
  72. $this->connection = $connection;
  73. return $this;
  74. }
  75. /**
  76. * Set the desired queue for the chain.
  77. *
  78. * @param string|null $queue
  79. * @return $this
  80. */
  81. public function allOnQueue($queue)
  82. {
  83. $this->chainQueue = $queue;
  84. $this->queue = $queue;
  85. return $this;
  86. }
  87. /**
  88. * Set the desired delay for the job.
  89. *
  90. * @param \DateTimeInterface|\DateInterval|int|null $delay
  91. * @return $this
  92. */
  93. public function delay($delay)
  94. {
  95. $this->delay = $delay;
  96. return $this;
  97. }
  98. /**
  99. * Set the jobs that should run if this job is successful.
  100. *
  101. * @param array $chain
  102. * @return $this
  103. */
  104. public function chain($chain)
  105. {
  106. $this->chained = collect($chain)->map(function ($job) {
  107. return serialize($job);
  108. })->all();
  109. return $this;
  110. }
  111. /**
  112. * Dispatch the next job on the chain.
  113. *
  114. * @return void
  115. */
  116. public function dispatchNextJobInChain()
  117. {
  118. if (! empty($this->chained)) {
  119. dispatch(tap(unserialize(array_shift($this->chained)), function ($next) {
  120. $next->chained = $this->chained;
  121. $next->onConnection($next->connection ?: $this->chainConnection);
  122. $next->onQueue($next->queue ?: $this->chainQueue);
  123. $next->chainConnection = $this->chainConnection;
  124. $next->chainQueue = $this->chainQueue;
  125. }));
  126. }
  127. }
  128. }