Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a75bcf8
Ensure list value is an array not a collection (#5255)
ryanmitchell Feb 10, 2022
e404135
Discard `{this}.` at top level (#5257)
arthurperton Feb 10, 2022
409fd2e
Fix toggle fieldtype alignment issues in config panels (#5251)
arthurperton Feb 11, 2022
0d3130d
changelog
jasonvarga Feb 11, 2022
5725007
Add --queue parameter (wip)
arthurperton Feb 14, 2022
a55a35f
Wrap up tests
arthurperton Feb 14, 2022
f5505e8
Fix code style
arthurperton Feb 14, 2022
b88d8e1
Support older versions for CI unit tests
arthurperton Feb 18, 2022
c13c09f
Remove unused import
arthurperton Feb 18, 2022
6bc695a
Remove another test method not supported in older Laravel versions
arthurperton Feb 18, 2022
2ede8df
Don’t use type declaration on property, because PHP 7.3?
arthurperton Feb 18, 2022
c25caec
Gray is not supported in older versions
arthurperton Feb 18, 2022
f2daee0
Revert "Support older versions for CI unit tests"
arthurperton Feb 19, 2022
5367e05
Revert "Remove unused import"
arthurperton Feb 19, 2022
3dc965e
Revert "Remove another test method not supported in older Laravel ver…
arthurperton Feb 19, 2022
30e02de
Revert "Don’t use type declaration on property, because PHP 7.3?"
arthurperton Feb 19, 2022
5726771
Revert "Gray is not supported in older versions"
arthurperton Feb 19, 2022
575fcdf
Merge branch 'master' of https://github.com/statamic/cms into pr/stat…
arthurperton Feb 19, 2022
2ae9194
Nope, still can’t use assertSuccesful() and assertFailed()
arthurperton Feb 21, 2022
5f80df1
Guzzle is still at v6
arthurperton Feb 21, 2022
c876d22
Still can’t use doesntExpectOutput()
arthurperton Feb 21, 2022
cf51d46
StyleCI
arthurperton Feb 21, 2022
18d8829
Still can’t use gray
arthurperton Feb 21, 2022
32ea4eb
undo unrelated changes
jasonvarga Feb 28, 2022
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
51 changes: 37 additions & 14 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Console\Command;
use Illuminate\Routing\Route;
use Illuminate\Support\Collection;
use Psr\Http\Client\ClientExceptionInterface;
use Statamic\Console\EnhancesCommands;
use Statamic\Console\RunsInPlease;
use Statamic\Entries\Collection as EntriesCollection;
Expand All @@ -29,9 +28,12 @@ class StaticWarm extends Command
use RunsInPlease;
use EnhancesCommands;

protected $name = 'statamic:static:warm';
protected $signature = 'statamic:static:warm {--queue : Queue the requests}';

protected $description = 'Warms the static cache by visiting all URLs';

protected $shouldQueue = false;

private $uris;

public function handle()
Expand All @@ -42,12 +44,22 @@ public function handle()
return 1;
}

$this->shouldQueue = $this->option('queue');

if ($this->shouldQueue && config('queue.default') === 'sync') {
$this->error('The queue connection is set to "sync". Queueing will be disabled.');
$this->shouldQueue = false;
}

$this->comment('Please wait. This may take a while if you have a lot of content.');

$this->warm();

$this->output->newLine();
$this->info('The static cache has been warmed.');
$this->info($this->shouldQueue
? 'All requests to warm the static cache have been added to the queue.'
: 'The static cache has been warmed.'
);

return 0;
}
Expand All @@ -59,18 +71,29 @@ private function warm(): void
$this->output->newLine();
$this->line('Compiling URLs...');

$pool = new Pool($client, $this->requests(), [
'concurrency' => $this->concurrency(),
'fulfilled' => [$this, 'outputSuccessLine'],
'rejected' => [$this, 'outputFailureLine'],
]);

$promise = $pool->promise();
$requests = $this->requests();

$this->output->newLine();
$this->line('Visiting '.$this->uris()->count().' URLs...');

$promise->wait();
if ($this->shouldQueue) {
$this->line('Queueing '.count($requests).' requests...');

foreach ($requests as $request) {
StaticWarmJob::dispatch($request);
}
} else {
$this->line('Visiting '.count($requests).' URLs...');

$pool = new Pool($client, $requests, [
'concurrency' => $this->concurrency(),
'fulfilled' => [$this, 'outputSuccessLine'],
'rejected' => [$this, 'outputFailureLine'],
]);

$promise = $pool->promise();

$promise->wait();
}
}

