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 for chains to be added to batches #34612

Merged
merged 6 commits into from Oct 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/Illuminate/Bus/Batch.php
Expand Up @@ -161,18 +161,37 @@ public function fresh()
*/
public function add($jobs)
{
$jobs = Collection::wrap($jobs)->map(function ($job) {
$jobTotal = 0;
$jobs = Collection::wrap($jobs)->map(function ($job) use (&$jobTotal) {
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}

$job->withBatchId($this->id);
if (is_array($job)) {
$jobChain = $job;
$batchId = $this->id;
array_walk($jobChain, function (&$job) use ($batchId) {
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}
$job->withBatchId($batchId);
});

$jobTotal += count($jobChain);

$chainHead = array_shift($jobChain);

return $chainHead->chain($jobChain);
} else {
$job->withBatchId($this->id);
$jobTotal++;
}

return $job;
});

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

$this->queue->connection($this->options['connection'] ?? null)->bulk(
$jobs->all(),
Expand Down
53 changes: 53 additions & 0 deletions tests/Bus/BusBatchTest.php
Expand Up @@ -8,10 +8,13 @@
use Illuminate\Bus\BatchFactory;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Bus\PendingBatch;
use Illuminate\Bus\Queueable;
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\Factory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\CallQueuedClosure;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -300,6 +303,41 @@ public function test_batch_state_can_be_inspected()
$this->assertTrue(is_string(json_encode($batch)));
}

public function test_chain_can_be_added_to_batch()
{
$queue = m::mock(Factory::class);

$batch = $this->createTestBatch($queue);

$chainHeadJob = new ChainHeadJob();

$secondJob = new SecondTestJob();

$thirdJob = new ThirdTestJob();

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

$connection->shouldReceive('bulk')->once()->with(\Mockery::on(function ($args) use ($chainHeadJob, $secondJob, $thirdJob) {
return
$args[0] == $chainHeadJob
&& serialize($secondJob) == $args[0]->chained[0]
&& serialize($thirdJob) == $args[0]->chained[1];
}), '', 'test-queue');

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

$this->assertEquals(3, $batch->totalJobs);
$this->assertEquals(3, $batch->pendingJobs);
$this->assertTrue(is_string($chainHeadJob->batchId));
$this->assertTrue(is_string($secondJob->batchId));
$this->assertTrue(is_string($thirdJob->batchId));
$this->assertInstanceOf(CarbonImmutable::class, $batch->createdAt);
}

protected function createTestBatch($queue, $allowFailures = false)
{
$repository = new DatabaseBatchRepository(new BatchFactory($queue), DB::connection(), 'job_batches');
Expand Down Expand Up @@ -345,3 +383,18 @@ protected function schema()
return $this->connection()->getSchemaBuilder();
}
}

class ChainHeadJob implements ShouldQueue
{
use Dispatchable, Queueable, Batchable;
}

class SecondTestJob implements ShouldQueue
{
use Dispatchable, Queueable, Batchable;
}

class ThirdTestJob implements ShouldQueue
{
use Dispatchable, Queueable, Batchable;
}