diff --git a/config/static_caching.php b/config/static_caching.php index e4a2fdd13b..c471f226ce 100644 --- a/config/static_caching.php +++ b/config/static_caching.php @@ -122,12 +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'), + + 'warm_queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION'), /* |-------------------------------------------------------------------------- diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 93ff56dc60..7737f9d380 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -41,6 +41,7 @@ class StaticWarm extends Command protected $description = 'Warms the static cache by visiting all URLs'; protected $shouldQueue = false; + protected $queueConnection; private $uris; @@ -53,8 +54,9 @@ public function handle() } $this->shouldQueue = $this->option('queue'); + $this->queueConnection = config('statamic.static_caching.warm_queue_connection') ?? config('queue.default'); - if ($this->shouldQueue && config('queue.default') === 'sync') { + if ($this->shouldQueue && $this->queueConnection === 'sync') { $this->components->error('The queue connection is set to "sync". Queueing will be disabled.'); $this->shouldQueue = false; } @@ -88,7 +90,9 @@ private function warm(): void $this->line(sprintf('Adding %s requests onto %squeue...', count($requests), $queue ? $queue.' ' : '')); foreach ($requests as $request) { - StaticWarmJob::dispatch($request, $this->clientConfig())->onQueue($queue); + StaticWarmJob::dispatch($request, $this->clientConfig()) + ->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 74ee487f88..15d8b40a36 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; @@ -18,10 +19,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'); } @@ -30,7 +27,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); } @@ -50,7 +47,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); } @@ -65,7 +62,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) { @@ -76,6 +73,40 @@ public function it_queues_the_requests() }); } + #[Test, DataProvider('queueConnectionsProvider')] + 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, + ]); + + Queue::fake(); + + $this->artisan('statamic:static:warm', ['--queue' => true]) + ->expectsOutputToContain('Adding 2 requests') + ->assertExitCode(0); + + Queue::assertPushed(StaticWarmJob::class, fn ($job) => $job->connection === $expectedJobConnection && $job->queue === $expectedJobQueue); + } + + public static function queueConnectionsProvider() + { + return [ + [null, null, 'redis', null, 'redis'], + ['warm', null, 'redis', 'warm', 'redis'], + [null, 'sqs', 'redis', null, 'sqs'], + ['warm', 'sqs', 'redis', 'warm', 'sqs'], + ]; + } + private function createPage($slug, $attributes = []) { $this->makeCollection()->save();