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

[8.x] Allow including a closure in a queued batch #34333

Merged
merged 3 commits into from
Sep 15, 2020
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
12 changes: 10 additions & 2 deletions src/Illuminate/Bus/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Illuminate\Bus;

use Carbon\CarbonImmutable;
use Closure;
use Illuminate\Contracts\Queue\Factory as QueueFactory;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Queue\SerializableClosure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -159,9 +161,15 @@ public function fresh()
*/
public function add($jobs)
{
$jobs = Collection::wrap($jobs);
$jobs = Collection::wrap($jobs)->map(function ($job) {
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}

$jobs->each->withBatchId($this->id);
$job->withBatchId($this->id);

return $job;
});

$this->repository->transaction(function () use ($jobs) {
$this->repository->incrementTotalJobs($this->id, count($jobs));
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Queue/CallQueuedClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Exception;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -12,7 +13,7 @@

class CallQueuedClosure implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The serializable Closure instance.
Expand Down
18 changes: 14 additions & 4 deletions tests/Bus/BusBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Contracts\Queue\Factory;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\CallQueuedClosure;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand Down Expand Up @@ -89,16 +90,25 @@ public function test_jobs_can_be_added_to_the_batch()
use Batchable;
};

$thirdJob = function () {
};

$queue->shouldReceive('connection')->once()
->with('test-connection')
->andReturn($connection = m::mock(stdClass::class));

$connection->shouldReceive('bulk')->once()->with([$job, $secondJob], '', 'test-queue');
$connection->shouldReceive('bulk')->once()->with(\Mockery::on(function ($args) use ($job, $secondJob) {
return
$args[0] == $job &&
$args[1] == $secondJob &&
$args[2] instanceof CallQueuedClosure
&& is_string($args[2]->batchId);
}), '', 'test-queue');

$batch = $batch->add([$job, $secondJob]);
$batch = $batch->add([$job, $secondJob, $thirdJob]);

$this->assertEquals(2, $batch->totalJobs);
$this->assertEquals(2, $batch->pendingJobs);
$this->assertEquals(3, $batch->totalJobs);
$this->assertEquals(3, $batch->pendingJobs);
$this->assertTrue(is_string($job->batchId));
$this->assertInstanceOf(CarbonImmutable::class, $batch->createdAt);
}
Expand Down