Browse Source

Merge branch 'dev_cache_20241214' into dev_20241205

DavidLiu 3 tháng trước cách đây
mục cha
commit
580499c19a

+ 0 - 1
src/core/callcenter/api.py

@@ -321,7 +321,6 @@ class SipURI:
 
         scheme = f"{self.SIP_SCHEME}{self.SCHEME_SEPARATOR}"
         # print('------->', sip_uri, sip_uri.startswith(scheme))
-        sys.stdout.flush()  # 强制刷新输出缓冲区
         if not sip_uri.startswith(scheme):
             raise SipUriSyntaxException(f"SIP URI must start with {scheme}")
 

+ 46 - 26
src/core/callcenter/cache.py

@@ -13,34 +13,39 @@ from src.core.callcenter.api import AgentInfo, CallInfo, RouteGateway
 from src.core.callcenter.dao import Agent, Phone
 from src.core.callcenter.data_handler import DataHandleServer
 from src.core.datasource import RedisHandler
-# from cacheout import CacheManager
+from cacheout import CacheManager
 
 
 @singleton_keys
 class Cache:
     def __init__(self, app):
         self.cacheDay = 7
-        self.deviceCall = {}
-        self.deviceUserPart = {}
+        # self.deviceCall = {}
+        # self.deviceUserPart = {}
         self.logger = app.logger
         self.redis_handler = RedisHandler()
         self.dataHandleServer = DataHandleServer(app)
-        # self.cacheman = CacheManager({'call':{'maxsize': 600, 'ttl': 60*60*1},
-        #                               'agent': {'maxsize': 600, 'ttl': 60*60*1},
-        #                               'deviceCall': {'maxsize': 600, 'ttl': 60*60*1},
-        #                               'deviceUserPart': {'maxsize': 600, 'ttl': 60*60*1},
-        #                               })
+        self.cacheman = CacheManager({'call':{'maxsize': 600, 'ttl': 60*60*1},
+                                      'agent': {'maxsize': 600, 'ttl': 60*60*1},
+                                      'deviceCall': {'maxsize': 600, 'ttl': 60*60*1},
+                                      'deviceUserPart': {'maxsize': 600, 'ttl': 60*60*1},
+                                      })
 
     def get_agent_info(self, saas_id, agent_number):
-        text = self.redis_handler.get(AGENT_INFO + saas_id + ":" + agent_number)
+        key = AGENT_INFO + saas_id + ":" + agent_number
+        agent_info = self.cacheman['agent'].get(key)
+        if agent_info:
+            return agent_info
+        text = self.redis_handler.get(key)
         self.logger.info('get_agent_info %s %s %s'%(saas_id, agent_number, text))
         if text:
-            return AgentInfo.from_json(text)
+            agent_info = AgentInfo.from_json(text)
+            self.cacheman['agent'].set(key, agent_info)
+            return agent_info
         phone = self.dataHandleServer.get_agent_phone(saas_id, agent_number)
         agent_info = AgentInfo(saas_id=saas_id, sip_server=phone.sip_server, agent_number=agent_number)
         self.logger.info('get_agent_info %s %s %s'% (saas_id, agent_number, agent_info))
         self.add_agent_info(agent=agent_info)
-        sys.stdout.flush()  # 强制刷新输出缓冲区
         return agent_info
 
 
@@ -55,7 +60,6 @@ class Cache:
     def get_agent_number(self, token):
         return self.redis_handler.get(AGENT_TOKEN + token)
 
-
     # 缓存坐席
     def add_agent_info(self, call_info: CallInfo = None, agent: AgentInfo = None, call_id=None, device_id=None):
         if call_info and not agent:
@@ -66,30 +70,41 @@ class Cache:
             agent.call_id = call_id
         if device_id:
             agent.device_id = device_id
-        self.redis_handler.set(AGENT_INFO + agent.saas_id + ":" + agent.agent_number, agent.to_json_string(), self.cacheDay * 24 * 60 * 60)
+        key = AGENT_INFO + agent.saas_id + ":" + agent.agent_number
+        self.cacheman['agent'].set(key, agent)
+        self.redis_handler.set(key, agent.to_json_string(), self.cacheDay * 24 * 60 * 60)
 
 
     # 缓存CALL_INFO
-    def add_call_info(self, call: CallInfo):
+    def add_call_info(self, call: CallInfo, persistent=False):
         for k, v in call.device_info_map.items():
             self.add_device(k, call.call_id)
         # print('add_call_info call_id:%s, call=%s'% (call.call_id, call))
