Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add before to the PendingBatch #50058

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading