horse race tests
This commit is contained in:
1 parent
734d43b817
commit
bcb4e65585
1 file changed
+32
-6
+32
-6
@@ -6,6 +6,8 @@ import uuid
|
|||||||
import re
|
import re
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
|
from square.core.api_error import ApiError
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from . import get_sq_env
|
from . import get_sq_env
|
||||||
|
|
||||||
@@ -229,13 +231,16 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None):
|
|||||||
# Convert price to cents for Square API
|
# Convert price to cents for Square API
|
||||||
price_in_cents = int(set_adjustedprice * 100)
|
price_in_cents = int(set_adjustedprice * 100)
|
||||||
|
|
||||||
# ALWAYS fetch fresh item data right before update to get latest version
|
# Retry logic for version conflicts (race conditions with concurrent updates)
|
||||||
# The item_data parameter is just used to avoid lookup during enrichment check,
|
max_retries = 3
|
||||||
# but we need fresh data for the update to avoid version conflicts
|
retry_delay = 0.5 # seconds
|
||||||
print(
|
|
||||||
f"[@DEBUG] Fetching fresh item data before update to get current version..."
|
for attempt in range(max_retries):
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
|
# ALWAYS fetch fresh item data right before update to get latest version
|
||||||
|
print(
|
||||||
|
f"[@DEBUG] Fetching fresh item data (attempt {attempt + 1}/{max_retries})..."
|
||||||
|
)
|
||||||
fresh_item = await get_item_by_id(item_id)
|
fresh_item = await get_item_by_id(item_id)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
# Item was deleted between webhook and enrichment
|
# Item was deleted between webhook and enrichment
|
||||||
@@ -301,6 +306,7 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None):
|
|||||||
print(f"[@DEBUG] Upserting item {item_id} with name: {pw_name}")
|
print(f"[@DEBUG] Upserting item {item_id} with name: {pw_name}")
|
||||||
|
|
||||||
# Perform upsert using catalog.object.upsert
|
# Perform upsert using catalog.object.upsert
|
||||||
|
try:
|
||||||
result = await client.catalog.object.upsert(
|
result = await client.catalog.object.upsert(
|
||||||
idempotency_key=upsert_body["idempotency_key"],
|
idempotency_key=upsert_body["idempotency_key"],
|
||||||
object=upsert_body["object"],
|
object=upsert_body["object"],
|
||||||
@@ -318,6 +324,26 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None):
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
except ApiError as e:
|
||||||
|
# Check if this is a VERSION_MISMATCH error
|
||||||
|
if e.status_code == 400 and e.body and isinstance(e.body, dict):
|
||||||
|
errors = e.body.get("errors", [])
|
||||||
|
if any(err.get("code") == "VERSION_MISMATCH" for err in errors):
|
||||||
|
if attempt < max_retries - 1:
|
||||||
|
print(
|
||||||
|
f"[@WARNING] Version conflict on attempt {attempt + 1}, retrying in {retry_delay}s..."
|
||||||
|
)
|
||||||
|
await asyncio.sleep(retry_delay)
|
||||||
|
retry_delay *= 2 # Exponential backoff
|
||||||
|
continue # Retry with fresh data
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"[@ERROR] Version conflict persists after {max_retries} attempts, giving up"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
# Re-raise other API errors
|
||||||
|
raise
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[@ERROR] Failed to update item {item_id}: {e}")
|
print(f"[@ERROR] Failed to update item {item_id}: {e}")
|
||||||
import traceback
|
import traceback
|
||||||
|
|||||||
Reference in new issue
Block a user