-        self.redis_handler.set(CALL_INFO + call.call_id, call.to_json_string(), self.cacheDay * 24 * 60 * 60)
+        self.cacheman['call'].set(call.call_id, call)
+        if persistent:
+            self.redis_handler.set(CALL_INFO + call.call_id, call.to_json_string(), self.cacheDay * 24 * 60 * 60)
 
 
     def get_call_id_by_device_id(self, device_id):
-        call_id = self.deviceCall.get(device_id)
-        return call_id
+        return self.cacheman['deviceCall'].get(device_id)
+        # call_id = self.deviceCall.get(device_id)
+        # return call_id
 
 
     # 获取callInfo
     def get_call_info(self, call_id):
-        text = None
-        if call_id:
-            text = self.redis_handler.get(CALL_INFO + call_id)
-            # print('get_call_info call_id:%s, text:%s'% (call_id, text))
+        if not call_id:
+            return None
+        call_info = self.cacheman['call'].get(call_id)
+        if call_info:
+            return call_info
+
+        text = self.redis_handler.get(CALL_INFO + call_id)
         if text:
-            return CallInfo.from_json(text)
+            call_info = CallInfo.from_json(text)
+            self.cacheman['call'].set(call_id, call_info)
+            return call_info
+        # print('get_call_info call_id:%s, text:%s'% (call_id, text))
 
 
     def remove_call_info(self, call_id):
@@ -98,24 +113,29 @@ class Cache:
         call_info = self.get_call_info(call_id)
         if call_info and call_info.device_info_map:
             for k, v in call_info.device_info_map.items():
-                self.deviceCall.pop(k)
+                self.cacheman['deviceCall'].delete(k)
+                # self.deviceCall.pop(k)
+        self.cacheman['call'].delete(call_id)
         self.delete_key(CALL_INFO + call_id)
 
 
     def add_device(self, device_id, call_id):
         if not device_id or not call_id:
             return None
-        self.deviceCall[device_id] = call_id
+        self.cacheman['deviceCall'].set(device_id, call_id)
+        # self.deviceCall[device_id] = call_id
 
 
     def get_user_part(self, device_id):
-        return self.deviceUserPart.get(device_id)
+        return self.cacheman['deviceUserPart'].get(device_id)
+        # return self.deviceUserPart.get(device_id)
 
 
     def add_device_user_part(self, device_id, user_part):
         if not device_id or not user_part:
             return
-        self.deviceUserPart[device_id] = user_part
+        self.cacheman['deviceUserPart'].set(device_id, user_part)
+        # self.deviceUserPart[device_id] = user_part
 
 
     def get_route_gateway(self, saas_id):

+ 3 - 3
src/core/callcenter/esl/handler/channel_hangup_handler.py

@@ -99,7 +99,7 @@ class ChannelHangupHandler(EslEventHandler):
             #     call_info.answer_flag = AnswerFlag.TRANSFER_TO_AGENT.code
             #     service_id = EslEventUtil.getLIBRAServiceId(event)
             #     call_info.transfer_agent = True
-            #     self.cache.add_call_info(call_info)
+            #     self.cache.add_call_info(call_info, persistent=True)
             #     self.logger.info('ChannelHangupHandler, transferToAgent, service_id=%s' % (service_id))
             #     self.acd_service.transfer_to_agent(call_info, device_id, service_id)
             #     return
@@ -129,7 +129,7 @@ class ChannelHangupHandler(EslEventHandler):
 
             # 判断挂机方向 && 更新缓存
             self.hangup_dir(call_info, device_info, cause)
-            self.cache.add_call_info(call_info)
+            self.cache.add_call_info(call_info, persistent=True)
 
         except:
             traceback.print_exc()
@@ -192,7 +192,7 @@ class ChannelHangupHandler(EslEventHandler):
 
         # 判断挂机方向 && 更新缓存
         self.hangup_dir(call_info, device_info, cause)
-        self.cache.add_call_info(call_info)
+        self.cache.add_call_info(call_info, persistent=True)
 
     def hangup_dir(self, call_info:CallInfo, device_info:DeviceInfo, cause):
         if call_info.hangup_dir or device_info.cdr_type > CdrType.CONSULT.code:

+ 1 - 1
src/core/voip/bot.py

@@ -380,7 +380,7 @@ class MyCall(pj.Call):
             # self.audio_media.stopTransmit()
             self.aud_media = None
 
-        # self.asr.close()
+        self.asr.close()
         # 远程挂机之后要将分机号回收
         self.agent.hangup(self.user_part)
         # self.agent.release(self.user_part)