API timeout için retry mekanizması - 3 deneme (10s, 15s, 20s)
Browse files- app.py +14 -4
- smart_warehouse.py +18 -9
app.py
CHANGED
@@ -53,10 +53,20 @@ def get_warehouse_stock_old(product_name):
|
|
53 |
import re
|
54 |
import requests
|
55 |
|
56 |
-
# Get XML
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Turkish normalize
|
62 |
def normalize(text):
|
|
|
53 |
import re
|
54 |
import requests
|
55 |
|
56 |
+
# Get XML with retry
|
57 |
+
xml_text = None
|
58 |
+
for attempt in range(3):
|
59 |
+
try:
|
60 |
+
url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
|
61 |
+
timeout_val = 10 + (attempt * 5) # 10, 15, 20 seconds
|
62 |
+
response = requests.get(url, verify=False, timeout=timeout_val)
|
63 |
+
xml_text = response.text
|
64 |
+
break
|
65 |
+
except requests.exceptions.Timeout:
|
66 |
+
if attempt == 2:
|
67 |
+
return None
|
68 |
+
except Exception:
|
69 |
+
return None
|
70 |
|
71 |
# Turkish normalize
|
72 |
def normalize(text):
|
smart_warehouse.py
CHANGED
@@ -10,15 +10,24 @@ def get_warehouse_stock_smart(user_message):
|
|
10 |
|
11 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
12 |
|
13 |
-
# Get XML data
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Extract just product blocks to reduce token usage
|
24 |
product_pattern = r'<Product>(.*?)</Product>'
|
|
|
10 |
|
11 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
12 |
|
13 |
+
# Get XML data with retry
|
14 |
+
xml_text = None
|
15 |
+
for attempt in range(3): # Try 3 times
|
16 |
+
try:
|
17 |
+
url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
|
18 |
+
timeout_val = 10 + (attempt * 5) # Increase timeout on each retry: 10, 15, 20
|
19 |
+
response = requests.get(url, verify=False, timeout=timeout_val)
|
20 |
+
xml_text = response.text
|
21 |
+
print(f"DEBUG - XML fetched: {len(xml_text)} characters (attempt {attempt+1})")
|
22 |
+
break
|
23 |
+
except requests.exceptions.Timeout:
|
24 |
+
print(f"XML fetch timeout (attempt {attempt+1}/3, timeout={timeout_val}s)")
|
25 |
+
if attempt == 2:
|
26 |
+
print("All attempts failed - timeout")
|
27 |
+
return None
|
28 |
+
except Exception as e:
|
29 |
+
print(f"XML fetch error: {e}")
|
30 |
+
return None
|
31 |
|
32 |
# Extract just product blocks to reduce token usage
|
33 |
product_pattern = r'<Product>(.*?)</Product>'
|