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
3 changes: 2 additions & 1 deletion src/Assets/AssetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Statamic\Facades\Search;
use Statamic\Facades\Stache;
use Statamic\Facades\URL;
use Statamic\Statamic;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;
Expand Down Expand Up @@ -344,7 +345,7 @@ public function listContents()

public function contents()
{
return Blink::once('asset-listing-cache-'.$this->handle(), function () {
return Blink::onceIf(! Statamic::isWorker(), 'asset-listing-cache-'.$this->handle(), function () {
return app(AssetContainerContents::class)->container($this);
});
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Assets/AssetContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,42 @@ public function it_gets_the_folders_from_the_cache_and_blink_every_time_if_runni
$this->assertEquals(3, $cacheHits);
}

#[Test]
public function it_does_not_leak_stale_contents_state_across_calls_when_running_in_a_queue_worker()
{
$cacheKey = 'asset-list-contents-test';

Cache::put($cacheKey, collect([
'a.txt' => ['type' => 'file', 'path' => 'a.txt', 'dirname' => ''],
'.meta/a.txt.yaml' => ['type' => 'file', 'path' => '.meta/a.txt.yaml', 'dirname' => '.meta'],
]));

$container = (new AssetContainer)->handle('test')->disk('test');

Request::swap(new FakeArtisanRequest('queue:work'));

// First job populates the instance's $metaFiles cache. metaFilesIn() has no
// isWorker() guard, so if contents() reused the same instance across jobs
// the filtered result would stick around and bleed into the next job.
$this->assertEquals(
['.meta/a.txt.yaml'],
$container->contents()->metaFilesIn('/', true)->keys()->all()
);

// Simulate the next job seeing a different state on disk.
Cache::put($cacheKey, collect([
'a.txt' => ['type' => 'file', 'path' => 'a.txt', 'dirname' => ''],
'.meta/a.txt.yaml' => ['type' => 'file', 'path' => '.meta/a.txt.yaml', 'dirname' => '.meta'],
'b.txt' => ['type' => 'file', 'path' => 'b.txt', 'dirname' => ''],
'.meta/b.txt.yaml' => ['type' => 'file', 'path' => '.meta/b.txt.yaml', 'dirname' => '.meta'],
]));

$this->assertEquals(
['.meta/a.txt.yaml', '.meta/b.txt.yaml'],
$container->contents()->metaFilesIn('/', true)->keys()->sort()->values()->all()
);
}

#[Test]
public function it_gets_an_asset()
{
Expand Down
Loading