Skip to content

Commit 84e7f85

Browse files
xiCO2ktaylorotwell
andauthored
[10.x] Add before to the PendingBatch (#50058)
* Add `before` to the `PendingBatch`. * style: fixes * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b7ca51d commit 84e7f85

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/Illuminate/Bus/PendingBatch.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ public function add($jobs)
7474
return $this;
7575
}
7676

77+
/**
78+
* Add a callback to be executed when the batch is stored.
79+
*
80+
* @param callable $callback
81+
* @return $this
82+
*/
83+
public function before($callback)
84+
{
85+
$this->options['before'][] = $callback instanceof Closure
86+
? new SerializableClosure($callback)
87+
: $callback;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Get the "before" callbacks that have been registered with the pending batch.
94+
*
95+
* @return array
96+
*/
97+
public function beforeCallbacks()
98+
{
99+
return $this->options['before'] ?? [];
100+
}
101+
77102
/**
78103
* Add a callback to be executed after a job in the batch have executed successfully.
79104
*
@@ -282,7 +307,7 @@ public function dispatch()
282307
$repository = $this->container->make(BatchRepository::class);
283308

284309
try {
285-
$batch = $repository->store($this);
310+
$batch = $this->store($repository);
286311

287312
$batch = $batch->add($this->jobs);
288313
} catch (Throwable $e) {
@@ -309,7 +334,7 @@ public function dispatchAfterResponse()
309334
{
310335
$repository = $this->container->make(BatchRepository::class);
311336

312-
$batch = $repository->store($this);
337+
$batch = $this->store($repository);
313338

314339
if ($batch) {
315340
$this->container->terminating(function () use ($batch) {
@@ -366,4 +391,27 @@ public function dispatchUnless($boolean)
366391
{
367392
return ! value($boolean) ? $this->dispatch() : null;
368393
}
394+
395+
/**
396+
* Store the batch using the given repository.
397+
*
398+
* @param \Illuminate\Bus\BatchRepository $repository
399+
* @return \Illuminate\Bus\Batch
400+
*/
401+
protected function store($repository)
402+
{
403+
$batch = $repository->store($this);
404+
405+
collect($this->beforeCallbacks())->each(function ($handler) use ($batch) {
406+
try {
407+
return $handler($batch);
408+
} catch (Throwable $e) {
409+
if (function_exists('report')) {
410+
report($e);
411+
}
412+
}
413+
});
414+
415+
return $batch;
416+
}
369417
}

tests/Bus/BusPendingBatchTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function test_pending_batch_may_be_configured_and_dispatched()
3737

3838
$pendingBatch = new PendingBatch($container, new Collection([$job]));
3939

40-
$pendingBatch = $pendingBatch->progress(function () {
40+
$pendingBatch = $pendingBatch->before(function () {
41+
//
42+
})->progress(function () {
4143
//
4244
})->then(function () {
4345
//
@@ -47,6 +49,7 @@ public function test_pending_batch_may_be_configured_and_dispatched()
4749

4850
$this->assertSame('test-connection', $pendingBatch->connection());
4951
$this->assertSame('test-queue', $pendingBatch->queue());
52+
$this->assertCount(1, $pendingBatch->beforeCallbacks());
5053
$this->assertCount(1, $pendingBatch->progressCallbacks());
5154
$this->assertCount(1, $pendingBatch->thenCallbacks());
5255
$this->assertCount(1, $pendingBatch->catchCallbacks());
@@ -186,4 +189,37 @@ public function test_batch_is_not_dispatched_when_dispatchunless_is_true()
186189

187190
$this->assertNull($result);
188191
}
192+
193+
public function test_batch_before_event_is_called()
194+
{
195+
$container = new Container;
196+
197+
$eventDispatcher = m::mock(Dispatcher::class);
198+
$eventDispatcher->shouldReceive('dispatch')->once();
199+
200+
$container->instance(Dispatcher::class, $eventDispatcher);
201+
202+
$job = new class
203+
{
204+
use Batchable;
205+
};
206+
207+
$beforeCalled = false;
208+
209+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
210+
211+
$pendingBatch = $pendingBatch->before(function () use (&$beforeCalled) {
212+
$beforeCalled = true;
213+
})->onConnection('test-connection')->onQueue('test-queue');
214+
215+
$repository = m::mock(BatchRepository::class);
216+
$repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
217+
$batch->shouldReceive('add')->once()->with(m::type(Collection::class))->andReturn($batch = m::mock(Batch::class));
218+
219+
$container->instance(BatchRepository::class, $repository);
220+
221+
$pendingBatch->dispatch();
222+
223+
$this->assertTrue($beforeCalled);
224+
}
189225
}

0 commit comments

Comments
 (0)