Davidliu 1 ماه پیش
والد
کامیت
98bd5b8e6b
1فایلهای تغییر یافته به همراه36 افزوده شده و 68 حذف شده
  1. 36 68
      src/core/voip/bot.py

+ 36 - 68
src/core/voip/bot.py

@@ -591,49 +591,18 @@ class BotAgent:
         self._start()
         # threading.Thread(target=self.main_thread_daemon).start()
 
-        # self.daemon_job_scheduler = BackgroundScheduler()
-        # self.daemon_job_scheduler.add_job(self._main_thread_daemon, 'interval', seconds=1, max_instances=1, name='bot_agent_daemon')
-        # self.daemon_job_scheduler.start()
-
-    class AsyncJob(pj.PendingJob):
-        def __init__(self, agent):
-            self.agent = agent
-            self.counter = 0
-            super().__init__()
-            agent.logger.warn("Job created id: %s", id(self))
-
-        def execute(self, is_pending):
-            self.agent.logger.warn("Executing job value: %s", is_pending)
-            if self.counter >10:
-                self.agent.main_thread_daemon()
-                return
-
-            self.counter += 1
-            time.sleep(1)
-            self.agent.ep.utilAddPendingJob(self)
-
-        def __del__(self):
-            self.agent.logger.warn("Job deleted id:%s", id(self))
-
-    def _add_new_job(self):
-        self.logger.warn("Creating job 1")
-        job = self.AsyncJob(self)
-        self.logger.warn("Adding job 1")
-        self.ep.utilAddPendingJob(job)
-        self.logger.warn("Adding job 1 ...")
+        self.daemon_job_scheduler = BackgroundScheduler()
+        self.daemon_job_scheduler.add_job(self._main_thread_daemon, 'interval', seconds=1, max_instances=1, name='bot_agent_daemon')
+        self.daemon_job_scheduler.start()
 
     def _create_pjsua2(self, timeout_sec=86400):
         start_time = time.time()
         try:
-            self.logger.info('11111111111')
             self.cache.set_register_per_hours(expire=timeout_sec - (60*3))
             # Create and initialize the library
             ep_cfg = build_ep_config()
-            self.logger.info('222222222222')
             self.ep.libCreate()
-            self.logger.info('333333333333')
             self.ep.libInit(ep_cfg)
-            self.logger.info('444444444')
 
             aud_dev_mgr = self.ep.audDevManager()
             aud_dev_mgr.setNullDev()  # 使用虚拟音频设备(如果没有实际设备)
@@ -647,7 +616,6 @@ class BotAgent:
             self.ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig)
             # Start the library
             self.ep.libStart()
-            self.logger.info('55555555555')
 
             for user_part in self.user_part_range:
                 acfg = build_account_config(self.host, self.port, user_part, self.password, timeout_sec)
@@ -664,42 +632,38 @@ class BotAgent:
             registry.BOT_CREATE_ACCOUNT_LATENCY.observe(latency)
             self.logger.info("create pjsua latency: %.3fs", latency)
 
-        self._add_new_job()
-        # while not self.is_stopping:
-        #     registry.BOT_AGENT_LIVES.set(self.user_part_pool.qsize())
-        #     self.ep.libHandleEvents(200)
+        while not self.is_stopping:
+            registry.BOT_AGENT_LIVES.set(self.user_part_pool.qsize())
+            self.ep.libHandleEvents(200)
 
-        # self.logger.info('2222222222222222222222')
-        # self.call_players.clear()
-        # self.accounts.clear()
-        # self.calls.clear()
-        # # Destroy the library
-        # self.logger.info('3333333333333333333')
-        # self.ep.libDestroy()
+        self.call_players.clear()
+        self.accounts.clear()
+        self.calls.clear()
+        # Destroy the library
+        self.ep.libDestroy()
         self.logger.info("create pjsua already shutdown")
 
 
-    def main_thread_daemon(self):
+    def _main_thread_daemon(self):
         # while not self.daemon_stopping:
-        # _lock = self._play_complete_degree_check()
-        _lock = True
+        _lock = self._play_complete_degree_check()
         if _lock:
             self.logger.error("daviddebugger::play time greater than 60s, will restart")
             self.restart()
-            return True
+            return
 
         _lock = self.cache.get_pjsua_thread_lock()
         if _lock:
             self.cache.del_pjsua_thread_lock()
             self.logger.error("daviddebugger::thread is lock, will restart")
             self.restart()
-            return True
+            return
 
         _lock = self.cache.lock_register_per_hours()
         if not _lock and len(self.accounts) == len(self.user_part_range):
             self.logger.error("daviddebugger::register expire, will restart")
             self.restart()
-            return True
+            return
             # time.sleep(0.1)
 
     def _play_complete_degree_check(self):
@@ -809,7 +773,6 @@ class BotAgent:
 
     def destroy(self):
         self.is_stopping = True
-        time.sleep(1)
         self.logger.info("destroy, come in 11111")
         try:
             while not self.user_part_pool.empty():
@@ -817,24 +780,29 @@ class BotAgent:
         except:
             pass
         self.logger.info("destroy, come in 22222")
-        self.call_players.clear()
-        self.accounts.clear()
-        self.calls.clear()
-        # Destroy the library
-        self.ep.libDestroy()
+        # self.call_players.clear()
+        # self.accounts.clear()
+        # self.calls.clear()
+        # # Destroy the library
+        # self.ep.libDestroy()
         time.sleep(1)
         self.logger.info("destroy, come in 33333")
 
-        # if not self.pjsua_thread.is_alive():
-        #     self.logger.info("destroy, pre thread already stopped")
-        #     return
-        # ident = self.pjsua_thread.ident
-        # thread_id = ctypes.pythonapi.PyThreadState_SetAsyncExc
-        # res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
-        #     ctypes.c_long(ident), ctypes.py_object(SystemExit)
-        # )
-        # self.logger.info("destroy, ident=%s, thread_id=%s, res=%s", ident, thread_id, res)
-
+        if not self.pjsua_thread.is_alive():
+            self.logger.info("destroy, pre thread already stopped")
+            return
+        ident = self.pjsua_thread.ident
+        thread_id = ctypes.pythonapi.PyThreadState_SetAsyncExc
+        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
+            ctypes.c_long(ident), ctypes.py_object(SystemExit)
+        )
+        self.logger.info("destroy, ident=%s, thread_id=%s, res=%s", ident, thread_id, res)
+        # if res == 0:
+        #     raise ValueError("Invalid thread ID")
+        # elif res > 1:
+        #     # 如果多次调用,需要复位
+        #     ctypes.pythonapi.PyThreadState_SetAsyncExc(ident, 0)
+        #     raise SystemError("PyThreadState_SetAsyncExc failed")
 
 
     def __del__(self):