Skip to content

Commit

Permalink
re-use StaticWarmJob rather than a new job
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Jul 22, 2024
1 parent ed4e220 commit d37c950
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 44 deletions.
39 changes: 0 additions & 39 deletions src/Jobs/StaticRecacheJob.php

This file was deleted.

18 changes: 16 additions & 2 deletions src/StaticCaching/Cachers/AbstractCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Statamic\StaticCaching\Cachers;

use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
use Statamic\Console\Commands\StaticWarmJob;
use Statamic\Facades\Site;
use Statamic\Jobs\StaticRecacheJob;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\UrlExcluder;
use Statamic\Support\Str;
Expand Down Expand Up @@ -288,7 +290,19 @@ public function recacheUrls($urls)
*/
public function recacheUrl($path, $domain = null)
{
StaticRecacheJob::dispatch($path, $domain);
$domain ??= app(Cacher::class)->getBaseUrl();

$url = $domain.$path;

$param = '__recache='.Hash::make($url);

$url .= (str_contains($url, '?') ? '&' : '?').$param;

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

StaticWarmJob::dispatch($request, [])
->onConnection(config('statamic.static_caching.warm_queue_connection') ?? config('queue.default'))
->onQueue(config('statamic.static_caching.warm_queue'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/StaticCaching/DefaultInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function form_urls_can_be_invalidated()
$this->assertNull($invalidator->invalidate($form));
}

/** @test */
#[Test]
public function it_doesnt_recache_when_background_recache_token_is_disabled()
{
$cacher = tap(Mockery::mock(Cacher::class), function ($cacher) {
Expand Down Expand Up @@ -288,7 +288,7 @@ public function it_doesnt_recache_when_background_recache_token_is_disabled()
$this->assertNull($invalidator->invalidateAndRecache($entry));
}

/** @test */
#[Test]
public function it_recaches_when_background_recache_token_is_enabled()
{
config()->set('statamic.static_caching.background_recache', true);
Expand Down
3 changes: 2 additions & 1 deletion tests/StaticCaching/FileCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\StaticCaching;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -336,7 +337,7 @@ public function recaching_a_url_will_trigger_a_recache_job()

$cacher->recacheUrl('/one', 'http://example.com');

Queue::assertPushed(\Statamic\Jobs\StaticRecacheJob::class);
Queue::assertPushed(\Statamic\Console\Commands\StaticWarmJob::class);
}

public static function invalidateEventProvider()
Expand Down

0 comments on commit d37c950

Please sign in to comment.