From dec2fd4009d108fe6273fd5ab6eff4eb447fce1c Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 14 Sep 2020 17:06:54 +0200 Subject: [PATCH 1/3] allow including a closure in a queued batch --- src/Illuminate/Bus/Batch.php | 12 ++++++++++-- src/Illuminate/Queue/CallQueuedClosure.php | 3 ++- tests/Bus/BusBatchTest.php | 19 +++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index c28386f17e71..a3bd1831663c 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -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; @@ -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)); diff --git a/src/Illuminate/Queue/CallQueuedClosure.php b/src/Illuminate/Queue/CallQueuedClosure.php index f0b1f0313615..5de581b24040 100644 --- a/src/Illuminate/Queue/CallQueuedClosure.php +++ b/src/Illuminate/Queue/CallQueuedClosure.php @@ -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; @@ -12,7 +13,7 @@ class CallQueuedClosure implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * The serializable Closure instance. diff --git a/tests/Bus/BusBatchTest.php b/tests/Bus/BusBatchTest.php index 5d046bfeffe6..0caaa5d6b8b5 100644 --- a/tests/Bus/BusBatchTest.php +++ b/tests/Bus/BusBatchTest.php @@ -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; @@ -89,16 +90,26 @@ 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, $thirdJob) { + 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); } From dca7e96ebc47751a23fa1ef10429fb0e758bc650 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 14 Sep 2020 17:09:59 +0200 Subject: [PATCH 2/3] fix style --- src/Illuminate/Bus/Batch.php | 2 +- tests/Bus/BusBatchTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Bus/Batch.php b/src/Illuminate/Bus/Batch.php index a3bd1831663c..687fffc25dfc 100644 --- a/src/Illuminate/Bus/Batch.php +++ b/src/Illuminate/Bus/Batch.php @@ -161,7 +161,7 @@ public function fresh() */ public function add($jobs) { - $jobs = Collection::wrap($jobs)->map(function($job){ + $jobs = Collection::wrap($jobs)->map(function ($job) { if ($job instanceof Closure) { $job = CallQueuedClosure::create($job); } diff --git a/tests/Bus/BusBatchTest.php b/tests/Bus/BusBatchTest.php index 0caaa5d6b8b5..3521942f8cc1 100644 --- a/tests/Bus/BusBatchTest.php +++ b/tests/Bus/BusBatchTest.php @@ -98,7 +98,7 @@ public function test_jobs_can_be_added_to_the_batch() ->with('test-connection') ->andReturn($connection = m::mock(stdClass::class)); - $connection->shouldReceive('bulk')->once()->with(\Mockery::on(function ($args) use ($job, $secondJob, $thirdJob) { + $connection->shouldReceive('bulk')->once()->with(\Mockery::on(function ($args) use ($job, $secondJob) { return $args[0] == $job && $args[1] == $secondJob && From 666b1919b33c55f5d06106802ea626db85fccf8c Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 14 Sep 2020 17:18:47 +0200 Subject: [PATCH 3/3] fix style --- tests/Bus/BusBatchTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Bus/BusBatchTest.php b/tests/Bus/BusBatchTest.php index 3521942f8cc1..a688170830d3 100644 --- a/tests/Bus/BusBatchTest.php +++ b/tests/Bus/BusBatchTest.php @@ -91,7 +91,6 @@ public function test_jobs_can_be_added_to_the_batch() }; $thirdJob = function () { - }; $queue->shouldReceive('connection')->once()