sycommon-python-lib 0.1.56b5__py3-none-any.whl → 0.1.57b4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. sycommon/config/Config.py +24 -3
  2. sycommon/config/LangfuseConfig.py +15 -0
  3. sycommon/config/SentryConfig.py +13 -0
  4. sycommon/llm/embedding.py +269 -50
  5. sycommon/llm/get_llm.py +9 -218
  6. sycommon/llm/struct_token.py +192 -0
  7. sycommon/llm/sy_langfuse.py +103 -0
  8. sycommon/llm/usage_token.py +117 -0
  9. sycommon/logging/kafka_log.py +187 -433
  10. sycommon/middleware/exception.py +10 -16
  11. sycommon/middleware/timeout.py +2 -1
  12. sycommon/middleware/traceid.py +81 -76
  13. sycommon/notice/uvicorn_monitor.py +32 -27
  14. sycommon/rabbitmq/rabbitmq_client.py +247 -242
  15. sycommon/rabbitmq/rabbitmq_pool.py +201 -123
  16. sycommon/rabbitmq/rabbitmq_service.py +25 -843
  17. sycommon/rabbitmq/rabbitmq_service_client_manager.py +211 -0
  18. sycommon/rabbitmq/rabbitmq_service_connection_monitor.py +73 -0
  19. sycommon/rabbitmq/rabbitmq_service_consumer_manager.py +285 -0
  20. sycommon/rabbitmq/rabbitmq_service_core.py +117 -0
  21. sycommon/rabbitmq/rabbitmq_service_producer_manager.py +238 -0
  22. sycommon/sentry/__init__.py +0 -0
  23. sycommon/sentry/sy_sentry.py +35 -0
  24. sycommon/services.py +122 -96
  25. sycommon/synacos/nacos_client_base.py +121 -0
  26. sycommon/synacos/nacos_config_manager.py +107 -0
  27. sycommon/synacos/nacos_heartbeat_manager.py +144 -0
  28. sycommon/synacos/nacos_service.py +63 -783
  29. sycommon/synacos/nacos_service_discovery.py +157 -0
  30. sycommon/synacos/nacos_service_registration.py +270 -0
  31. sycommon/tools/env.py +62 -0
  32. sycommon/tools/merge_headers.py +20 -0
  33. sycommon/tools/snowflake.py +101 -153
  34. {sycommon_python_lib-0.1.56b5.dist-info → sycommon_python_lib-0.1.57b4.dist-info}/METADATA +10 -8
  35. {sycommon_python_lib-0.1.56b5.dist-info → sycommon_python_lib-0.1.57b4.dist-info}/RECORD +38 -20
  36. {sycommon_python_lib-0.1.56b5.dist-info → sycommon_python_lib-0.1.57b4.dist-info}/WHEEL +0 -0
  37. {sycommon_python_lib-0.1.56b5.dist-info → sycommon_python_lib-0.1.57b4.dist-info}/entry_points.txt +0 -0
  38. {sycommon_python_lib-0.1.56b5.dist-info → sycommon_python_lib-0.1.57b4.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,211 @@
1
+ from typing import Dict
2
+ import asyncio
3
+
4
+ from sycommon.logging.kafka_log import SYLogger
5
+ from sycommon.rabbitmq.rabbitmq_client import RabbitMQClient
6
+ from sycommon.rabbitmq.rabbitmq_service_core import RabbitMQCoreService
7
+
8
+ logger = SYLogger
9
+
10
+
11
+ class RabbitMQClientManager(RabbitMQCoreService):
12
+ """
13
+ RabbitMQ客户端管理类 - 负责客户端的创建、获取、重连、资源清理
14
+ """
15
+ # 客户端存储
16
+ _clients: Dict[str, RabbitMQClient] = {}
17
+ _init_locks: Dict[str, asyncio.Lock] = {}
18
+
19
+ # 模式标记
20
+ _has_listeners: bool = False
21
+ _has_senders: bool = False
22
+
23
+ @classmethod
24
+ def set_mode_flags(cls, has_listeners: bool = False, has_senders: bool = False) -> None:
25
+ """设置模式标记(是否有监听器/发送器)"""
26
+ cls._has_listeners = has_listeners
27
+ cls._has_senders = has_senders
28
+
29
+ @classmethod
30
+ async def _clean_client_resources(cls, client: RabbitMQClient) -> None:
31
+ """清理客户端无效资源"""
32
+ try:
33
+ # 先停止消费
34
+ if client._consumer_tag:
35
+ await client.stop_consuming()
36
+ logger.debug("客户端无效资源清理完成(单通道无需归还)")
37
+ except Exception as e:
38
+ logger.warning(f"释放客户端无效资源失败: {str(e)}")
39
+ finally:
40
+ # 强制重置客户端状态
41
+ client._channel = None
42
+ client._channel_conn = None
43
+ client._exchange = None
44
+ client._queue = None
45
+ client._consumer_tag = None
46
+
47
+ @classmethod
48
+ async def _reconnect_client(cls, client_name: str, client: RabbitMQClient) -> bool:
49
+ """客户端重连"""
50
+ if cls._is_shutdown or not (cls._connection_pool and await cls._connection_pool.is_alive):
51
+ return False
52
+
53
+ # 重连冷却
54
+ await asyncio.sleep(cls.RECONNECT_INTERVAL)
55
+
56
+ try:
57
+ # 清理旧资源
58
+ await cls._clean_client_resources(client)
59
+
60
+ # 执行重连
61
+ await client.connect()
62
+
63
+ # 验证重连结果
64
+ if await client.is_connected:
65
+ logger.info(f"客户端 '{client_name}' 重连成功")
66
+ return True
67
+ else:
68
+ logger.warning(f"客户端 '{client_name}' 重连失败:资源未完全初始化")
69
+ return False
70
+ except Exception as e:
71
+ logger.error(f"客户端 '{client_name}' 重连失败: {str(e)}", exc_info=True)
72
+ return False
73
+
74
+ @classmethod
75
+ async def _create_client(cls, **kwargs) -> RabbitMQClient:
76
+ """创建客户端实例"""
77
+ if cls._is_shutdown:
78
+ raise RuntimeError("RabbitMQService已关闭,无法创建客户端")
79
+
80
+ # 等待连接池就绪
81
+ await cls.wait_for_pool_ready()
82
+
83
+ app_name = kwargs.get('app_name', cls._config.get(
84
+ "APP_NAME", "")) if cls._config else ""
85
+ queue_name = kwargs.get('queue_name', '')
86
+
87
+ create_if_not_exists = kwargs.get('create_if_not_exists', True)
88
+
89
+ processed_queue_name = queue_name
90
+ if create_if_not_exists and processed_queue_name and app_name:
91
+ if not processed_queue_name.endswith(f".{app_name}"):
92
+ processed_queue_name = f"{processed_queue_name}.{app_name}"
93
+ logger.info(f"监听器队列名称自动拼接app-name: {processed_queue_name}")
94
+ else:
95
+ logger.info(f"监听器队列已包含app-name: {processed_queue_name}")
96
+
97
+ logger.info(
98
+ f"创建客户端 - 原始队列名: {queue_name}, "
99
+ f"处理后队列名: {processed_queue_name}, "
100
+ f"是否创建队列: {create_if_not_exists}"
101
+ )
102
+
103
+ final_queue_name = None
104
+ if create_if_not_exists and processed_queue_name.endswith(f".{app_name}"):
105
+ final_queue_name = processed_queue_name
106
+
107
+ # 创建客户端实例
108
+ client = RabbitMQClient(
109
+ connection_pool=cls._connection_pool,
110
+ exchange_name=cls._config.get(
111
+ 'exchange_name', "system.topic.exchange"),
112
+ exchange_type=kwargs.get('exchange_type', "topic"),
113
+ queue_name=final_queue_name,
114
+ app_name=app_name,
115
+ routing_key=kwargs.get(
116
+ 'routing_key',
117
+ f"{queue_name.split('.')[0]}.#"
118
+ ),
119
+ durable=kwargs.get('durable', True),
120
+ auto_delete=kwargs.get('auto_delete', False),
121
+ auto_parse_json=kwargs.get('auto_parse_json', True),
122
+ create_if_not_exists=create_if_not_exists,
123
+ prefetch_count=kwargs.get('prefetch_count', 2),
124
+ )
125
+
126
+ # 连接客户端
127
+ await client.connect()
128
+
129
+ return client
130
+
131
+ @classmethod
132
+ async def get_client(
133
+ cls,
134
+ client_name: str = "default",
135
+ client_type: str = "sender", # sender(发送器)/listener(监听器),默认sender
136
+ **kwargs
137
+ ) -> RabbitMQClient:
138
+ """
139
+ 获取或创建RabbitMQ客户端
140
+ :param client_name: 客户端名称
141
+ :param client_type: 客户端类型 - sender(发送器)/listener(监听器)
142
+ :param kwargs: 其他参数
143
+ :return: RabbitMQClient实例
144
+ """
145
+ if cls._is_shutdown:
146
+ raise RuntimeError("RabbitMQService已关闭,无法获取客户端")
147
+
148
+ # 校验client_type合法性
149
+ if client_type not in ["sender", "listener"]:
150
+ raise ValueError(
151
+ f"client_type只能是sender/listener,当前值:{client_type}")
152
+
153
+ # 等待连接池就绪
154
+ await cls.wait_for_pool_ready()
155
+
156
+ # 确保锁存在
157
+ if client_name not in cls._init_locks:
158
+ cls._init_locks[client_name] = asyncio.Lock()
159
+
160
+ async with cls._init_locks[client_name]:
161
+ # ===== 原有“客户端已存在”的逻辑保留 =====
162
+ if client_name in cls._clients:
163
+ client = cls._clients[client_name]
164
+ # 核心:根据client_type重置客户端的队列创建配置
165
+ if client_type == "sender":
166
+ client.create_if_not_exists = False
167
+ else: # listener
168
+ client.create_if_not_exists = True
169
+ # 监听器必须有队列名,从kwargs补全
170
+ if not client.queue_name and kwargs.get("queue_name"):
171
+ client.queue_name = kwargs.get("queue_name")
172
+
173
+ if await client.is_connected:
174
+ return client
175
+ else:
176
+ logger.info(f"客户端 '{client_name}' 连接已断开,重新创建")
177
+ await cls._clean_client_resources(client)
178
+
179
+ # ===== 核心逻辑:根据client_type统一控制队列创建 =====
180
+ if client_type == "sender":
181
+ kwargs["create_if_not_exists"] = False # 禁用创建队列
182
+ else:
183
+ if not kwargs.get("queue_name"):
184
+ raise ValueError("监听器类型必须指定queue_name参数")
185
+ kwargs["create_if_not_exists"] = True
186
+
187
+ client = await cls._create_client(
188
+ **kwargs
189
+ )
190
+
191
+ # 监听器额外验证队列创建结果
192
+ if client_type == "listener" and not client._queue:
193
+ raise RuntimeError(f"监听器队列 '{kwargs['queue_name']}' 创建失败")
194
+
195
+ # 存储客户端
196
+ cls._clients[client_name] = client
197
+ return client
198
+
199
+ @classmethod
200
+ async def shutdown_clients(cls, timeout: float = 15.0) -> None:
201
+ """关闭所有客户端"""
202
+ # 关闭所有客户端
203
+ for client in cls._clients.values():
204
+ try:
205
+ await client.close()
206
+ except Exception as e:
207
+ logger.error(f"关闭客户端失败: {str(e)}")
208
+
209
+ # 清理客户端状态
210
+ cls._clients.clear()
211
+ cls._init_locks.clear()
@@ -0,0 +1,73 @@
1
+ import asyncio
2
+ from typing import Optional
3
+ from sycommon.rabbitmq.rabbitmq_service_client_manager import RabbitMQClientManager
4
+ from sycommon.logging.kafka_log import SYLogger
5
+
6
+ logger = SYLogger
7
+
8
+
9
+ class RabbitMQConnectionMonitor(RabbitMQClientManager):
10
+ """
11
+ RabbitMQ连接监控类 - 负责连接状态监控、自动重连
12
+ """
13
+ _connection_monitor_task: Optional[asyncio.Task] = None
14
+
15
+ @classmethod
16
+ async def _monitor_connections(cls):
17
+ """连接监控任务:定期检查所有客户端连接状态"""
18
+ logger.info("RabbitMQ连接监控任务启动")
19
+ while not cls._is_shutdown:
20
+ try:
21
+ await asyncio.sleep(cls.RECONNECT_INTERVAL)
22
+
23
+ # 跳过未初始化的连接池
24
+ if not cls._connection_pool or not cls._connection_pool._initialized:
25
+ continue
26
+
27
+ # 检查连接池本身状态
28
+ pool_alive = await cls._connection_pool.is_alive
29
+ if not pool_alive:
30
+ logger.error("RabbitMQ连接池已断开,等待原生自动重连")
31
+ continue
32
+
33
+ # 检查所有客户端连接
34
+ for client_name, client in list(cls._clients.items()):
35
+ try:
36
+ client_connected = await client.is_connected
37
+ if not client_connected:
38
+ logger.warning(
39
+ f"客户端 '{client_name}' 连接异常,触发重连")
40
+ asyncio.create_task(
41
+ cls._reconnect_client(client_name, client))
42
+ except Exception as e:
43
+ logger.error(
44
+ f"监控客户端 '{client_name}' 连接状态失败: {str(e)}", exc_info=True)
45
+
46
+ except Exception as e:
47
+ logger.error("RabbitMQ连接监控任务异常", exc_info=True)
48
+ await asyncio.sleep(cls.RECONNECT_INTERVAL)
49
+
50
+ logger.info("RabbitMQ连接监控任务停止")
51
+
52
+ @classmethod
53
+ def start_connection_monitor(cls) -> None:
54
+ """启动连接监控任务"""
55
+ if cls._connection_monitor_task and not cls._connection_monitor_task.done():
56
+ return
57
+
58
+ cls._connection_monitor_task = asyncio.create_task(
59
+ cls._monitor_connections())
60
+
61
+ @classmethod
62
+ async def stop_connection_monitor(cls, timeout: float = 15.0) -> None:
63
+ """停止连接监控任务"""
64
+ if cls._connection_monitor_task and not cls._connection_monitor_task.done():
65
+ cls._connection_monitor_task.cancel()
66
+ try:
67
+ await asyncio.wait_for(cls._connection_monitor_task, timeout=timeout)
68
+ except asyncio.TimeoutError:
69
+ logger.warning("连接监控任务关闭超时")
70
+ except Exception as e:
71
+ logger.error(f"关闭连接监控任务失败: {str(e)}")
72
+
73
+ cls._connection_monitor_task = None
@@ -0,0 +1,285 @@
1
+ from typing import Dict, List, Callable, Coroutine, Any
2
+ import asyncio
3
+ from aio_pika.abc import AbstractIncomingMessage, ConsumerTag
4
+
5
+ from sycommon.logging.kafka_log import SYLogger
6
+ from sycommon.models.mqlistener_config import RabbitMQListenerConfig
7
+ from sycommon.rabbitmq.rabbitmq_service_client_manager import RabbitMQClientManager
8
+ from sycommon.models.mqmsg_model import MQMsgModel
9
+
10
+ logger = SYLogger
11
+
12
+
13
+ class RabbitMQConsumerManager(RabbitMQClientManager):
14
+ """
15
+ RabbitMQ消费者管理类 - 负责监听器设置、消费启动/停止、消费验证
16
+ """
17
+ # 消费者相关状态
18
+ _message_handlers: Dict[str, Callable[[
19
+ MQMsgModel, AbstractIncomingMessage], Coroutine[Any, Any, None]]] = {}
20
+ _consumer_tasks: Dict[str, asyncio.Task] = {}
21
+ _consumer_events: Dict[str, asyncio.Event] = {}
22
+ _consumer_tags: Dict[str, ConsumerTag] = {}
23
+
24
+ # 配置常量
25
+ CONSUMER_START_TIMEOUT = 30 # 消费启动超时(秒)
26
+
27
+ @classmethod
28
+ async def add_listener(
29
+ cls,
30
+ queue_name: str,
31
+ handler: Callable[[MQMsgModel, AbstractIncomingMessage], Coroutine[Any, Any, None]], **kwargs
32
+ ) -> None:
33
+ """添加消息监听器"""
34
+ if cls._is_shutdown:
35
+ logger.warning("服务已关闭,无法添加监听器")
36
+ return
37
+
38
+ if queue_name in cls._message_handlers:
39
+ logger.info(f"监听器 '{queue_name}' 已存在,跳过重复添加")
40
+ return
41
+
42
+ # 创建并初始化客户端
43
+ await cls.get_client(
44
+ client_name=queue_name,
45
+ client_type="listener",
46
+ queue_name=queue_name,
47
+ ** kwargs
48
+ )
49
+
50
+ # 注册消息处理器
51
+ cls._message_handlers[queue_name] = handler
52
+ logger.info(f"监听器 '{queue_name}' 已添加")
53
+
54
+ @classmethod
55
+ async def setup_listeners(cls, listeners: List[RabbitMQListenerConfig], has_senders: bool = False, **kwargs) -> None:
56
+ """设置消息监听器"""
57
+ if cls._is_shutdown:
58
+ logger.warning("服务已关闭,无法设置监听器")
59
+ return
60
+
61
+ cls.set_mode_flags(has_listeners=True, has_senders=has_senders)
62
+ logger.info(f"开始设置 {len(listeners)} 个消息监听器")
63
+
64
+ for idx, listener_config in enumerate(listeners):
65
+ try:
66
+ # 转换配置
67
+ listener_dict = listener_config.model_dump()
68
+ listener_dict['create_if_not_exists'] = True
69
+ listener_dict['prefetch_count'] = listener_config.prefetch_count
70
+ queue_name = listener_dict['queue_name']
71
+
72
+ logger.info(
73
+ f"设置监听器 {idx+1}/{len(listeners)}: {queue_name} (prefetch_count: {listener_config.prefetch_count})")
74
+
75
+ # 添加监听器
76
+ await cls.add_listener(**listener_dict)
77
+ except Exception as e:
78
+ logger.error(
79
+ f"设置监听器 {idx+1} 失败: {str(e)}", exc_info=True)
80
+ logger.warning("继续处理其他监听器")
81
+
82
+ # 启动所有消费者
83
+ await cls.start_all_consumers()
84
+
85
+ # 验证消费者启动结果
86
+ await cls._verify_consumers_started()
87
+
88
+ logger.info(f"消息监听器设置完成")
89
+
90
+ @classmethod
91
+ async def _verify_consumers_started(cls, timeout: int = 30) -> None:
92
+ """验证消费者是否成功启动"""
93
+ start_time = asyncio.get_event_loop().time()
94
+ required_clients = list(cls._message_handlers.keys())
95
+ running_clients = []
96
+
97
+ while len(running_clients) < len(required_clients) and \
98
+ (asyncio.get_event_loop().time() - start_time) < timeout and \
99
+ not cls._is_shutdown:
100
+
101
+ running_clients = [
102
+ name for name, task in cls._consumer_tasks.items()
103
+ if not task.done() and name in cls._consumer_tags
104
+ ]
105
+
106
+ logger.info(
107
+ f"消费者启动验证: {len(running_clients)}/{len(required_clients)} 已启动")
108
+ await asyncio.sleep(1)
109
+
110
+ failed_clients = [
111
+ name for name in required_clients if name not in running_clients and not cls._is_shutdown]
112
+ if failed_clients:
113
+ logger.error(f"以下消费者启动失败: {', '.join(failed_clients)}")
114
+ for client_name in failed_clients:
115
+ logger.info(f"尝试重新启动消费者: {client_name}")
116
+ asyncio.create_task(cls.start_consumer(client_name))
117
+
118
+ @classmethod
119
+ async def start_all_consumers(cls) -> None:
120
+ """启动所有已注册的消费者"""
121
+ if cls._is_shutdown:
122
+ logger.warning("服务已关闭,无法启动消费者")
123
+ return
124
+
125
+ for client_name in cls._message_handlers:
126
+ await cls.start_consumer(client_name)
127
+
128
+ @classmethod
129
+ async def start_consumer(cls, client_name: str) -> None:
130
+ """启动指定客户端的消费者"""
131
+ if cls._is_shutdown:
132
+ logger.warning("服务已关闭,无法启动消费者")
133
+ return
134
+
135
+ # 检查任务状态
136
+ if client_name in cls._consumer_tasks:
137
+ existing_task = cls._consumer_tasks[client_name]
138
+ if not existing_task.done():
139
+ if existing_task.exception() is not None:
140
+ logger.info(f"消费者 '{client_name}' 任务异常,重启")
141
+ existing_task.cancel()
142
+ else:
143
+ logger.info(f"消费者 '{client_name}' 已在运行中,无需重复启动")
144
+ return
145
+ else:
146
+ logger.info(f"消费者 '{client_name}' 任务已完成,重新启动")
147
+
148
+ if client_name not in cls._clients:
149
+ raise ValueError(f"RabbitMQ客户端 '{client_name}' 未初始化")
150
+
151
+ client = cls._clients[client_name]
152
+ handler = cls._message_handlers.get(client_name)
153
+
154
+ if not handler:
155
+ logger.warning(f"未找到客户端 '{client_name}' 的处理器")
156
+ return
157
+
158
+ # 设置消息处理器
159
+ await client.set_message_handler(handler)
160
+
161
+ # 确保客户端已连接
162
+ start_time = asyncio.get_event_loop().time()
163
+ while not await client.is_connected and not cls._is_shutdown:
164
+ if asyncio.get_event_loop().time() - start_time > cls.CONSUMER_START_TIMEOUT:
165
+ raise TimeoutError(f"等待客户端 '{client_name}' 连接超时")
166
+
167
+ logger.info(f"等待客户端 '{client_name}' 连接就绪...")
168
+ await asyncio.sleep(1)
169
+ if cls._is_shutdown:
170
+ return
171
+
172
+ # 创建停止事件
173
+ stop_event = asyncio.Event()
174
+ cls._consumer_events[client_name] = stop_event
175
+
176
+ # 定义消费任务
177
+ async def consume_task():
178
+ try:
179
+ # 启动消费,带重试机制
180
+ max_attempts = 3
181
+ attempt = 0
182
+ consumer_tag = None
183
+
184
+ while attempt < max_attempts and not stop_event.is_set() and not cls._is_shutdown:
185
+ try:
186
+ # 启动消费前再次校验
187
+ if not await client.is_connected:
188
+ logger.info(f"消费者 '{client_name}' 连接断开,尝试重连")
189
+ await client.connect()
190
+
191
+ if not client._queue:
192
+ raise Exception("队列未初始化完成")
193
+ if not client._message_handler:
194
+ raise Exception("消息处理器未设置")
195
+
196
+ consumer_tag = await client.start_consuming()
197
+ if consumer_tag:
198
+ break
199
+ except Exception as e:
200
+ attempt += 1
201
+ logger.warning(
202
+ f"启动消费者尝试 {attempt}/{max_attempts} 失败: {str(e)}")
203
+ if attempt < max_attempts:
204
+ await asyncio.sleep(2)
205
+
206
+ if cls._is_shutdown:
207
+ return
208
+
209
+ if not consumer_tag:
210
+ raise Exception(f"经过 {max_attempts} 次尝试仍无法启动消费者")
211
+
212
+ # 记录消费者标签
213
+ cls._consumer_tags[client_name] = consumer_tag
214
+ logger.info(
215
+ f"消费者 '{client_name}' 开始消费(单通道),tag: {consumer_tag}"
216
+ )
217
+
218
+ # 等待停止事件
219
+ await stop_event.wait()
220
+ logger.info(f"收到停止信号,消费者 '{client_name}' 准备退出")
221
+
222
+ except asyncio.CancelledError:
223
+ logger.info(f"消费者 '{client_name}' 被取消")
224
+ except Exception as e:
225
+ logger.error(
226
+ f"消费者 '{client_name}' 错误: {str(e)}", exc_info=True)
227
+ # 非主动停止时尝试重启
228
+ if not stop_event.is_set() and not cls._is_shutdown:
229
+ logger.info(f"尝试重启消费者 '{client_name}'")
230
+ await asyncio.sleep(cls.RECONNECT_INTERVAL)
231
+ asyncio.create_task(cls.start_consumer(client_name))
232
+ finally:
233
+ # 清理资源
234
+ try:
235
+ await client.stop_consuming()
236
+ except Exception as e:
237
+ logger.error(f"停止消费者 '{client_name}' 时出错: {str(e)}")
238
+
239
+ # 移除状态记录
240
+ if client_name in cls._consumer_tags:
241
+ del cls._consumer_tags[client_name]
242
+ if client_name in cls._consumer_events:
243
+ del cls._consumer_events[client_name]
244
+
245
+ logger.info(f"消费者 '{client_name}' 已停止")
246
+
247
+ # 创建并跟踪消费任务
248
+ task = asyncio.create_task(
249
+ consume_task(), name=f"consumer-{client_name}")
250
+ cls._consumer_tasks[client_name] = task
251
+
252
+ # 添加任务完成回调
253
+ def task_done_callback(t: asyncio.Task) -> None:
254
+ try:
255
+ if t.done():
256
+ t.result()
257
+ except Exception as e:
258
+ logger.error(f"消费者任务 '{client_name}' 异常结束: {str(e)}")
259
+ if client_name in cls._message_handlers and not cls._is_shutdown:
260
+ asyncio.create_task(cls.start_consumer(client_name))
261
+
262
+ task.add_done_callback(task_done_callback)
263
+ logger.info(f"消费者任务 '{client_name}' 已创建")
264
+
265
+ @classmethod
266
+ async def shutdown_consumers(cls, timeout: float = 15.0) -> None:
267
+ """关闭所有消费者"""
268
+ # 停止所有消费者任务
269
+ for client_name, task in cls._consumer_tasks.items():
270
+ if not task.done():
271
+ # 触发停止事件
272
+ if client_name in cls._consumer_events:
273
+ cls._consumer_events[client_name].set()
274
+ # 取消任务
275
+ task.cancel()
276
+ try:
277
+ await asyncio.wait_for(task, timeout=timeout)
278
+ except Exception as e:
279
+ logger.error(f"关闭消费者 '{client_name}' 失败: {str(e)}")
280
+
281
+ # 清理消费者状态
282
+ cls._message_handlers.clear()
283
+ cls._consumer_tasks.clear()
284
+ cls._consumer_events.clear()
285
+ cls._consumer_tags.clear()
@@ -0,0 +1,117 @@
1
+ import asyncio
2
+ from typing import Optional
3
+
4
+ from sycommon.logging.kafka_log import SYLogger
5
+ from sycommon.rabbitmq.rabbitmq_client import RabbitMQConnectionPool
6
+
7
+ logger = SYLogger
8
+
9
+
10
+ class RabbitMQCoreService:
11
+ """
12
+ RabbitMQ核心服务类 - 负责基础初始化、连接池管理、全局状态控制
13
+ """
14
+ # 全局共享状态
15
+ _config: Optional[dict] = None
16
+ _connection_pool: Optional[RabbitMQConnectionPool] = None
17
+ _is_shutdown: bool = False
18
+ _shutdown_lock = asyncio.Lock()
19
+
20
+ # 配置常量
21
+ RECONNECT_INTERVAL = 15 # 重连基础间隔(秒)
22
+ CONNECTION_POOL_TIMEOUT = 30 # 连接池初始化超时(秒)
23
+
24
+ @classmethod
25
+ def init_config(cls, config: dict) -> None:
26
+ """初始化基础配置(从Nacos加载)"""
27
+ from sycommon.synacos.nacos_service import NacosService
28
+
29
+ if cls._config:
30
+ logger.warning("RabbitMQ配置已初始化,无需重复调用")
31
+ return
32
+
33
+ # 从Nacos获取MQ配置
34
+ cls._config = NacosService(config).share_configs.get(
35
+ "mq.yml", {}).get('spring', {}).get('rabbitmq', {})
36
+ cls._config["APP_NAME"] = config.get("Name", "")
37
+
38
+ # 打印关键配置信息
39
+ logger.info(
40
+ f"RabbitMQ服务初始化 - 集群节点: {cls._config.get('host')}, "
41
+ f"端口: {cls._config.get('port')}, "
42
+ f"虚拟主机: {cls._config.get('virtual-host')}, "
43
+ f"应用名: {cls._config.get('APP_NAME')}, "
44
+ f"心跳: {cls._config.get('heartbeat', 30)}s"
45
+ )
46
+ cls._is_shutdown = False
47
+
48
+ @classmethod
49
+ async def init_connection_pool(cls) -> None:
50
+ """初始化单通道连接池(带重试机制)"""
51
+ if cls._connection_pool or not cls._config or cls._is_shutdown:
52
+ return
53
+
54
+ try:
55
+ # 解析集群节点
56
+ hosts_str = cls._config.get('host', "")
57
+ hosts_list = [host.strip()
58
+ for host in hosts_str.split(',') if host.strip()]
59
+ if not hosts_list:
60
+ raise ValueError("RabbitMQ集群配置为空,请检查host参数")
61
+
62
+ global_prefetch_count = cls._config.get('prefetch_count', 2)
63
+
64
+ # 创建单通道连接池
65
+ cls._connection_pool = RabbitMQConnectionPool(
66
+ hosts=hosts_list,
67
+ port=cls._config.get('port', 5672),
68
+ username=cls._config.get('username', ""),
69
+ password=cls._config.get('password', ""),
70
+ virtualhost=cls._config.get('virtual-host', "/"),
71
+ app_name=cls._config.get("APP_NAME", ""),
72
+ prefetch_count=global_prefetch_count,
73
+ heartbeat=cls._config.get('heartbeat', 15),
74
+ connection_timeout=cls._config.get('connection_timeout', 15),
75
+ reconnect_interval=cls._config.get('reconnect_interval', 5),
76
+ )
77
+
78
+ # 初始化连接池
79
+ await asyncio.wait_for(cls._connection_pool.init_pools(), timeout=cls.CONNECTION_POOL_TIMEOUT)
80
+ logger.info("RabbitMQ单通道连接池初始化成功")
81
+
82
+ except Exception as e:
83
+ logger.error(f"RabbitMQ连接池初始化失败: {str(e)}", exc_info=True)
84
+ # 连接池初始化失败时重试(未关闭状态下)
85
+ if not cls._is_shutdown:
86
+ await asyncio.sleep(cls.RECONNECT_INTERVAL)
87
+ asyncio.create_task(cls.init_connection_pool())
88
+
89
+ @classmethod
90
+ async def wait_for_pool_ready(cls) -> None:
91
+ """等待连接池就绪(带超时)"""
92
+ if not cls._config:
93
+ raise ValueError("RabbitMQ配置尚未初始化,请先调用init_config方法")
94
+
95
+ start_time = asyncio.get_event_loop().time()
96
+ while not (cls._connection_pool and cls._connection_pool._initialized) and not cls._is_shutdown:
97
+ if asyncio.get_event_loop().time() - start_time > cls.CONNECTION_POOL_TIMEOUT:
98
+ raise TimeoutError("等待连接池初始化超时")
99
+ await asyncio.sleep(1)
100
+
101
+ if cls._is_shutdown:
102
+ raise RuntimeError("服务关闭中,取消等待连接池")
103
+
104
+ @classmethod
105
+ async def shutdown_core_resources(cls, timeout: float = 15.0) -> None:
106
+ """关闭核心资源(连接池)"""
107
+ # 关闭连接池
108
+ if cls._connection_pool and cls._connection_pool._initialized:
109
+ try:
110
+ await cls._connection_pool.close()
111
+ logger.info("RabbitMQ单通道连接池已关闭")
112
+ except Exception as e:
113
+ logger.error(f"关闭连接池失败: {str(e)}")
114
+
115
+ # 清理核心状态
116
+ cls._config = None
117
+ cls._connection_pool = None