Skip to content
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
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Client/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Utils;
use Illuminate\Support\Defer\DeferredCallback;

use function Illuminate\Support\defer;

/**
* @mixin \Illuminate\Http\Client\Factory
Expand Down Expand Up @@ -207,6 +210,16 @@ public function finally(Closure $callback): self
return $this;
}

/**
* Defer the batch to run in the background after the current task has finished.
*
* @return \Illuminate\Support\Defer\DeferredCallback
*/
public function defer(): DeferredCallback
{
return defer(fn () => $this->send());
}

/**
* Send all of the requests in the batch.
*
Expand Down
28 changes: 26 additions & 2 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Defer\DeferredCallback;
use Illuminate\Support\Fluent;
use Illuminate\Support\Sleep;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -3808,7 +3809,6 @@ public function testBatchNoCallbacks(): void
});

$this->assertSame(3, $batch->totalRequests);
// $this->assertSame(0, $batch->completion());
$this->assertFalse($batch->finished());

$responses = $batch->send();
Expand All @@ -3820,11 +3820,35 @@ public function testBatchNoCallbacks(): void
$this->assertSame(3, $batch->totalRequests);
$this->assertSame(0, $batch->pendingRequests);
$this->assertSame(1, $batch->failedRequests);
// $this->assertSame(100, $batch->completion());
$this->assertTrue($batch->hasFailures());
$this->assertTrue($batch->finished());
}

public function testBatchDefer(): void
{
$this->factory->fake([
'https://200.com' => $this->factory::response('OK', 200),
'https://201.com' => $this->factory::response('Created', 201),
'https://500.com' => $this->factory::response('Error', 500),
]);

$batch = $this->factory->batch(function (Batch $batch) {
return [
$batch->as('first')->get('https://200.com'),
$batch->as('second')->get('https://201.com'),
$batch->as('third')->get('https://500.com'),
];
});

$this->assertSame(3, $batch->totalRequests);
$this->assertFalse($batch->finished());

$deferredCallback = $batch->defer();

$this->assertInstanceOf(DeferredCallback::class, $deferredCallback);
$this->assertFalse($batch->finished());
}

public function testCannotAddRequestsToInProgressBatch(): void
{
$this->expectException(BatchInProgressException::class);
Expand Down