Skip to content

Commit 4c5f48f

Browse files
committed
fix kvs storage
1 parent d2c79d3 commit 4c5f48f

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/apify/storage_clients/_apify/_utils.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
logger = getLogger(__name__)
1414

15+
_ALIAS_MAPPING_KEY = '__STORAGE_ALIASES_MAPPING'
16+
1517

1618
async def resolve_alias_to_id(
1719
alias: str,
@@ -30,22 +32,18 @@ async def resolve_alias_to_id(
3032
"""
3133
default_kvs_client = await _get_default_kvs_client(configuration)
3234

33-
# Create the key for this alias
35+
# Create the dictionary key for this alias.
3436
alias_key = f'alias-{storage_type}-{alias}'
3537

36-
# Try to get the stored ID for this alias
3738
try:
38-
record = await default_kvs_client.get_record(alias_key)
39-
if record and record.get('value'):
40-
# The record structure is: {'value': {'value': 'storage_id'}}
41-
value_data = record['value']
42-
if isinstance(value_data, dict) and 'value' in value_data:
43-
storage_id = value_data['value']
44-
if isinstance(storage_id, str):
45-
return storage_id
46-
except Exception as e:
39+
record = await default_kvs_client.get_record(_ALIAS_MAPPING_KEY)
40+
41+
if isinstance(record, dict) and alias_key in record:
42+
return str(record[alias_key])
43+
44+
except Exception as exc:
4745
# If there's any error accessing the record, treat it as not found
48-
logger.warning(f'Error accessing alias mapping for {alias}: {e}')
46+
logger.warning(f'Error accessing alias mapping for {alias}: {exc}')
4947

5048
return None
5149

@@ -66,11 +64,22 @@ async def store_alias_mapping(
6664
"""
6765
default_kvs_client = await _get_default_kvs_client(configuration)
6866

69-
# Create the key for this alias (must match the format in resolve_alias_to_id)
67+
# Create the dictionary key for this alias.
7068
alias_key = f'alias-{storage_type}-{alias}'
7169

72-
# Store the mapping
73-
await default_kvs_client.set_record(alias_key, {'value': storage_id})
70+
try:
71+
record = await default_kvs_client.get_record(_ALIAS_MAPPING_KEY)
72+
73+
# Update or create the record with the new alias mapping
74+
if isinstance(record, dict) and alias_key in record:
75+
record[alias_key] = storage_id
76+
else:
77+
record = {alias_key: storage_id}
78+
79+
# Store the mapping back in the KVS.
80+
await default_kvs_client.set_record(_ALIAS_MAPPING_KEY, record)
81+
except Exception as exc:
82+
logger.warning(f'Error accessing alias mapping for {alias}: {exc}')
7483

7584

7685
async def _get_default_kvs_client(configuration: Configuration) -> KeyValueStoreClientAsync:

0 commit comments

Comments
 (0)