Skip to content

Commit

Permalink
[5.x] Pass the Guzzle config to the StaticWarmJob (#10395)
Browse files Browse the repository at this point in the history
Co-authored-by: duncanmcclean <[email protected]>
  • Loading branch information
duncanmcclean and duncanmcclean authored Jul 5, 2024
1 parent 1e91bbb commit 3c52d88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
19 changes: 12 additions & 7 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ public function handle()

private function warm(): void
{
$client = new Client([
'verify' => $this->shouldVerifySsl(),
'auth' => $this->option('user') && $this->option('password')
? [$this->option('user'), $this->option('password')]
: null,
]);
$client = new Client($this->clientConfig());

$this->output->newLine();
$this->line('Compiling URLs...');
Expand All @@ -93,7 +88,7 @@ private function warm(): void
$this->line(sprintf('Adding %s requests onto %squeue...', count($requests), $queue ? $queue.' ' : ''));

foreach ($requests as $request) {
StaticWarmJob::dispatch($request)->onQueue($queue);
StaticWarmJob::dispatch($request, $this->clientConfig())->onQueue($queue);
}
} else {
$this->line('Visiting '.count($requests).' URLs...');
Expand All @@ -117,6 +112,16 @@ private function concurrency(): int
return config("statamic.static_caching.strategies.$strategy.warm_concurrency", 25);
}

private function clientConfig(): array
{
return [
'verify' => $this->shouldVerifySsl(),
'auth' => $this->option('user') && $this->option('password')
? [$this->option('user'), $this->option('password')]
: null,
];
}

public function outputSuccessLine(Response $response, $index): void
{
$this->components->twoColumnDetail($this->getRelativeUri($index), '<info>✓ Cached</info>');
Expand Down
9 changes: 3 additions & 6 deletions src/Console/Commands/StaticWarmJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ class StaticWarmJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public Request $request;

public $tries = 1;

public function __construct(Request $request)
public function __construct(public Request $request, public array $clientConfig)
{
$this->request = $request;
}

public function handle(Client $client)
public function handle()
{
$client->send($this->request);
(new Client($this->clientConfig))->send($this->request);
}
}
7 changes: 2 additions & 5 deletions tests/Console/Commands/StaticWarmJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Console\Commands;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
Expand All @@ -22,11 +21,9 @@ public function it_sends_a_get_request()

$handlerStack = HandlerStack::create($mock);

$client = new Client(['handler' => $handlerStack]);
$job = new StaticWarmJob(new Request('GET', '/about'), ['handler' => $handlerStack]);

$job = new StaticWarmJob(new Request('GET', '/about'));

$job->handle($client);
$job->handle();

$this->assertEquals('/about', $mock->getLastRequest()->getUri()->getPath());
}
Expand Down

0 comments on commit 3c52d88

Please sign in to comment.