ChainedBatch.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @return void
  36. */
  37. public function __construct(PendingBatch $batch)
  38. {
  39. $this->jobs = static::prepareNestedBatches($batch->jobs);
  40. $this->name = $batch->name;
  41. $this->options = $batch->options;
  42. }
  43. /**
  44. * Prepare any nested batches within the given collection of jobs.
  45. *
  46. * @param \Illuminate\Support\Collection $jobs
  47. * @return \Illuminate\Support\Collection
  48. */
  49. public static function prepareNestedBatches(Collection $jobs): Collection
  50. {
  51. return $jobs->map(fn ($job) => match (true) {
  52. is_array($job) => static::prepareNestedBatches(collect($job))->all(),
  53. $job instanceof Collection => static::prepareNestedBatches($job),
  54. $job instanceof PendingBatch => new ChainedBatch($job),
  55. default => $job,
  56. });
  57. }
  58. /**
  59. * Handle the job.
  60. *
  61. * @return void
  62. */
  63. public function handle()
  64. {
  65. $this->attachRemainderOfChainToEndOfBatch(
  66. $this->toPendingBatch()
  67. )->dispatch();
  68. }
  69. /**
  70. * Convert the chained batch instance into a pending batch.
  71. *
  72. * @return \Illuminate\Bus\PendingBatch
  73. */
  74. public function toPendingBatch()
  75. {
  76. $batch = Container::getInstance()->make(Dispatcher::class)->batch($this->jobs);
  77. $batch->name = $this->name;
  78. $batch->options = $this->options;
  79. if ($this->queue) {
  80. $batch->onQueue($this->queue);
  81. }
  82. if ($this->connection) {
  83. $batch->onConnection($this->connection);
  84. }
  85. foreach ($this->chainCatchCallbacks ?? [] as $callback) {
  86. $batch->catch(function (Batch $batch, ?Throwable $exception) use ($callback) {
  87. if (! $batch->allowsFailures()) {
  88. $callback($exception);
  89. }
  90. });
  91. }
  92. return $batch;
  93. }
  94. /**
  95. * Move the remainder of the chain to a "finally" batch callback.
  96. *
  97. * @param \Illuminate\Bus\PendingBatch $batch
  98. * @return \Illuminate\Bus\PendingBatch
  99. */
  100. protected function attachRemainderOfChainToEndOfBatch(PendingBatch $batch)
  101. {
  102. if (! empty($this->chained)) {
  103. $next = unserialize(array_shift($this->chained));
  104. $next->chained = $this->chained;
  105. $next->onConnection($next->connection ?: $this->chainConnection);
  106. $next->onQueue($next->queue ?: $this->chainQueue);
  107. $next->chainConnection = $this->chainConnection;
  108. $next->chainQueue = $this->chainQueue;
  109. $next->chainCatchCallbacks = $this->chainCatchCallbacks;
  110. $batch->finally(function (Batch $batch) use ($next) {
  111. if (! $batch->cancelled()) {
  112. Container::getInstance()->make(Dispatcher::class)->dispatch($next);
  113. }
  114. });
  115. $this->chained = [];
  116. }
  117. return $batch;
  118. }
  119. }