private function concurrency(): int
Expand All @@ -85,7 +108,7 @@ public function outputSuccessLine(Response $response, $index): void
$this->checkLine($this->getRelativeUri($index));
}

public function outputFailureLine(ClientExceptionInterface $exception, $index): void
public function outputFailureLine($exception, $index): void
{
$uri = $this->getRelativeUri($index);

Expand All @@ -101,7 +124,7 @@ public function outputFailureLine(ClientExceptionInterface $exception, $index):
$message = $exception->getMessage();
}

$this->crossLine("$uri → <fg=gray>$message</fg=gray>");
$this->crossLine("$uri → <fg=cyan>$message</fg=cyan>");
}

private function getRelativeUri(int $index): string
Expand Down
29 changes: 29 additions & 0 deletions src/Console/Commands/StaticWarmJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Statamic\Console\Commands;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;

class StaticWarmJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public Request $request;

public $tries = 1;

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

public function handle(Client $client)
{
$client->send($this->request);
}
}
32 changes: 32 additions & 0 deletions tests/Console/Commands/StaticWarmJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Console\Commands;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Statamic\Console\Commands\StaticWarmJob;
use Tests\TestCase;

class StaticWarmJobTest extends TestCase
{
/** @test */
public function it_sends_a_get_request()
{
$mock = new MockHandler([
new Response(200),
]);

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

$client = new Client(['handler' => $handlerStack]);

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

$job->handle($client);

$this->assertEquals('/about', $mock->getLastRequest()->getUri()->getPath());
}
}
96 changes: 96 additions & 0 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Tests\Console\Commands;

use Facades\Tests\Factories\EntryFactory;
use Illuminate\Support\Facades\Queue;
use Statamic\Console\Commands\StaticWarmJob;
use Statamic\Facades\Collection;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class StaticWarmTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

public function setUp(): void
{
parent::setUp();

$this->createPage('about');
$this->createPage('contact');
}

/** @test */
public function it_exits_with_error_when_static_caching_is_disabled()
{
$this->artisan('statamic:static:warm')
->expectsOutput('Static caching is not enabled.')
->assertExitCode(1);
}

/** @test */
public function it_warms_the_static_cache()
{
config(['statamic.static_caching.strategy' => 'half']);

$this->artisan('statamic:static:warm')
->expectsOutput('Visiting 2 URLs...')
->assertExitCode(0);
}

/** @test */
public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync()
{
config(['statamic.static_caching.strategy' => 'half']);

$this->artisan('statamic:static:warm', ['--queue' => true])
->expectsOutput('The queue connection is set to "sync". Queueing will be disabled.')
->assertExitCode(0);
}

/** @test */
public function it_queues_the_requests()
{
config([
'statamic.static_caching.strategy' => 'half',
'queue.default' => 'redis',
]);

Queue::fake();

$this->artisan('statamic:static:warm', ['--queue' => true])
->expectsOutput('Queueing 2 requests...')
->assertExitCode(0);

Queue::assertPushed(StaticWarmJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/about';
});
Queue::assertPushed(StaticWarmJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/contact';
});
}

private function createPage($slug, $attributes = [])
{
$this->makeCollection()->save();

return tap($this->makePage($slug, $attributes))->save();
}

private function makePage($slug, $attributes = [])
{
return EntryFactory::slug($slug)
->id($slug)
->collection('pages')
->data($attributes['with'] ?? [])
->make();
}

private function makeCollection()
{
return Collection::make('pages')
->routes('{slug}')
->template('default');
}
}