From 2623efe92d51913f01aed416c184f60bf410d6cd Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Wed, 18 Dec 2024 22:39:53 +0100 Subject: [PATCH] [5.x] Add `--max-requests` option to the static warm command (#11278) Co-authored-by: Jason Varga --- src/Console/Commands/StaticWarm.php | 4 +++- tests/Console/Commands/StaticWarmTest.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 03e33da0b5..909d6043ce 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -41,6 +41,7 @@ class StaticWarm extends Command {--max-depth= : Maximum depth of URLs to warm} {--include= : Only warm specific URLs} {--exclude= : Exclude specific URLs} + {--max-requests= : Maximum number of requests to warm} '; protected $description = 'Warms the static cache by visiting all URLs'; @@ -195,7 +196,8 @@ private function uris(): Collection return $cacher->isExcluded($uri); }) ->sort() - ->values(); + ->values() + ->when($this->option('max-requests'), fn ($uris, $max) => $uris->take($max)); } private function shouldInclude($uri): bool diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index bcb7f5fdd8..db9ce453e2 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -141,6 +141,16 @@ public function it_respects_max_depth() ->assertExitCode(0); } + #[Test] + public function it_limits_the_number_of_requests_when_max_requests_is_set() + { + config(['statamic.static_caching.strategy' => 'half']); + + $this->artisan('statamic:static:warm', ['--max-requests' => 1]) + ->expectsOutput('Visiting 1 URLs...') + ->assertExitCode(0); + } + #[Test] public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() {