-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreset_profiles_index_timeout.py
88 lines (73 loc) · 2.77 KB
/
reset_profiles_index_timeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from pinecone import Pinecone
import os
from dotenv import load_dotenv
import time
import logging
from concurrent.futures import ThreadPoolExecutor, TimeoutError
import threading
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def create_index_with_timeout(pc, max_wait=60):
"""Create index with timeout"""
try:
logger.info("Creating new 'profiles' index...")
pc.create_index(
name="profiles",
dimension=384,
metric="cosine",
spec={
"pod": {
"environment": "gcp-starter"
}
}
)
return True
except Exception as e:
logger.error(f"Error creating index: {str(e)}")
return False
def reset_profiles_index():
try:
# Load environment variables
load_dotenv()
# Initialize Pinecone
logger.info("Connecting to Pinecone...")
pc = Pinecone()
# List current indexes
current_indexes = pc.list_indexes().names()
logger.info(f"Current indexes: {current_indexes}")
# Delete if exists
if "profiles" in current_indexes:
logger.info("Deleting existing 'profiles' index...")
pc.delete_index("profiles")
# Wait for deletion with timeout
start_time = time.time()
max_wait = 30 # Maximum seconds to wait
while "profiles" in pc.list_indexes().names():
if time.time() - start_time > max_wait:
raise TimeoutError("Index deletion timed out")
logger.info("Waiting for deletion to complete...")
time.sleep(5)
logger.info("Index deleted successfully")
# Create new index with timeout
with ThreadPoolExecutor() as executor:
future = executor.submit(create_index_with_timeout, pc)
try:
result = future.result(timeout=60) # Wait max 60 seconds
if not result:
raise Exception("Failed to create index")
except TimeoutError:
raise TimeoutError("Index creation timed out after 60 seconds")
logger.info("Index created successfully!")
# Quick verification
if "profiles" not in pc.list_indexes().names():
raise Exception("Index creation verified failed")
logger.info("All operations completed successfully!")
except TimeoutError as e:
logger.error(f"Timeout error: {str(e)}")
raise
except Exception as e:
logger.error(f"Error: {str(e)}")
raise
if __name__ == "__main__":
reset_profiles_index()