Skip to content

[5.x] Add ability to specify the queue connection on static:warm command #8634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions config/static_caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

/*
|--------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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...');
Expand Down
45 changes: 38 additions & 7 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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();
Expand Down
Loading