Skip to content

Commit

Permalink
[10.x] Add before to the PendingBatch (#50058)
Browse files Browse the repository at this point in the history
* Add `before` to the `PendingBatch`.

* style: fixes

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
xiCO2k and taylorotwell authored Feb 13, 2024
1 parent b7ca51d commit 84e7f85
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
52 changes: 50 additions & 2 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ public function add($jobs)
return $this;
}

/**
* Add a callback to be executed when the batch is stored.
*
* @param callable $callback
* @return $this
*/
public function before($callback)
{
$this->options['before'][] = $callback instanceof Closure
? new SerializableClosure($callback)
: $callback;

return $this;
}

/**
* Get the "before" callbacks that have been registered with the pending batch.
*
* @return array
*/
public function beforeCallbacks()
{
return $this->options['before'] ?? [];
}

/**
* Add a callback to be executed after a job in the batch have executed successfully.
*
Expand Down Expand Up @@ -282,7 +307,7 @@ public function dispatch()
$repository = $this->container->make(BatchRepository::class);

try {
$batch = $repository->store($this);
$batch = $this->store($repository);

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

$batch = $repository->store($this);
$batch = $this->store($repository);

if ($batch) {
$this->container->terminating(function () use ($batch) {
Expand Down Expand Up @@ -366,4 +391,27 @@ public function dispatchUnless($boolean)
{
return ! value($boolean) ? $this->dispatch() : null;
}

/**
* Store the batch using the given repository.
*
* @param \Illuminate\Bus\BatchRepository $repository
* @return \Illuminate\Bus\Batch
*/
protected function store($repository)
{
$batch = $repository->store($this);

collect($this->beforeCallbacks())->each(function ($handler) use ($batch) {
try {
return $handler($batch);
} catch (Throwable $e) {
if (function_exists('report')) {
report($e);
}
}
});

return $batch;
}
}
38 changes: 37 additions & 1 deletion tests/Bus/BusPendingBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function test_pending_batch_may_be_configured_and_dispatched()

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

$pendingBatch = $pendingBatch->progress(function () {
$pendingBatch = $pendingBatch->before(function () {
//
})->progress(function () {
//
})->then(function () {
//
Expand All @@ -47,6 +49,7 @@ public function test_pending_batch_may_be_configured_and_dispatched()

$this->assertSame('test-connection', $pendingBatch->connection());
$this->assertSame('test-queue', $pendingBatch->queue());
$this->assertCount(1, $pendingBatch->beforeCallbacks());
$this->assertCount(1, $pendingBatch->progressCallbacks());
$this->assertCount(1, $pendingBatch->thenCallbacks());
$this->assertCount(1, $pendingBatch->catchCallbacks());
Expand Down Expand Up @@ -186,4 +189,37 @@ public function test_batch_is_not_dispatched_when_dispatchunless_is_true()

$this->assertNull($result);
}

public function test_batch_before_event_is_called()
{
$container = new Container;

$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldReceive('dispatch')->once();

$container->instance(Dispatcher::class, $eventDispatcher);

$job = new class
{
use Batchable;
};

$beforeCalled = false;

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

$pendingBatch = $pendingBatch->before(function () use (&$beforeCalled) {
$beforeCalled = true;
})->onConnection('test-connection')->onQueue('test-queue');

$repository = m::mock(BatchRepository::class);
$repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
$batch->shouldReceive('add')->once()->with(m::type(Collection::class))->andReturn($batch = m::mock(Batch::class));

$container->instance(BatchRepository::class, $repository);

$pendingBatch->dispatch();

$this->assertTrue($beforeCalled);
}
}

0 comments on commit 84e7f85

Please sign in to comment.