-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Allow placing a batch on a chain (#48633)
* Allow placing a batch on a chain * missing file * Chains of batches of chains of batches * remove auto-format * remove auto-format * just collection * chained comes from queueable * better function name * reflop * messed up indentation * styleci * allow batch to fail but chain to succeed * formatting * formatting * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
f171d70
commit e5e67c4
Showing
3 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Illuminate\Bus; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Contracts\Bus\Dispatcher; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Collection; | ||
use Throwable; | ||
|
||
class ChainedBatch implements ShouldQueue | ||
{ | ||
use Batchable, Dispatchable, InteractsWithQueue, Queueable; | ||
|
||
/** | ||
* The collection of batched jobs. | ||
* | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
public Collection $jobs; | ||
|
||
/** | ||
* The name of the batch. | ||
* | ||
* @var string | ||
*/ | ||
public string $name; | ||
|
||
/** | ||
* The batch options. | ||
* | ||
* @var array | ||
*/ | ||
public array $options; | ||
|
||
/** | ||
* Create a new chained batch instance. | ||
* | ||
* @param \Illuminate\Bus\PendingBatch $batch | ||
* @return void | ||
*/ | ||
public function __construct(PendingBatch $batch) | ||
{ | ||
$this->jobs = static::prepareNestedBatches($batch->jobs); | ||
|
||
$this->name = $batch->name; | ||
$this->options = $batch->options; | ||
} | ||
|
||
/** | ||
* Prepare any nested batches within the given collection of jobs. | ||
* | ||
* @param \Illuminate\Support\Collection $jobs | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public static function prepareNestedBatches(Collection $jobs): Collection | ||
{ | ||
return $jobs->map(fn ($job) => match (true) { | ||
is_array($job) => static::prepareNestedBatches(collect($job))->all(), | ||
$job instanceof Collection => static::prepareNestedBatches($job), | ||
$job instanceof PendingBatch => new ChainedBatch($job), | ||
default => $job, | ||
}); | ||
} | ||
|
||
/** | ||
* Handle the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$batch = new PendingBatch(Container::getInstance(), $this->jobs); | ||
|
||
$batch->name = $this->name; | ||
$batch->options = $this->options; | ||
|
||
if ($this->queue) { | ||
$batch->onQueue($this->queue); | ||
} | ||
|
||
if ($this->connection) { | ||
$batch->onConnection($this->connection); | ||
} | ||
|
||
$this->dispatchRemainderOfChainAfterBatch($batch); | ||
|
||
foreach ($this->chainCatchCallbacks ?? [] as $callback) { | ||
$batch->catch(function (Batch $batch, ?Throwable $exception) use ($callback) { | ||
if (! $batch->allowsFailures()) { | ||
$callback($exception); | ||
} | ||
}); | ||
} | ||
|
||
$batch->dispatch(); | ||
} | ||
|
||
/** | ||
* Move the remainder of the chain to a "finally" batch callback. | ||
* | ||
* @param \Illuminate\Bus\PendingBatch $batch | ||
* @return | ||
*/ | ||
protected function dispatchRemainderOfChainAfterBatch(PendingBatch $batch) | ||
{ | ||
if (! empty($this->chained)) { | ||
$next = unserialize(array_shift($this->chained)); | ||
|
||
$next->chained = $this->chained; | ||
|
||
$next->onConnection($next->connection ?: $this->chainConnection); | ||
$next->onQueue($next->queue ?: $this->chainQueue); | ||
|
||
$next->chainConnection = $this->chainConnection; | ||
$next->chainQueue = $this->chainQueue; | ||
$next->chainCatchCallbacks = $this->chainCatchCallbacks; | ||
|
||
$batch->finally(function (Batch $batch) use ($next) { | ||
if (! $batch->cancelled()) { | ||
Container::getInstance()->make(Dispatcher::class)->dispatch($next); | ||
} | ||
}); | ||
|
||
$this->chained = []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.