Skip to content
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
6 changes: 6 additions & 0 deletions config/static_caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@
| that will be used when warming the static cache and
| optionally set the "--insecure" flag by default.
|
| You may also define the job class used to warm each URL. It should
| extend Statamic\Console\Commands\StaticWarmJob, for example to
| change its uniqueness or add a delay.
|
*/

'warm_queue' => env('STATAMIC_STATIC_WARM_QUEUE'),
Expand All @@ -176,6 +180,8 @@

'warm_insecure' => env('STATAMIC_STATIC_WARM_INSECURE', false),

'warm_job' => null,

/*
|--------------------------------------------------------------------------
| Background Re-cache
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function warm(): void

$jobClass = $this->option('uncached')
? StaticWarmUncachedJob::class
: StaticWarmJob::class;
: (config('statamic.static_caching.warm_job') ?? StaticWarmJob::class);

foreach ($requests as $request) {
$jobClass::dispatch($request, $this->clientConfig())
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/StaticWarmJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handle()
"{$pageName}={$page}",
]);
})
->each(fn (string $uri) => StaticWarmJob::dispatch(
->each(fn (string $uri) => static::dispatch(
new Request('GET', $uri, $this->request->getHeaders()),
$this->clientConfig
));
Expand Down
4 changes: 3 additions & 1 deletion src/StaticCaching/Cachers/AbstractCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ public function refreshUrl($url, $domain = null)

$request = new GuzzleRequest('GET', $url);

StaticWarmJob::dispatch($request, [])
$job = config('statamic.static_caching.warm_job') ?? StaticWarmJob::class;

$job::dispatch($request, [])
->onConnection(config('statamic.static_caching.warm_queue_connection') ?? config('queue.default'))
->onQueue(config('statamic.static_caching.warm_queue'));
});
Expand Down
24 changes: 24 additions & 0 deletions tests/Console/Commands/StaticWarmJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ public function it_sends_a_get_request()
$this->assertEquals('/about', $mock->getLastRequest()->getUri()->getPath());
}

#[Test]
public function it_dispatches_paginated_pages_with_the_same_job_class()
{
Queue::fake();

$mock = new MockHandler([
(new Response(200))->withHeader('X-Statamic-Pagination', [
'current' => 1,
'total' => 2,
'name' => 'page',
]),
]);

$job = new CustomPaginatedStaticWarmJob(new Request('GET', 'http://localhost/blog'), ['handler' => HandlerStack::create($mock)]);

$job->handle();

Queue::assertPushed(CustomPaginatedStaticWarmJob::class, 2);
}

#[Test]
public function it_sends_a_get_request_and_dispatches_static_warm_job_for_page_with_pagination()
{
Expand Down Expand Up @@ -173,3 +193,7 @@ public function parse($response)
$this->assertSame([1, 3, 'pa,ge'], $parser->parse($folded));
}
}

class CustomPaginatedStaticWarmJob extends StaticWarmJob
{
}
46 changes: 46 additions & 0 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ public function it_queues_the_requests()
});
}

#[Test]
public function it_queues_the_requests_using_the_configured_warm_job()
{
config([
'statamic.static_caching.strategy' => 'half',
'statamic.static_caching.warm_job' => CustomStaticWarmJob::class,
'queue.default' => 'redis',
]);

Queue::fake();

$this->artisan('statamic:static:warm', ['--queue' => true])
->expectsOutputToContain('Adding 2 requests')
->assertExitCode(0);

Queue::assertCount(2);
Queue::assertPushed(CustomStaticWarmJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/about';
});
Queue::assertPushed(CustomStaticWarmJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/contact';
});
}

#[Test]
public function it_queues_the_uncached_job_when_the_uncached_option_is_used_with_a_configured_warm_job()
{
config([
'statamic.static_caching.strategy' => 'half',
'statamic.static_caching.warm_job' => CustomStaticWarmJob::class,
'queue.default' => 'redis',
]);

Queue::fake();

$this->artisan('statamic:static:warm', ['--queue' => true, '--uncached' => true])
->assertExitCode(0);

Queue::assertPushed(StaticWarmUncachedJob::class, 2);
Queue::assertNotPushed(CustomStaticWarmJob::class);
}

#[Test]
public function it_queues_the_request_when_the_uncached_option_is_used()
{
Expand Down Expand Up @@ -362,3 +404,7 @@ private function makeCollection()
->template('default');
}
}

class CustomStaticWarmJob extends StaticWarmJob
{
}
24 changes: 24 additions & 0 deletions tests/StaticCaching/ApplicationCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ public static function cachedResponseProvider()
];
}

#[Test]
public function refreshing_urls_dispatches_the_configured_warm_job()
{
Queue::fake();

config(['statamic.static_caching.warm_job' => CustomStaticWarmJob::class]);

$cache = app(Repository::class);
$cacher = new ApplicationCacher($cache, ['base_url' => 'http://example.com']);

$request = Request::create('http://example.com/one', 'GET');
$cacher->cachePage($request, response('one', 200));

$cacher->refreshUrls(['/one']);

Queue::assertPushed(CustomStaticWarmJob::class, function ($job) {
return str_contains((string) $job->request->getUri(), '/one');
});
}

#[Test]
public function refreshing_a_wildcard_warms_successful_urls_and_invalidates_error_urls()
{
Expand Down Expand Up @@ -398,3 +418,7 @@ public static function currentUrlProvider()
];
}
}

class CustomStaticWarmJob extends StaticWarmJob
{
}
Loading