-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathChainedBatch.php
142 lines (119 loc) · 3.72 KB
/
ChainedBatch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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(new Collection($job))->all(),
$job instanceof Collection => static::prepareNestedBatches($job),
$job instanceof PendingBatch => new ChainedBatch($job),
default => $job,
});
}
/**
* Handle the job.
*
* @return void
*/
public function handle()
{
$this->attachRemainderOfChainToEndOfBatch(
$this->toPendingBatch()
)->dispatch();
}
/**
* Convert the chained batch instance into a pending batch.
*
* @return \Illuminate\Bus\PendingBatch
*/
public function toPendingBatch()
{
$batch = Container::getInstance()->make(Dispatcher::class)->batch($this->jobs);
$batch->name = $this->name;
$batch->options = $this->options;
if ($this->queue) {
$batch->onQueue($this->queue);
}
if ($this->connection) {
$batch->onConnection($this->connection);
}
foreach ($this->chainCatchCallbacks ?? [] as $callback) {
$batch->catch(function (Batch $batch, ?Throwable $exception) use ($callback) {
if (! $batch->allowsFailures()) {
$callback($exception);
}
});
}
return $batch;
}
/**
* Move the remainder of the chain to a "finally" batch callback.
*
* @param \Illuminate\Bus\PendingBatch $batch
* @return \Illuminate\Bus\PendingBatch
*/
protected function attachRemainderOfChainToEndOfBatch(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 = [];
}
return $batch;
}
}