ChainedBatch.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Illuminate\Bus;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Contracts\Bus\Dispatcher;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Support\Collection;
  9. use Throwable;
  10. class ChainedBatch implements ShouldQueue
  11. {
  12. use Batchable, Dispatchable, InteractsWithQueue, Queueable;
  13. /**
  14. * The collection of batched jobs.
  15. *
  16. * @var \Illuminate\Support\Collection
  17. */
  18. public Collection $jobs;
  19. /**
  20. * The name of the batch.
  21. *
  22. * @var string
  23. */
  24. public string $name;
  25. /**
  26. * The batch options.
  27. *
  28. * @var array
  29. */
  30. public array $options;
  31. /**
  32. * Create a new chained batch instance.
  33. *
  34. * @param \Illuminate\Bus\PendingBatch $batch
  35. */
  36. public function __construct(PendingBatch $batch)
  37. {
  38. $this->jobs = static::prepareNestedBatches($batch->jobs);
  39. $this->name = $batch->name;
  40. $this->options = $batch->options;
  41. $this->queue = $batch->queue();
  42. $this->connection = $batch->connection();
  43. }
  44. /**
  45. * Prepare any nested batches within the given collection of jobs.
  46. *
  47. * @param \Illuminate\Support\Collection $jobs
  48. * @return \Illuminate\Support\Collection
  49. */
  50. public static function prepareNestedBatches(Collection $jobs): Collection
  51. {
  52. return $jobs->map(fn ($job) => match (true) {
  53. is_array($job) => static::prepareNestedBatches(new Collection($job))->all(),
  54. $job instanceof Collection => static::prepareNestedBatches($job),
  55. $job instanceof PendingBatch => new ChainedBatch($job),
  56. default => $job,
  57. });
  58. }
  59. /**
  60. * Handle the job.
  61. *
  62. * @return void
  63. */
  64. public function handle()
  65. {
  66. $this->attachRemainderOfChainToEndOfBatch(
  67. $this->toPendingBatch()
  68. )->dispatch();
  69. }
  70. /**
  71. * Convert the chained batch instance into a pending batch.
  72. *
  73. * @return \Illuminate\Bus\PendingBatch
  74. */
  75. public function toPendingBatch()
  76. {
  77. $batch = Container::getInstance()->make(Dispatcher::class)->batch($this->jobs);
  78. $batch->name = $this->name;
  79. $batch->options = $this->options;
  80. if ($this->queue) {
  81. $batch->onQueue($this->queue);
  82. }
  83. if ($this->connection) {
  84. $batch->onConnection($this->connection);
  85. }
  86. foreach ($this->chainCatchCallbacks ?? [] as $callback) {
  87. $batch->catch(function (Batch $batch, ?Throwable $exception) use ($callback) {
  88. if (! $batch->allowsFailures()) {
  89. $callback($exception);
  90. }
  91. });
  92. }
  93. return $batch;
  94. }
  95. /**
  96. * Move the remainder of the chain to a "finally" batch callback.
  97. *
  98. * @param \Illuminate\Bus\PendingBatch $batch
  99. * @return \Illuminate\Bus\PendingBatch
  100. */
  101. protected function attachRemainderOfChainToEndOfBatch(PendingBatch $batch)
  102. {
  103. if (! empty($this->chained)) {
  104. $next = unserialize(array_shift($this->chained));
  105. $next->chained = $this->chained;
  106. $next->onConnection($next->connection ?: $this->chainConnection);
  107. $next->onQueue($next->queue ?: $this->chainQueue);
  108. $next->chainConnection = $this->chainConnection;
  109. $next->chainQueue = $this->chainQueue;
  110. $next->chainCatchCallbacks = $this->chainCatchCallbacks;
  111. $batch->finally(function (Batch $batch) use ($next) {
  112. if (! $batch->cancelled()) {
  113. Container::getInstance()->make(Dispatcher::class)->dispatch($next);
  114. }
  115. });
  116. $this->chained = [];
  117. }
  118. return $batch;
  119. }
  120. }