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 8 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
12 changes: 12 additions & 0 deletions config/static_caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be worth changing this to warm_queue_connection to stay consistent with warm_queue above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I wonder if it might be worth falling back to the default connection here so we don't need to do that in the StaticWarm command:

Suggested change
'queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION'),
'warm_queue_connection' => env('STATAMIC_STATIC_WARM_QUEUE_CONNECTION', config('queue.default')),


];
14 changes: 10 additions & 4 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class StaticWarm extends Command
use RunsInPlease;

protected $signature = 'statamic:static:warm
{--queue : Queue the requests}
{--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}
Expand All @@ -38,6 +39,7 @@ class StaticWarm extends Command
protected $description = 'Warms the static cache by visiting all URLs';

protected $shouldQueue = false;
protected $queueConnection;

private $uris;

Expand All @@ -49,9 +51,11 @@ public function handle()
return 1;
}

$this->shouldQueue = $this->option('queue');
$this->shouldQueue = (bool) $this->option('queue');
$this->queueConnection = $this->option('connection') ??
(config('statamic.static_caching.queue_connection') ?? config('queue.default'));

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;
}
Expand Down Expand Up @@ -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...');
Expand Down
45 changes: 40 additions & 5 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -53,6 +49,45 @@ public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync()
->assertExitCode(0);
}

/** @test */
public function it_queues_the_requests_with_connection_option()
{
config([
'statamic.static_caching.strategy' => 'half',
'queue.default' => 'sync',
]);

Queue::fake();

$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);

Queue::assertPushed(StaticWarmJob::class, function ($job) {
return $job->connection === 'redis';
});
}

/** @test */
public function it_queues_the_requests()
{
Expand All @@ -64,7 +99,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 Down