Skip to content

Commit 02d1424

Browse files
authored
[8.x] Allow including a closure in a queued batch (#34333)
* allow including a closure in a queued batch * fix style * fix style
1 parent e6199a4 commit 02d1424

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Illuminate/Bus/Batch.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Illuminate\Bus;
44

55
use Carbon\CarbonImmutable;
6+
use Closure;
67
use Illuminate\Contracts\Queue\Factory as QueueFactory;
78
use Illuminate\Contracts\Support\Arrayable;
9+
use Illuminate\Queue\CallQueuedClosure;
810
use Illuminate\Queue\SerializableClosure;
911
use Illuminate\Support\Arr;
1012
use Illuminate\Support\Collection;
@@ -159,9 +161,15 @@ public function fresh()
159161
*/
160162
public function add($jobs)
161163
{
162-
$jobs = Collection::wrap($jobs);
164+
$jobs = Collection::wrap($jobs)->map(function ($job) {
165+
if ($job instanceof Closure) {
166+
$job = CallQueuedClosure::create($job);
167+
}
163168

164-
$jobs->each->withBatchId($this->id);
169+
$job->withBatchId($this->id);
170+
171+
return $job;
172+
});
165173

166174
$this->repository->transaction(function () use ($jobs) {
167175
$this->repository->incrementTotalJobs($this->id, count($jobs));

src/Illuminate/Queue/CallQueuedClosure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Exception;
7+
use Illuminate\Bus\Batchable;
78
use Illuminate\Bus\Queueable;
89
use Illuminate\Contracts\Container\Container;
910
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -12,7 +13,7 @@
1213

1314
class CallQueuedClosure implements ShouldQueue
1415
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1617

1718
/**
1819
* The serializable Closure instance.

tests/Bus/BusBatchTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Contracts\Queue\Factory;
1313
use Illuminate\Database\Capsule\Manager as DB;
1414
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Queue\CallQueuedClosure;
1516
use Mockery as m;
1617
use PHPUnit\Framework\TestCase;
1718
use RuntimeException;
@@ -89,16 +90,25 @@ public function test_jobs_can_be_added_to_the_batch()
8990
use Batchable;
9091
};
9192

93+
$thirdJob = function () {
94+
};
95+
9296
$queue->shouldReceive('connection')->once()
9397
->with('test-connection')
9498
->andReturn($connection = m::mock(stdClass::class));
9599

96-
$connection->shouldReceive('bulk')->once()->with([$job, $secondJob], '', 'test-queue');
100+
$connection->shouldReceive('bulk')->once()->with(\Mockery::on(function ($args) use ($job, $secondJob) {
101+
return
102+
$args[0] == $job &&
103+
$args[1] == $secondJob &&
104+
$args[2] instanceof CallQueuedClosure
105+
&& is_string($args[2]->batchId);
106+
}), '', 'test-queue');
97107

98-
$batch = $batch->add([$job, $secondJob]);
108+
$batch = $batch->add([$job, $secondJob, $thirdJob]);
99109

100-
$this->assertEquals(2, $batch->totalJobs);
101-
$this->assertEquals(2, $batch->pendingJobs);
110+
$this->assertEquals(3, $batch->totalJobs);
111+
$this->assertEquals(3, $batch->pendingJobs);
102112
$this->assertTrue(is_string($job->batchId));
103113
$this->assertInstanceOf(CarbonImmutable::class, $batch->createdAt);
104114
}

0 commit comments

Comments
 (0)