Skip to content

Commit

Permalink
[8.x] Allow including a closure in a queued batch (#34333)
Browse files Browse the repository at this point in the history
* allow including a closure in a queued batch

* fix style

* fix style
  • Loading branch information
themsaid authored Sep 15, 2020
1 parent e6199a4 commit 02d1424
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
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

0 comments on commit 02d1424

Please sign in to comment.