From f42e417a068a447eeb94d12bcd2282adde904c74 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Tue, 17 Dec 2024 20:25:04 +0100 Subject: [PATCH 1/3] Recheck whether page is cached for queued requests --- src/Console/Commands/StaticWarm.php | 6 ++- .../Commands/StaticWarmUncachedJob.php | 20 +++++++ tests/Console/Commands/StaticWarmTest.php | 54 +++++++++++++++++++ .../Commands/StaticWarmUncachedJobTest.php | 53 ++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/Console/Commands/StaticWarmUncachedJob.php create mode 100644 tests/Console/Commands/StaticWarmUncachedJobTest.php diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 03e33da0b5..3510e3ad26 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -94,8 +94,12 @@ private function warm(): void $queue = config('statamic.static_caching.warm_queue'); $this->line(sprintf('Adding %s requests onto %squeue...', count($requests), $queue ? $queue.' ' : '')); + $jobClass = $this->option('uncached') + ? StaticWarmUncachedJob::class + : StaticWarmJob::class; + foreach ($requests as $request) { - StaticWarmJob::dispatch($request, $this->clientConfig()) + $jobClass::dispatch($request, $this->clientConfig()) ->onConnection($this->queueConnection) ->onQueue($queue); } diff --git a/src/Console/Commands/StaticWarmUncachedJob.php b/src/Console/Commands/StaticWarmUncachedJob.php new file mode 100644 index 0000000000..11c0c246d6 --- /dev/null +++ b/src/Console/Commands/StaticWarmUncachedJob.php @@ -0,0 +1,20 @@ +hasCachedPage(Request::create($this->request->getUri()))) { + return; + } + + parent::handle(); + } +} diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index bcb7f5fdd8..70b5ac0cc0 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Console\Commands\StaticWarmJob; +use Statamic\Console\Commands\StaticWarmUncachedJob; use Statamic\Facades\Collection; use Statamic\StaticCaching\Cacher; use Tests\PreventSavingStacheItemsToDisk; @@ -173,6 +174,59 @@ public function it_queues_the_requests() }); } + #[Test] + public function it_queues_the_request_when_the_uncached_option_is_used() + { + config([ + 'statamic.static_caching.strategy' => 'half', + 'queue.default' => 'redis', + ]); + + Queue::fake(); + + $this->artisan('statamic:static:warm', ['--queue' => true, '--uncached' => true]) + ->expectsOutputToContain('Adding 2 requests') + ->assertExitCode(0); + + Queue::assertCount(2); + + Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) { + return $job->request->getUri()->getPath() === '/about'; + }); + Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) { + return $job->request->getUri()->getPath() === '/contact'; + }); + } + + #[Test] + public function it_doesnt_queue_the_request_when_the_uncached_option_is_used_and_the_page_is_cached() + { + config([ + 'statamic.static_caching.strategy' => 'half', + 'queue.default' => 'redis', + ]); + + $mock = Mockery::mock(Cacher::class); + $mock->shouldReceive('hasCachedPage')->times(2)->andReturn(true, false); + $mock->allows('isExcluded')->andReturn(false); + app()->instance(Cacher::class, $mock); + + Queue::fake(); + + $this->artisan('statamic:static:warm', ['--queue' => true, '--uncached' => true]) + ->expectsOutputToContain('Adding 1 requests') + ->assertExitCode(0); + + Queue::assertCount(1); + + Queue::assertNotPushed(StaticWarmUncachedJob::class, function ($job) { + return $job->request->getUri()->getPath() === '/about'; + }); + Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) { + return $job->request->getUri()->getPath() === '/contact'; + }); + } + #[Test, DataProvider('queueConnectionsProvider')] public function it_queues_the_requests_with_appropriate_queue_and_connection( $configuredQueue, diff --git a/tests/Console/Commands/StaticWarmUncachedJobTest.php b/tests/Console/Commands/StaticWarmUncachedJobTest.php new file mode 100644 index 0000000000..5b5459a53b --- /dev/null +++ b/tests/Console/Commands/StaticWarmUncachedJobTest.php @@ -0,0 +1,53 @@ + $handlerStack]); + + $job->handle(); + + $this->assertEquals('/about', $mock->getLastRequest()->getUri()->getPath()); + } + + #[Test] + public function it_does_not_send_a_request_if_the_page_is_cached() + { + $mockCacher = Mockery::mock(Cacher::class); + $mockCacher->shouldReceive('hasCachedPage')->once()->andReturn(true); + $mockCacher->allows('isExcluded')->andReturn(false); + app()->instance(Cacher::class, $mockCacher); + + $mock = new MockHandler([ + new Response(200), + ]); + + $handlerStack = HandlerStack::create($mock); + + $job = new StaticWarmUncachedJob(new Request('GET', '/about'), ['handler' => $handlerStack]); + + $job->handle(); + + $this->assertNull($mock->getLastRequest()); + } +} From 5bce221f1b46cf5e8aa191025d4a5e691e1912f9 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Tue, 17 Dec 2024 21:25:45 +0100 Subject: [PATCH 2/3] Trigger checks From e2440f37249e1668e7c7f0dfd1770fde1c487c6c Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Wed, 18 Dec 2024 09:03:53 +0100 Subject: [PATCH 3/3] Trigger checks again