From 8ef9dc81a8760fe701d187afb05c94b59472eded Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Mon, 28 Aug 2023 13:01:27 +0400 Subject: [PATCH 01/12] Add ability to specify the queue connection on static:warm command --- src/Console/Commands/StaticWarm.php | 14 ++++++++++---- tests/Console/Commands/StaticWarmTest.php | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 351c558a37..f2485b3474 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -29,7 +29,7 @@ class StaticWarm extends Command use EnhancesCommands; protected $signature = 'statamic:static:warm - {--queue : Queue the requests} + {--queue= : Queue the requests} {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} @@ -38,6 +38,7 @@ class StaticWarm extends Command protected $description = 'Warms the static cache by visiting all URLs'; protected $shouldQueue = false; + protected $queueConnection; private $uris; @@ -49,9 +50,12 @@ public function handle() return 1; } - $this->shouldQueue = $this->option('queue'); + $this->shouldQueue = !!$this->option('queue'); + $this->queueConnection = is_bool($this->option('queue')) + ? config('queue.default') + : $this->option('queue'); - if ($this->shouldQueue && config('queue.default') === 'sync') { + if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->error('The queue connection is set to "sync". Queueing will be disabled.'); $this->shouldQueue = false; } @@ -90,7 +94,9 @@ 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) + ->onConnection($this->queueConnection) + ->onQueue($queue); } } else { $this->line('Visiting '.count($requests).' URLs...'); diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 4d41f9584b..eea4db7829 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -53,6 +53,25 @@ public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() ->assertExitCode(0); } + /** @test */ + public function it_queues_the_requests_with_connection() + { + config([ + 'statamic.static_caching.strategy' => 'half', + 'queue.default' => 'sync', + ]); + + Queue::fake(); + + $this->artisan('statamic:static:warm', ['--queue' => 'redis']) + ->expectsOutputToContain('Adding 2 requests') + ->assertExitCode(0); + + Queue::assertPushed(StaticWarmJob::class, function ($job) { + return $job->connection === 'redis'; + }); + } + /** @test */ public function it_queues_the_requests() { @@ -64,7 +83,7 @@ public function it_queues_the_requests() Queue::fake(); $this->artisan('statamic:static:warm', ['--queue' => true]) - ->expectsOutput('Queueing 2 requests...') + ->expectsOutputToContain('Adding 2 requests') ->assertExitCode(0); Queue::assertPushed(StaticWarmJob::class, function ($job) { From 018e89802c929e73121ca45704e3661d2dacc116 Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Mon, 28 Aug 2023 13:07:28 +0400 Subject: [PATCH 02/12] Update description --- src/Console/Commands/StaticWarm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index f2485b3474..647787686d 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -29,7 +29,7 @@ class StaticWarm extends Command use EnhancesCommands; protected $signature = 'statamic:static:warm - {--queue= : Queue the requests} + {--queue= : Queue the requests, optionally providing the connection name} {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} From 1a4af6510bf955182c1b29e8341c932c25d303f9 Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Mon, 28 Aug 2023 13:47:23 +0400 Subject: [PATCH 03/12] Fix code style --- src/Console/Commands/StaticWarm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 647787686d..ab214123a2 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -50,7 +50,7 @@ public function handle() return 1; } - $this->shouldQueue = !!$this->option('queue'); + $this->shouldQueue = (bool) $this->option('queue'); $this->queueConnection = is_bool($this->option('queue')) ? config('queue.default') : $this->option('queue'); From 51a32863fdebb10d175d9aabae89b76d8dabf00e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 28 Aug 2023 12:30:45 -0400 Subject: [PATCH 04/12] run 'em --- tests/Console/Commands/StaticWarmTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index eea4db7829..fb5ca0dded 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -17,10 +17,6 @@ public function setUp(): void { parent::setUp(); - // Temporarily skip because we get random failures. - // If you spam it_warms_the_static_cache, it'll eventually fail. - $this->markTestIncomplete(); - $this->createPage('about'); $this->createPage('contact'); } From 5c10b006beae0d372e2174c9c25ae173a42f6e58 Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Thu, 7 Sep 2023 09:46:12 +0400 Subject: [PATCH 05/12] Add config option and additional command option for connection --- config/static_caching.php | 12 ++++++++++++ src/Console/Commands/StaticWarm.php | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/config/static_caching.php b/config/static_caching.php index 0e20114a1b..2df7321fa7 100644 --- a/config/static_caching.php +++ b/config/static_caching.php @@ -125,4 +125,16 @@ 'warm_queue' => null, + /* + |-------------------------------------------------------------------------- + | Queue Connection + |-------------------------------------------------------------------------- + | + | Here you may define the queue connection name that + | will be used when warming the static cache. + | + */ + + 'queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION'), + ]; diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index ab214123a2..879c1c1917 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -29,7 +29,8 @@ class StaticWarm extends Command use EnhancesCommands; protected $signature = 'statamic:static:warm - {--queue= : Queue the requests, optionally providing the connection name} + {--queue= : Queue the requests} + {--connection= : Specify the connection name for the queue, i.e. redis } {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} @@ -51,9 +52,8 @@ public function handle() } $this->shouldQueue = (bool) $this->option('queue'); - $this->queueConnection = is_bool($this->option('queue')) - ? config('queue.default') - : $this->option('queue'); + $this->queueConnection = $this->option('connection') ?? + (config('statamic.static_caching.queue_connection') ?? config('queue.default')); if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->error('The queue connection is set to "sync". Queueing will be disabled.'); From b062c96cd4f764d7f97f2047a43934da41ccfb5f Mon Sep 17 00:00:00 2001 From: Grant Holle Date: Thu, 7 Sep 2023 09:54:45 +0400 Subject: [PATCH 06/12] Add and fix warm tests --- tests/Console/Commands/StaticWarmTest.php | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index fb5ca0dded..64f552db08 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -50,7 +50,7 @@ public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() } /** @test */ - public function it_queues_the_requests_with_connection() + public function it_queues_the_requests_with_connection_option() { config([ 'statamic.static_caching.strategy' => 'half', @@ -59,7 +59,27 @@ public function it_queues_the_requests_with_connection() Queue::fake(); - $this->artisan('statamic:static:warm', ['--queue' => 'redis']) + $this->artisan('statamic:static:warm', ['--queue' => true, '--connection' => 'redis']) + ->expectsOutputToContain('Adding 2 requests') + ->assertExitCode(0); + + Queue::assertPushed(StaticWarmJob::class, function ($job) { + return $job->connection === 'redis'; + }); + } + + /** @test */ + public function it_queues_the_requests_with_connection_config() + { + config([ + 'statamic.static_caching.strategy' => 'half', + 'statamic.static_caching.queue_connection' => 'redis', + 'queue.default' => 'sync', + ]); + + Queue::fake(); + + $this->artisan('statamic:static:warm', ['--queue' => true]) ->expectsOutputToContain('Adding 2 requests') ->assertExitCode(0); From 61c614c14006d96c7040161eb22ddf26833b7f69 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 14:00:21 -0400 Subject: [PATCH 07/12] these have failed since we switched to component output. using these helpers pass the test again. --- tests/Console/Commands/StaticWarmTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 77d2322cc1..9ec7668c7c 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -26,7 +26,7 @@ public function setUp(): void public function it_exits_with_error_when_static_caching_is_disabled() { $this->artisan('statamic:static:warm') - ->expectsOutput('Static caching is not enabled.') + ->expectsOutputToContain('Static caching is not enabled.') ->assertExitCode(1); } @@ -46,7 +46,7 @@ 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.') + ->expectsOutputToContain('The queue connection is set to "sync". Queueing will be disabled.') ->assertExitCode(0); } From 4aec8f2b77963113b2cc48ed843c316f0474a4ac Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 14:52:54 -0400 Subject: [PATCH 08/12] nitpick --- src/Console/Commands/StaticWarm.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 82284722ac..da06695af1 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -55,8 +55,7 @@ public function handle() } $this->shouldQueue = (bool) $this->option('queue'); - $this->queueConnection = $this->option('connection') ?? - (config('statamic.static_caching.queue_connection') ?? config('queue.default')); + $this->queueConnection = $this->option('connection') ?? config('statamic.static_caching.queue_connection') ?? config('queue.default'); if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->components->error('The queue connection is set to "sync". Queueing will be disabled.'); From e9732c0988d99429fa0bf0fef2ceccb36b8bcc8c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 15:04:49 -0400 Subject: [PATCH 09/12] get rid of ability to specify connection. if you add the queue flag, it'll figure out which connection based on your config. --- src/Console/Commands/StaticWarm.php | 3 +- tests/Console/Commands/StaticWarmTest.php | 48 +++++++++-------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index da06695af1..d05850eed9 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -33,7 +33,6 @@ class StaticWarm extends Command protected $signature = 'statamic:static:warm {--queue= : Queue the requests} - {--connection= : Specify the connection name for the queue, i.e. redis } {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} @@ -55,7 +54,7 @@ public function handle() } $this->shouldQueue = (bool) $this->option('queue'); - $this->queueConnection = $this->option('connection') ?? config('statamic.static_caching.queue_connection') ?? config('queue.default'); + $this->queueConnection = config('statamic.static_caching.queue_connection') ?? config('queue.default'); if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->components->error('The queue connection is set to "sync". Queueing will be disabled.'); diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 9ec7668c7c..1957e3b276 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -4,6 +4,7 @@ use Facades\Tests\Factories\EntryFactory; use Illuminate\Support\Facades\Queue; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Console\Commands\StaticWarmJob; use Statamic\Facades\Collection; @@ -51,31 +52,34 @@ public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() } #[Test] - public function it_queues_the_requests_with_connection_option() + public function it_queues_the_requests() { config([ 'statamic.static_caching.strategy' => 'half', - 'queue.default' => 'sync', + 'queue.default' => 'redis', ]); Queue::fake(); - $this->artisan('statamic:static:warm', ['--queue' => true, '--connection' => 'redis']) + $this->artisan('statamic:static:warm', ['--queue' => true]) ->expectsOutputToContain('Adding 2 requests') ->assertExitCode(0); Queue::assertPushed(StaticWarmJob::class, function ($job) { - return $job->connection === 'redis'; + return $job->request->getUri()->getPath() === '/about'; + }); + Queue::assertPushed(StaticWarmJob::class, function ($job) { + return $job->request->getUri()->getPath() === '/contact'; }); } - #[Test] - public function it_queues_the_requests_with_connection_config() + #[Test, DataProvider('queueConnectionsProvider')] + public function it_queues_the_requests_with_appropriate_connection($configuredConnection, $defaultConnection, $expectedJobConnection) { config([ 'statamic.static_caching.strategy' => 'half', - 'statamic.static_caching.queue_connection' => 'redis', - 'queue.default' => 'sync', + 'statamic.static_caching.queue_connection' => $configuredConnection, + 'queue.default' => $defaultConnection, ]); Queue::fake(); @@ -84,31 +88,15 @@ public function it_queues_the_requests_with_connection_config() ->expectsOutputToContain('Adding 2 requests') ->assertExitCode(0); - Queue::assertPushed(StaticWarmJob::class, function ($job) { - return $job->connection === 'redis'; - }); + Queue::assertPushed(StaticWarmJob::class, fn ($job) => $job->connection === $expectedJobConnection); } - #[Test] - public function it_queues_the_requests() + public static function queueConnectionsProvider() { - config([ - 'statamic.static_caching.strategy' => 'half', - 'queue.default' => 'redis', - ]); - - Queue::fake(); - - $this->artisan('statamic:static:warm', ['--queue' => true]) - ->expectsOutputToContain('Adding 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'; - }); + return [ + [null, 'redis', 'redis'], + ['sqs', 'redis', 'sqs'], + ]; } private function createPage($slug, $attributes = []) From f68da4fe0a117907996f8b3b0dc543168bed0dbf Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 15:07:19 -0400 Subject: [PATCH 10/12] prefix config with warm_ for consistency. add env var. group together. --- config/static_caching.php | 18 ++++-------------- src/Console/Commands/StaticWarm.php | 2 +- tests/Console/Commands/StaticWarmTest.php | 2 +- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/config/static_caching.php b/config/static_caching.php index c118349f9a..c471f226ce 100644 --- a/config/static_caching.php +++ b/config/static_caching.php @@ -122,24 +122,14 @@ | Warm Queue |-------------------------------------------------------------------------- | - | Here you may define the name of the queue that requests will be pushed - | onto when warming the static cache using the static:warm command. + | Here you may define the queue name and connection + | that will be used when warming the static cache. | */ - 'warm_queue' => null, + 'warm_queue' => env('STATAMIC_STATIC_WARM_QUEUE'), - /* - |-------------------------------------------------------------------------- - | Queue Connection - |-------------------------------------------------------------------------- - | - | Here you may define the queue connection name that - | will be used when warming the static cache. - | - */ - - 'queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION'), + 'warm_queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION'), /* |-------------------------------------------------------------------------- diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index d05850eed9..af26736f0c 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -54,7 +54,7 @@ public function handle() } $this->shouldQueue = (bool) $this->option('queue'); - $this->queueConnection = config('statamic.static_caching.queue_connection') ?? config('queue.default'); + $this->queueConnection = config('statamic.static_caching.warm_queue_connection') ?? config('queue.default'); if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->components->error('The queue connection is set to "sync". Queueing will be disabled.'); diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 1957e3b276..03d8b549b2 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -78,7 +78,7 @@ public function it_queues_the_requests_with_appropriate_connection($configuredCo { config([ 'statamic.static_caching.strategy' => 'half', - 'statamic.static_caching.queue_connection' => $configuredConnection, + 'statamic.static_caching.warm_queue_connection' => $configuredConnection, 'queue.default' => $defaultConnection, ]); From c3646cb374e563bf0463a60c3183123cfa755707 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 15:11:54 -0400 Subject: [PATCH 11/12] include queue in test --- tests/Console/Commands/StaticWarmTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 03d8b549b2..15d8b40a36 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -74,10 +74,16 @@ public function it_queues_the_requests() } #[Test, DataProvider('queueConnectionsProvider')] - public function it_queues_the_requests_with_appropriate_connection($configuredConnection, $defaultConnection, $expectedJobConnection) - { + public function it_queues_the_requests_with_appropriate_queue_and_connection( + $configuredQueue, + $configuredConnection, + $defaultConnection, + $expectedJobQueue, + $expectedJobConnection + ) { config([ 'statamic.static_caching.strategy' => 'half', + 'statamic.static_caching.warm_queue' => $configuredQueue, 'statamic.static_caching.warm_queue_connection' => $configuredConnection, 'queue.default' => $defaultConnection, ]); @@ -88,14 +94,16 @@ public function it_queues_the_requests_with_appropriate_connection($configuredCo ->expectsOutputToContain('Adding 2 requests') ->assertExitCode(0); - Queue::assertPushed(StaticWarmJob::class, fn ($job) => $job->connection === $expectedJobConnection); + Queue::assertPushed(StaticWarmJob::class, fn ($job) => $job->connection === $expectedJobConnection && $job->queue === $expectedJobQueue); } public static function queueConnectionsProvider() { return [ - [null, 'redis', 'redis'], - ['sqs', 'redis', 'sqs'], + [null, null, 'redis', null, 'redis'], + ['warm', null, 'redis', 'warm', 'redis'], + [null, 'sqs', 'redis', null, 'sqs'], + ['warm', 'sqs', 'redis', 'warm', 'sqs'], ]; } From 45b429f20109bfb712b08bcdbb1e542a01b3ee9d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 16 Jul 2024 15:23:08 -0400 Subject: [PATCH 12/12] wip --- src/Console/Commands/StaticWarm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index af26736f0c..7737f9d380 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -32,7 +32,7 @@ class StaticWarm extends Command use RunsInPlease; protected $signature = 'statamic:static:warm - {--queue= : Queue the requests} + {--queue : Queue the requests} {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} @@ -53,7 +53,7 @@ public function handle() return 1; } - $this->shouldQueue = (bool) $this->option('queue'); + $this->shouldQueue = $this->option('queue'); $this->queueConnection = config('statamic.static_caching.warm_queue_connection') ?? config('queue.default'); if ($this->shouldQueue && $this->queueConnection === 'sync') {