I have a chatbot app at Heroku. Works fine in local but I get session errors in Heroku. The main issue is that sessions are getting crazy. I keep getting 404 errors.
My code to manage sessions :
class SessionManager:
def __init__(self):
self.user_threads = {}
self.user_assistant_ids = {}
self.thread_to_sid = {}
self.user_ids = {}
async def create_thread_for_sid(self, sid, assistant_id, user_id):
try:
loop = asyncio.get_running_loop()
# Assuming client.beta.threads.create returns a Thread-like object
thread = await loop.run_in_executor(None, client.beta.threads.create)
# Access the 'id' attribute of the thread object (adjust according to the actual attribute name)
self.user_threads[sid] = thread.id if hasattr(thread, 'id') else None
self.user_assistant_ids[sid] = assistant_id
self.user_ids[sid] = user_id
self.thread_to_sid[thread.id if hasattr(thread, 'id') else None] = sid
print(f"Thread created for new user: {thread.id if hasattr(thread, 'id') else 'Unknown ID'} with SID: {sid}")
except Exception as e:
print(f"Error creating thread for SID {sid}: {e}")
def get_user_id(self, sid):
return self.user_ids.get(sid)
def get_thread_id(self, sid):
return self.user_threads.get(sid)
def get_sid_by_thread_id(self, thread_id):
return self.thread_to_sid.get(thread_id)
def get_assistant_id(self, sid):
return self.user_assistant_ids.get(sid)
def remove_session(self, sid):
thread_id = self.user_threads.pop(sid, None)
self.user_assistant_ids.pop(sid, None)
if thread_id:
self.thread_to_sid.pop(thread_id, None)
# print(f"Session data removed for SID: {sid}")
I keep getting 404 erros