Skip to content

Commit 9843ab8

Browse files
committed
Always refresh metadata
1 parent 8926a58 commit 9843ab8

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/apify/storage_clients/_file_system/_key_value_store_client.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ async def _sanitize_input_json_files(self) -> None:
5555
alternative_keys = configuration.input_key_candidates - {configuration.canonical_input_key}
5656

5757
if (self.path_to_kvs / configuration.canonical_input_key).exists():
58-
# Handle missing metadata
59-
if not await self.record_exists(key=configuration.canonical_input_key):
60-
input_data = await asyncio.to_thread(
61-
lambda: json.loads((self.path_to_kvs / configuration.canonical_input_key).read_text())
62-
)
63-
await self.set_value(key=configuration.canonical_input_key, value=input_data)
58+
# Refresh metadata to prevent inconsistencies
59+
input_data = await asyncio.to_thread(
60+
lambda: json.loads((self.path_to_kvs / configuration.canonical_input_key).read_text())
61+
)
62+
await self.set_value(key=configuration.canonical_input_key, value=input_data)
6463

6564
for alternative_key in alternative_keys:
6665
if (alternative_input_file := self.path_to_kvs / alternative_key).exists():
@@ -69,11 +68,10 @@ async def _sanitize_input_json_files(self) -> None:
6968
for alternative_key in alternative_keys:
7069
alternative_input_file = self.path_to_kvs / alternative_key
7170

72-
# Handle missing metadata
73-
if alternative_input_file.exists() and not await self.record_exists(key=alternative_key):
74-
with alternative_input_file.open() as f:
75-
input_data = await asyncio.to_thread(lambda: json.load(f))
76-
await self.set_value(key=configuration.canonical_input_key, value=input_data)
71+
# Refresh metadata to prevent inconsistencies
72+
with alternative_input_file.open() as f:
73+
input_data = await asyncio.to_thread(lambda: json.load(f))
74+
await self.set_value(key=configuration.canonical_input_key, value=input_data)
7775

7876
@override
7977
async def get_value(self, *, key: str) -> KeyValueStoreRecord | None:

tests/unit/storage_clients/test_file_system.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44
import json
5-
from typing import TYPE_CHECKING
5+
from pathlib import Path
66

77
import pytest
88

@@ -11,9 +11,6 @@
1111
from apify import Actor, Configuration
1212
from apify.storage_clients._file_system import ApifyFileSystemKeyValueStoreClient
1313

14-
if TYPE_CHECKING:
15-
from pathlib import Path
16-
1714

1815
async def test_purge_preserves_input_file_and_metadata() -> None:
1916
"""Test that purge() preserves INPUT.json and metadata files but removes other files."""
@@ -67,13 +64,15 @@ async def test_purge_preserves_input_file_and_metadata() -> None:
6764

6865

6966
@pytest.mark.parametrize('input_file_name', ['INPUT', 'INPUT.json'])
70-
async def test_pre_existing_input_used_by_actor(tmp_path: Path, input_file_name: str) -> None:
67+
async def test_pre_existing_input_used_by_actor(input_file_name: str) -> None:
68+
configuration = Configuration.get_global_configuration()
69+
7170
pre_existing_input = {
7271
'foo': 'bar',
7372
}
7473

7574
# Create pre-existing INPUT.json file
76-
path_to_input = tmp_path / 'key_value_stores' / 'default'
75+
path_to_input = Path(configuration.storage_dir) / 'key_value_stores' / 'default'
7776
path_to_input.mkdir(parents=True)
7877
(path_to_input / input_file_name).write_text(json.dumps(pre_existing_input))
7978

0 commit comments

Comments
 (0)