Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add cancel to batch implementation
  • Loading branch information
WendellAdriel committed Sep 14, 2025
commit 4dcdfdf217895dac24eda5241e417a6e0d0b93c9
35 changes: 34 additions & 1 deletion src/Illuminate/Http/Client/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Batch
*/
public $createdAt;

/**
* The date indicating when the batch was cancelled.
*
* @var \Carbon\CarbonImmutable|null
*/
public $cancelledAt;

/**
* The date indicating when the batch was finished.
*
Expand Down Expand Up @@ -182,6 +189,26 @@ public function hasFailures(): bool
return $this->failedRequests > 0;
}

/**
* Cancel the batch.
*
* @return void
*/
public function cancel(): void
{
$this->cancelledAt = new CarbonImmutable();
}

/**
* Determine if the batch was cancelled.
*
* @return bool
*/
public function cancelled(): bool
{
return ! is_null($this->cancelledAt);
}

/**
* Determine if the batch has finished executing.
*
Expand Down Expand Up @@ -326,6 +353,10 @@ public function send(): array
}

foreach ($requests as $key => $item) {
if ($this->cancelled()) {
break;
}

$result = $item->getPromise()->wait();
$results[$key] = $result;
$this->decrementPendingRequests();
Expand All @@ -352,7 +383,9 @@ public function send(): array
$finallyCallback($this, $results);
}

$this->finishedAt = new CarbonImmutable();
if (! $this->cancelled()) {
$this->finishedAt = new CarbonImmutable();
}

return $results;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3824,6 +3824,44 @@ public function testBatchNoCallbacks(): void
$this->assertTrue($batch->finished());
}

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

$batch = $this->factory->batch(function (Batch $batch) {
return [
$batch->as('first')->get('https://200.com'),
$batch->as('second')->get('https://500.com'),
$batch->as('third')->get('https://201.com'),
];
})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) {
$batch->cancel();
});

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

$responses = $batch->send();

$this->assertCount(2, $responses);
$this->assertSame(200, $responses['first']->status());
$this->assertSame(500, $responses['second']->status());

$this->assertSame(3, $batch->totalRequests);
$this->assertSame(1, $batch->pendingRequests);
$this->assertSame(1, $batch->failedRequests);
$this->assertSame(67, $batch->completion());
$this->assertTrue($batch->hasFailures());
$this->assertFalse($batch->finished());
$this->assertTrue($batch->cancelled());
}

public function testBatchBeforeHook(): void
{
$this->factory->fake([
Expand Down
Loading