停止 AstrBot 核心生命周期管理类, 取消所有当前任务并终止各个管理器.
(self)
| 357 | await asyncio.gather(*self.curr_tasks, return_exceptions=True) |
| 358 | |
| 359 | async def stop(self) -> None: |
| 360 | """停止 AstrBot 核心生命周期管理类, 取消所有当前任务并终止各个管理器.""" |
| 361 | if self.temp_dir_cleaner: |
| 362 | await self.temp_dir_cleaner.stop() |
| 363 | |
| 364 | # 请求停止所有正在运行的异步任务 |
| 365 | for task in self.curr_tasks: |
| 366 | task.cancel() |
| 367 | |
| 368 | if self.cron_manager: |
| 369 | await self.cron_manager.shutdown() |
| 370 | |
| 371 | for plugin in self.plugin_manager.context.get_all_stars(): |
| 372 | try: |
| 373 | await self.plugin_manager._terminate_plugin(plugin) |
| 374 | except Exception as e: |
| 375 | logger.warning(traceback.format_exc()) |
| 376 | logger.warning( |
| 377 | f"插件 {plugin.name} 未被正常终止 {e!s}, 可能会导致资源泄露等问题。", |
| 378 | ) |
| 379 | |
| 380 | await self.provider_manager.terminate() |
| 381 | await self.platform_manager.terminate() |
| 382 | await self.kb_manager.terminate() |
| 383 | self.dashboard_shutdown_event.set() |
| 384 | |
| 385 | # 再次遍历curr_tasks等待每个任务真正结束 |
| 386 | for task in self.curr_tasks: |
| 387 | try: |
| 388 | await task |
| 389 | except asyncio.CancelledError: |
| 390 | pass |
| 391 | except Exception as e: |
| 392 | logger.error(f"任务 {task.get_name()} 发生错误: {e}") |
| 393 | |
| 394 | # 释放数据库引擎连接池 |
| 395 | try: |
| 396 | await self.db.engine.dispose() |
| 397 | except Exception as e: |
| 398 | logger.warning(f"释放数据库引擎失败: {e}") |
| 399 | |
| 400 | async def restart(self) -> None: |
| 401 | """重启 AstrBot 核心生命周期管理类, 终止各个管理器并重新加载平台实例""" |