sycommon-python-lib 0.1.8__tar.gz → 0.1.9__tar.gz

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.

Potentially problematic release.


This version of sycommon-python-lib might be problematic. Click here for more details.

Files changed (51) hide show
  1. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/PKG-INFO +1 -1
  2. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/pyproject.toml +1 -1
  3. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_client.py +68 -40
  4. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_service.py +103 -46
  5. sycommon_python_lib-0.1.9/src/sycommon/services.py +208 -0
  6. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/PKG-INFO +1 -1
  7. sycommon_python_lib-0.1.8/src/sycommon/services.py +0 -217
  8. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/README.md +0 -0
  9. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/setup.cfg +0 -0
  10. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/__init__.py +0 -0
  11. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/Config.py +0 -0
  12. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/DatabaseConfig.py +0 -0
  13. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/EmbeddingConfig.py +0 -0
  14. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/LLMConfig.py +0 -0
  15. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/MQConfig.py +0 -0
  16. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/RerankerConfig.py +0 -0
  17. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/config/__init__.py +0 -0
  18. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/database/base_db_service.py +0 -0
  19. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/database/database_service.py +0 -0
  20. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/health/__init__.py +0 -0
  21. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/health/health_check.py +0 -0
  22. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/health/ping.py +0 -0
  23. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/logging/__init__.py +0 -0
  24. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/logging/kafka_log.py +0 -0
  25. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/logging/logger_wrapper.py +0 -0
  26. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/__init__.py +0 -0
  27. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/context.py +0 -0
  28. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/cors.py +0 -0
  29. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/exception.py +0 -0
  30. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/middleware.py +0 -0
  31. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/monitor_memory.py +0 -0
  32. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/mq.py +0 -0
  33. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/timeout.py +0 -0
  34. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/traceid.py +0 -0
  35. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/__init__.py +0 -0
  36. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/base_http.py +0 -0
  37. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/log.py +0 -0
  38. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqlistener_config.py +0 -0
  39. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqmsg_model.py +0 -0
  40. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqsend_config.py +0 -0
  41. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/models/sso_user.py +0 -0
  42. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/__init__.py +0 -0
  43. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/feign.py +0 -0
  44. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/nacos_service.py +0 -0
  45. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/tools/__init__.py +0 -0
  46. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/tools/snowflake.py +0 -0
  47. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon/tools/timing.py +0 -0
  48. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/SOURCES.txt +0 -0
  49. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
  50. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/requires.txt +0 -0
  51. {sycommon_python_lib-0.1.8 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.8
3
+ Version: 0.1.9
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sycommon-python-lib"
3
- version = "0.1.8"
3
+ version = "0.1.9"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -183,8 +183,14 @@ class RabbitMQClient:
183
183
  await asyncio.sleep(1)
184
184
  return False
185
185
 
186
- async def connect(self, force_reconnect: bool = False, max_retries: int = 3) -> None:
187
- """建立连接并检查/创建资源"""
186
+ async def connect(self, force_reconnect: bool = False, declare_queue: bool = True) -> None:
187
+ """建立连接并检查/创建资源,新增declare_queue参数控制是否声明队列"""
188
+ # 增加日志确认参数状态
189
+ logging.debug(
190
+ f"connect() 调用 - force_reconnect={force_reconnect}, "
191
+ f"declare_queue={declare_queue}, create_if_not_exists={self.create_if_not_exists}"
192
+ )
193
+
188
194
  if self.is_connected and not force_reconnect:
189
195
  return
190
196
 
@@ -196,7 +202,8 @@ class RabbitMQClient:
196
202
  f"尝试连接RabbitMQ - 主机: {self.host}:{self.port}, "
197
203
  f"虚拟主机: {self.virtualhost}, "
198
204
  f"队列: {self.queue_name}, "
199
- f"心跳间隔: {self.heartbeat}s"
205
+ f"声明队列: {declare_queue}, "
206
+ f"允许创建: {self.create_if_not_exists}"
200
207
  )
201
208
 
202
209
  # 重置状态
@@ -207,13 +214,13 @@ class RabbitMQClient:
207
214
  retries = 0
208
215
  last_exception = None
209
216
 
210
- while retries < max_retries:
217
+ while retries < 3: # 使用固定重试次数
211
218
  try:
212
219
  # 关闭旧连接
213
220
  if self.connection and not self.connection.is_closed:
214
221
  await self.connection.close()
215
222
 
216
- # 建立新连接 - 配置更短的心跳间隔
223
+ # 建立新连接
217
224
  self.connection = await asyncio.wait_for(
218
225
  aio_pika.connect_robust(
219
226
  host=self.host,
@@ -223,7 +230,7 @@ class RabbitMQClient:
223
230
  virtualhost=self.virtualhost,
224
231
  heartbeat=self.heartbeat,
225
232
  client_properties={
226
- "connection_name": self.app_name or "rabbitmq-client"} # 增加连接标识
233
+ "connection_name": self.app_name or "rabbitmq-client"}
227
234
  ),
228
235
  timeout=self.connection_timeout
229
236
  )
@@ -262,28 +269,29 @@ class RabbitMQClient:
262
269
  )
263
270
  logging.info(f"使用已存在的交换机 '{self.exchange_name}'")
264
271
 
265
- # 2. 处理队列
266
- if self.queue_name:
272
+ # 2. 处理队列 - 只有declare_queue为True时才处理
273
+ if declare_queue and self.queue_name:
267
274
  queue_exists = await self._check_queue_exists()
268
275
 
269
276
  if not queue_exists:
270
- if self.create_if_not_exists:
271
- # 创建队列
272
- self.queue = await asyncio.wait_for(
273
- self.channel.declare_queue(
274
- name=self.queue_name,
275
- durable=self.durable,
276
- auto_delete=self.auto_delete,
277
- exclusive=False,
278
- passive=False
279
- ),
280
- timeout=self.rpc_timeout
281
- )
282
- self._queue_exists = True
283
- logging.info(f"已创建队列 '{self.queue_name}'")
284
- else:
277
+ # 关键检查点:确保有权限创建队列
278
+ if not self.create_if_not_exists:
285
279
  raise Exception(
286
280
  f"队列 '{self.queue_name}' 不存在且不允许自动创建")
281
+
282
+ # 创建队列
283
+ self.queue = await asyncio.wait_for(
284
+ self.channel.declare_queue(
285
+ name=self.queue_name,
286
+ durable=self.durable,
287
+ auto_delete=self.auto_delete,
288
+ exclusive=False,
289
+ passive=False
290
+ ),
291
+ timeout=self.rpc_timeout
292
+ )
293
+ self._queue_exists = True
294
+ logging.info(f"已创建队列 '{self.queue_name}'")
287
295
  else:
288
296
  # 获取已有队列
289
297
  self.queue = await asyncio.wait_for(
@@ -292,12 +300,18 @@ class RabbitMQClient:
292
300
  )
293
301
  logging.info(f"使用已存在的队列 '{self.queue_name}'")
294
302
 
295
- # 3. 绑定队列到交换机
296
- if self.queue and self.exchange:
297
- bound = await self._bind_queue()
298
- if not bound:
299
- raise Exception(
300
- f"队列 '{self.queue_name}' 绑定到交换机 '{self.exchange_name}' 失败")
303
+ # 3. 绑定队列到交换机
304
+ if self.queue and self.exchange:
305
+ bound = await self._bind_queue()
306
+ if not bound:
307
+ raise Exception(
308
+ f"队列 '{self.queue_name}' 绑定到交换机 '{self.exchange_name}' 失败")
309
+ else:
310
+ # 不声明队列时,将队列相关状态设为False
311
+ self.queue = None
312
+ self._queue_exists = False
313
+ self._queue_bound = False
314
+ logging.debug(f"跳过队列 '{self.queue_name}' 的声明和绑定")
301
315
 
302
316
  # 如果之前在消费,重新开始消费
303
317
  if self._is_consuming and self.message_handler:
@@ -308,23 +322,22 @@ class RabbitMQClient:
308
322
  self._start_keepalive_task()
309
323
 
310
324
  self._update_activity_timestamp()
311
- logging.info(f"RabbitMQ客户端连接成功 (队列: {self.queue_name})")
325
+ logging.info(
326
+ f"RabbitMQ客户端连接成功 (队列: {self.queue_name}, 声明队列: {declare_queue})")
312
327
  return
313
328
 
314
329
  except Exception as e:
315
330
  retries += 1
316
331
  last_exception = e
317
332
  logging.warning(
318
- f"连接失败({retries}/{max_retries}): {str(e)},重试中...")
333
+ f"连接失败({retries}/3): {str(e)}, create_if_not_exists={self.create_if_not_exists}, 重试中...")
319
334
 
320
- if retries < max_retries:
335
+ if retries < 3:
321
336
  await asyncio.sleep(self.reconnection_delay)
322
337
 
323
- logging.error(f"最终连接失败: {str(last_exception)}", exc_info=True)
338
+ logging.error(f"最终连接失败: {str(last_exception)}")
324
339
  raise Exception(
325
- f"经过{max_retries}次重试后仍无法完成连接和资源初始化。"
326
- f"最后错误: {str(last_exception)}"
327
- )
340
+ f"经过3次重试后仍无法完成连接和资源初始化。最后错误: {str(last_exception)}")
328
341
 
329
342
  def _start_connection_monitor(self):
330
343
  """启动连接监控任务,检测连接/通道关闭"""
@@ -534,16 +547,31 @@ class RabbitMQClient:
534
547
  self.message_handler = handler
535
548
 
536
549
  async def start_consuming(self, timeout: Optional[float] = None) -> str:
537
- """开始消费消息并返回consumer_tag,支持超时控制"""
550
+ """开始消费消息并返回consumer_tag,支持超时控制和队列检查重试"""
538
551
  if self._is_consuming:
539
552
  logging.debug("已经在消费中,返回现有consumer_tag")
540
553
  return self._consumer_tag
541
554
 
542
- if not self.is_connected:
543
- await self.connect()
555
+ # 增加队列检查和连接确保逻辑
556
+ max_attempts = 5
557
+ attempt = 0
558
+ while attempt < max_attempts:
559
+ if not self.is_connected:
560
+ await self.connect()
561
+
562
+ if self.queue:
563
+ break
564
+
565
+ attempt += 1
566
+ logging.warning(f"队列尚未初始化,等待后重试({attempt}/{max_attempts})")
567
+ await asyncio.sleep(1)
544
568
 
545
569
  if not self.queue:
546
- raise Exception("队列未初始化")
570
+ # 最后尝试一次显式连接并声明队列
571
+ logging.warning("最后尝试重新连接并声明队列")
572
+ await self.connect(force_reconnect=True, declare_queue=True)
573
+ if not self.queue:
574
+ raise Exception("队列未初始化,多次尝试后仍无法创建")
547
575
 
548
576
  if not self.message_handler:
549
577
  raise Exception("未设置消息处理函数")
@@ -31,15 +31,22 @@ class RabbitMQService:
31
31
  _initialized_queues: Dict[str, Dict[str, bool]] = {}
32
32
  # 添加异步锁
33
33
  _init_lock: Dict[str, asyncio.Lock] = {}
34
+ _has_listeners: bool = False
35
+ _has_senders: bool = False
34
36
 
35
37
  @classmethod
36
- def init(cls, config: dict) -> Type['RabbitMQService']:
37
- """初始化RabbitMQ服务,保存配置"""
38
+ def init(cls, config: dict, has_listeners: bool = False, has_senders: bool = False) -> Type['RabbitMQService']:
39
+ """初始化RabbitMQ服务,保存配置和发送器/监听器状态"""
38
40
  from sycommon.synacos.nacos_service import NacosService
39
41
  # 获取 common 配置
40
42
  cls.config = NacosService(config).share_configs.get(
41
43
  "mq.yml", {}).get('spring', {}).get('rabbitmq', {})
42
44
  cls.config["APP_NAME"] = config.get("Name", "")
45
+
46
+ # 保存发送器和监听器存在状态
47
+ cls._has_listeners = has_listeners
48
+ cls._has_senders = has_senders
49
+
43
50
  return cls()
44
51
 
45
52
  @classmethod
@@ -56,21 +63,35 @@ class RabbitMQService:
56
63
 
57
64
  @classmethod
58
65
  def create_client(cls, mq_config: dict, queue_name: str, **kwargs):
59
- """创建并返回新的RabbitMQClient实例"""
60
- # 从kwargs中提取是否自动创建队列的参数,默认True
61
- create_if_not_exists = kwargs.get('create_if_not_exists', True)
66
+ """创建并返回新的RabbitMQClient实例,遵循队列创建规则"""
67
+ # 获取当前项目名
68
+ app_name = kwargs.get('app_name', cls.config.get(
69
+ "APP_NAME", "")) if cls.config else kwargs.get('app_name', "")
62
70
 
63
71
  # 确保只在需要时处理一次队列名称
64
72
  processed_queue_name = queue_name
65
73
 
66
- # 如果是监听器且需要自动创建队列,确保队列名拼接app-name(只拼接一次)
67
- if create_if_not_exists and processed_queue_name and 'app_name' in kwargs and kwargs['app_name']:
68
- app_name = kwargs['app_name']
69
- # 只在队列名尚未包含app-name时进行拼接
74
+ # 通过上下文判断是否为发送器
75
+ # 发送器场景:当没有监听器时
76
+ is_sender = not cls._has_listeners
77
+
78
+ # 核心逻辑:根据组件存在状态决定是否允许创建队列
79
+ # 1. 只有发送器:不允许创建队列
80
+ # 2. 只有监听器:允许创建队列
81
+ # 3. 两者都存在:允许创建队列(由监听器负责)
82
+ create_if_not_exists = cls._has_listeners # 只要有监听器就允许创建
83
+
84
+ # 当需要创建队列且是监听器时,拼接项目名
85
+ if create_if_not_exists and not is_sender and processed_queue_name and app_name:
70
86
  if not processed_queue_name.endswith(f".{app_name}"):
71
87
  processed_queue_name = f"{processed_queue_name}.{app_name}"
72
88
  logging.debug(f"监听器队列名称自动拼接app-name: {processed_queue_name}")
73
89
 
90
+ logging.debug(
91
+ f"队列创建权限 - 监听器存在: {cls._has_listeners}, 发送器存在: {cls._has_senders}, "
92
+ f"是否发送器: {is_sender}, 允许创建: {create_if_not_exists}, 队列: {processed_queue_name}"
93
+ )
94
+
74
95
  return RabbitMQClient(
75
96
  host=mq_config.get('host', ""),
76
97
  port=mq_config.get('port', 0),
@@ -80,7 +101,7 @@ class RabbitMQService:
80
101
  exchange_name=mq_config.get(
81
102
  'exchange_name', "system.topic.exchange"),
82
103
  exchange_type=kwargs.get('exchange_type', "topic"),
83
- queue_name=processed_queue_name, # 使用处理后的队列名
104
+ queue_name=processed_queue_name,
84
105
  routing_key=kwargs.get(
85
106
  'routing_key', f"{processed_queue_name.split('.')[0]}.#" if processed_queue_name else "#"),
86
107
  durable=kwargs.get('durable', True),
@@ -89,7 +110,7 @@ class RabbitMQService:
89
110
  create_if_not_exists=create_if_not_exists,
90
111
  connection_timeout=kwargs.get('connection_timeout', 10),
91
112
  rpc_timeout=kwargs.get('rpc_timeout', 5),
92
- app_name=kwargs.get('app_name', "")
113
+ app_name=app_name
93
114
  )
94
115
 
95
116
  @classmethod
@@ -105,52 +126,80 @@ class RabbitMQService:
105
126
  async with cls._init_lock[client_name]:
106
127
  if client_name in cls.clients:
107
128
  client = cls.clients[client_name]
129
+ # 移除is_sender判断,通过上下文推断
130
+ is_sender = not cls._has_listeners or (
131
+ not kwargs.get('create_if_not_exists', True))
132
+
108
133
  if client.is_connected:
109
- logging.debug(f"客户端 '{client_name}' 已存在且连接有效,直接返回")
134
+ if not is_sender and not client.queue:
135
+ logging.debug(f"客户端 '{client_name}' 存在但队列未初始化,重新连接")
136
+ client.create_if_not_exists = True
137
+ await client.connect(force_reconnect=True, declare_queue=True)
138
+ else:
139
+ logging.debug(f"客户端 '{client_name}' 已存在且连接有效,直接返回")
110
140
  return client
111
141
  else:
112
142
  logging.debug(f"客户端 '{client_name}' 存在但连接已关闭,重新连接")
113
- await client.connect()
143
+ if not is_sender:
144
+ client.create_if_not_exists = True
145
+ await client.connect(declare_queue=not is_sender)
114
146
  return client
115
147
 
116
148
  initial_queue_name = kwargs.pop('queue_name', '')
117
- create_if_not_exists = kwargs.get('create_if_not_exists', True)
149
+ # 移除is_sender参数,通过上下文推断
150
+ is_sender = not cls._has_listeners or (
151
+ not kwargs.get('create_if_not_exists', True))
152
+
153
+ # 发送器特殊处理
154
+ if is_sender:
155
+ kwargs['create_if_not_exists'] = False
118
156
 
119
- # 检查队列是否已初始化
120
- if initial_queue_name in cls._initialized_queues:
121
- logging.debug(f"队列 '{initial_queue_name}' 已初始化过,直接创建客户端")
122
157
  client = RabbitMQService.create_client(
123
158
  mq_config,
124
159
  initial_queue_name,
125
160
  app_name=cls.config.get("APP_NAME", ""),
126
- **kwargs
161
+ **kwargs # 不再传递is_sender参数
127
162
  )
128
- await client.connect()
163
+
164
+ await client.connect(declare_queue=False)
129
165
  cls.clients[client_name] = client
130
166
  return client
131
167
 
132
- # 处理create_if_not_exists参数,避免重复传递
133
- create_if_not_exists = kwargs.pop('create_if_not_exists', True)
168
+ # 监听器逻辑
169
+ kwargs['create_if_not_exists'] = True
134
170
 
135
- # 对于监听器,确保create_if_not_exists为True
136
- # 判断是否为监听器:客户端名称与队列名称相同通常是监听器
137
- is_listener = client_name == initial_queue_name and initial_queue_name
138
- if is_listener:
139
- create_if_not_exists = True
140
- logging.debug(f"监听器 '{client_name}' 将自动创建队列(如果不存在)")
171
+ if initial_queue_name in cls._initialized_queues:
172
+ logging.debug(f"队列 '{initial_queue_name}' 已初始化过,直接创建客户端")
173
+ client = RabbitMQService.create_client(
174
+ mq_config,
175
+ initial_queue_name,
176
+ # 不再传递is_sender参数
177
+ app_name=cls.config.get("APP_NAME", ""), ** kwargs
178
+ )
179
+ await client.connect(declare_queue=True)
180
+ cls.clients[client_name] = client
181
+ return client
141
182
 
142
- # 创建客户端(这里会处理队列名称,添加app-name)
143
183
  client = RabbitMQService.create_client(
144
184
  mq_config,
145
185
  initial_queue_name,
146
186
  app_name=cls.config.get("APP_NAME", ""),
147
- create_if_not_exists=create_if_not_exists,
148
- connection_timeout=mq_config.get('connection_timeout', 15),
149
- rpc_timeout=mq_config.get('rpc_timeout', 5), ** kwargs
187
+ **kwargs # 不再传递is_sender参数
150
188
  )
151
189
 
152
- # 连接时不再处理队列名称,避免重复添加app-name
153
- await client.connect()
190
+ client.create_if_not_exists = True
191
+ logging.debug(
192
+ f"监听器客户端创建 - create_if_not_exists={client.create_if_not_exists}")
193
+
194
+ await client.connect(declare_queue=True)
195
+
196
+ if not client.queue:
197
+ logging.error(f"队列 '{initial_queue_name}' 创建失败,尝试重新创建")
198
+ client.create_if_not_exists = True
199
+ await client.connect(force_reconnect=True, declare_queue=True)
200
+ if not client.queue:
201
+ raise Exception(f"无法创建队列 '{initial_queue_name}'")
202
+
154
203
  final_queue_name = client.queue_name
155
204
 
156
205
  if final_queue_name not in cls._initialized_queues:
@@ -163,8 +212,11 @@ class RabbitMQService:
163
212
  return client
164
213
 
165
214
  @classmethod
166
- async def setup_senders(cls, senders: List[RabbitMQSendConfig]):
167
- """设置MQ发送客户端 - 保持不自动创建队列的行为"""
215
+ async def setup_senders(cls, senders: List[RabbitMQSendConfig], has_listeners: bool = False):
216
+ """设置MQ发送客户端"""
217
+ cls._has_listeners = has_listeners
218
+ cls._has_senders = True # 明确标记存在发送器
219
+
168
220
  async def setup_sender_tasks():
169
221
  for idx, sender_config in enumerate(senders):
170
222
  try:
@@ -173,20 +225,23 @@ class RabbitMQService:
173
225
 
174
226
  normalized_name = sender_config.queue_name
175
227
  app_name = cls.config.get("APP_NAME", "")
228
+
176
229
  if app_name and normalized_name.endswith(f".{app_name}"):
177
230
  normalized_name = normalized_name[:-
178
231
  len(f".{app_name}")]
232
+ logging.debug(
233
+ f"发送器队列名称移除app-name后缀: {normalized_name}")
179
234
 
180
235
  if normalized_name in cls.sender_client_names:
181
- logging.debug(f"发送客户端 '{normalized_name}' 已存在,跳过重复配置")
236
+ logging.debug(f"发送客户端 '{normalized_name}' 已存在,跳过")
182
237
  continue
183
238
 
184
239
  if normalized_name in cls.clients:
185
240
  client = cls.clients[normalized_name]
186
241
  if not client.is_connected:
187
- await client.connect()
242
+ await client.connect(declare_queue=False)
188
243
  else:
189
- # 发送端明确设置不自动创建队列
244
+ # 移除is_sender参数传递
190
245
  client = await cls.setup_rabbitmq(
191
246
  cls.config,
192
247
  client_name=normalized_name,
@@ -195,12 +250,12 @@ class RabbitMQService:
195
250
  auto_delete=sender_config.auto_delete,
196
251
  auto_parse_json=sender_config.auto_parse_json,
197
252
  queue_name=sender_config.queue_name,
198
- create_if_not_exists=False # 发送端不自动创建队列
253
+ create_if_not_exists=False # 仅通过此参数控制
199
254
  )
200
255
 
201
256
  if normalized_name not in cls.clients:
202
257
  cls.clients[normalized_name] = client
203
- logging.info(f"客户端 '{normalized_name}' 已添加到客户端列表")
258
+ logging.info(f"发送客户端 '{normalized_name}' 已添加")
204
259
 
205
260
  if normalized_name not in cls.sender_client_names:
206
261
  cls.sender_client_names.append(normalized_name)
@@ -217,8 +272,11 @@ class RabbitMQService:
217
272
  raise
218
273
 
219
274
  @classmethod
220
- async def setup_listeners(cls, listeners: List[RabbitMQListenerConfig]):
275
+ async def setup_listeners(cls, listeners: List[RabbitMQListenerConfig], has_senders: bool = False):
221
276
  """设置MQ监听器 - 确保自动创建队列"""
277
+ # 存在监听器,设置标志
278
+ cls._has_listeners = True
279
+
222
280
  for listener_config in listeners:
223
281
  # 将监听器配置转换为字典并添加到监听器
224
282
  # 强制设置create_if_not_exists为True
@@ -337,19 +395,17 @@ class RabbitMQService:
337
395
 
338
396
  @classmethod
339
397
  def get_sender(cls, client_name: Optional[str] = None) -> Optional[RabbitMQClient]:
340
- """获取发送客户端(仅返回已注册的客户端,移除模糊匹配)"""
398
+ """获取发送客户端(仅返回已注册的客户端)"""
341
399
  if not client_name:
342
400
  logging.warning("发送器名称不能为空")
343
401
  return None
344
402
 
345
- # 仅精确匹配已注册的客户端,不做模糊匹配
403
+ # 仅精确匹配已注册的客户端
346
404
  if client_name in cls.clients:
347
405
  return cls.clients[client_name]
348
406
 
349
- # 不允许通过拼接app-name反向查找(避免绕过注册)
350
407
  app_name = cls.config.get("APP_NAME", "") if cls.config else ""
351
408
  if app_name and not client_name.endswith(f".{app_name}"):
352
- # 不尝试拼接app-name查找,确保只有注册的名称可被找到
353
409
  return None
354
410
 
355
411
  logging.debug(f"发送器 '{client_name}' 不在已注册客户端列表中")
@@ -359,7 +415,7 @@ class RabbitMQService:
359
415
  async def send_message(
360
416
  cls,
361
417
  data: Union[BaseModel, str, Dict[str, Any], None],
362
- queue_name: Optional[str] = None, ** kwargs
418
+ queue_name: Optional[str] = None, **kwargs
363
419
  ) -> None:
364
420
  """发送消息到RabbitMQ"""
365
421
  sender = cls.get_sender(queue_name)
@@ -472,5 +528,6 @@ class RabbitMQService:
472
528
  cls.clients.clear()
473
529
  cls.sender_client_names.clear()
474
530
  cls._init_lock.clear()
531
+ cls._has_listeners = False # 重置标志
475
532
 
476
533
  logging.info("RabbitMQ服务已完全关闭")
@@ -0,0 +1,208 @@
1
+ from typing import Any, Callable, Dict, List, Tuple, Union, Optional, AsyncGenerator
2
+ import asyncio
3
+ import logging
4
+ from contextlib import asynccontextmanager
5
+ from fastapi import FastAPI
6
+ from pydantic import BaseModel
7
+ from sycommon.config.Config import SingletonMeta
8
+ from sycommon.models.mqlistener_config import RabbitMQListenerConfig
9
+ from sycommon.models.mqsend_config import RabbitMQSendConfig
10
+ from sycommon.rabbitmq.rabbitmq_service import RabbitMQService
11
+
12
+
13
+ class Services(metaclass=SingletonMeta):
14
+ _loop: Optional[asyncio.AbstractEventLoop] = None
15
+ _config: Optional[dict] = None
16
+ _initialized: bool = False
17
+ _registered_senders: List[str] = []
18
+ _mq_tasks: List[asyncio.Task] = []
19
+ _instance: Optional['Services'] = None
20
+ _app: Optional[FastAPI] = None
21
+
22
+ def __init__(self, config: dict, app: FastAPI):
23
+ if not Services._config:
24
+ Services._config = config
25
+ Services._instance = self
26
+ Services._app = app
27
+ self._init_event_loop()
28
+
29
+ def _init_event_loop(self):
30
+ """初始化事件循环,确保全局只有一个循环实例"""
31
+ if not Services._loop:
32
+ try:
33
+ Services._loop = asyncio.get_running_loop()
34
+ except RuntimeError:
35
+ Services._loop = asyncio.new_event_loop()
36
+ asyncio.set_event_loop(Services._loop)
37
+
38
+ @classmethod
39
+ def plugins(
40
+ cls,
41
+ app: FastAPI,
42
+ config: dict,
43
+ middleware: Optional[Callable[[FastAPI, dict], None]] = None,
44
+ nacos_service: Optional[Callable[[dict], None]] = None,
45
+ logging_service: Optional[Callable[[dict], None]] = None,
46
+ database_service: Optional[Union[
47
+ Tuple[Callable[[dict, str], None], str],
48
+ List[Tuple[Callable[[dict, str], None], str]]
49
+ ]] = None,
50
+ rabbitmq_listeners: Optional[List[RabbitMQListenerConfig]] = None,
51
+ rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None
52
+ ) -> FastAPI:
53
+ # 保存应用实例和配置
54
+ cls._app = app
55
+ cls._config = config
56
+
57
+ # 立即配置非异步服务(在应用启动前)
58
+ if middleware:
59
+ middleware(app, config)
60
+
61
+ if nacos_service:
62
+ nacos_service(config)
63
+
64
+ if logging_service:
65
+ logging_service(config)
66
+
67
+ if database_service:
68
+ cls._setup_database_static(database_service, config)
69
+
70
+ # 创建生命周期管理器
71
+ @asynccontextmanager
72
+ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
73
+ # 启动阶段 - 执行初始化
74
+ instance = cls(config, app)
75
+
76
+ # 检查是否存在监听器
77
+ has_listeners = bool(
78
+ rabbitmq_listeners and len(rabbitmq_listeners) > 0)
79
+ # 检查是否存在发送器
80
+ has_senders = bool(
81
+ rabbitmq_senders and len(rabbitmq_senders) > 0)
82
+
83
+ # 执行MQ异步初始化,传递是否有监听器的标志
84
+ try:
85
+ await instance._setup_mq_async(
86
+ rabbitmq_listeners=rabbitmq_listeners,
87
+ rabbitmq_senders=rabbitmq_senders,
88
+ has_listeners=has_listeners,
89
+ has_senders=has_senders
90
+ )
91
+ cls._initialized = True
92
+ logging.info("所有服务初始化完成")
93
+ except Exception as e:
94
+ logging.error(f"服务初始化失败: {str(e)}", exc_info=True)
95
+ raise
96
+
97
+ # 将实例挂载到app上
98
+ app.state.services = instance
99
+
100
+ yield # 应用运行阶段
101
+
102
+ # 关闭阶段 - 清理资源
103
+ await cls.shutdown()
104
+ logging.info("所有服务已关闭")
105
+
106
+ # 设置生命周期
107
+ app.router.lifespan_context = lifespan
108
+
109
+ return app
110
+
111
+ @staticmethod
112
+ def _setup_database_static(database_service, config):
113
+ """静态方法:设置数据库服务"""
114
+ if isinstance(database_service, tuple):
115
+ db_setup, db_name = database_service
116
+ db_setup(config, db_name)
117
+ elif isinstance(database_service, list):
118
+ for db_setup, db_name in database_service:
119
+ db_setup(config, db_name)
120
+
121
+ async def _setup_mq_async(
122
+ self,
123
+ rabbitmq_listeners: Optional[List[RabbitMQListenerConfig]] = None,
124
+ rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None,
125
+ has_listeners: bool = False,
126
+ has_senders: bool = False,
127
+ ):
128
+ """异步设置MQ相关服务,保存发送器和监听器存在状态"""
129
+ # 保存状态到类变量
130
+ Services._has_listeners = has_listeners
131
+ Services._has_senders = has_senders
132
+
133
+ # 初始化RabbitMQ服务,传递状态
134
+ RabbitMQService.init(self._config, has_listeners, has_senders)
135
+
136
+ # 设置发送器,传递是否有监听器的标志
137
+ if rabbitmq_senders:
138
+ await self._setup_senders_async(rabbitmq_senders, has_listeners)
139
+
140
+ # 设置监听器,传递是否有发送器的标志
141
+ if rabbitmq_listeners:
142
+ await self._setup_listeners_async(rabbitmq_listeners, has_senders)
143
+
144
+ async def _setup_senders_async(self, rabbitmq_senders, has_listeners: bool):
145
+ Services._registered_senders = [
146
+ sender.queue_name for sender in rabbitmq_senders]
147
+
148
+ # 将是否有监听器的信息传递给RabbitMQService
149
+ await RabbitMQService.setup_senders(rabbitmq_senders, has_listeners)
150
+ logging.info(f"已注册的RabbitMQ发送器: {Services._registered_senders}")
151
+
152
+ async def _setup_listeners_async(self, rabbitmq_listeners, has_senders: bool):
153
+ await RabbitMQService.setup_listeners(rabbitmq_listeners, has_senders)
154
+
155
+ @classmethod
156
+ async def send_message(
157
+ cls,
158
+ queue_name: str,
159
+ data: Union[str, Dict[str, Any], BaseModel, None],
160
+ max_retries: int = 3,
161
+ retry_delay: float = 1.0, ** kwargs
162
+ ) -> None:
163
+ """发送消息,添加重试机制"""
164
+ if not cls._initialized or not cls._loop:
165
+ logging.error("Services not properly initialized!")
166
+ raise ValueError("服务未正确初始化")
167
+
168
+ for attempt in range(max_retries):
169
+ try:
170
+ if queue_name not in cls._registered_senders:
171
+ cls._registered_senders = RabbitMQService.sender_client_names
172
+ if queue_name not in cls._registered_senders:
173
+ raise ValueError(f"发送器 {queue_name} 未注册")
174
+
175
+ sender = RabbitMQService.get_sender(queue_name)
176
+ if not sender:
177
+ raise ValueError(f"发送器 '{queue_name}' 不存在")
178
+
179
+ await RabbitMQService.send_message(data, queue_name, **kwargs)
180
+ logging.info(f"消息发送成功(尝试 {attempt+1}/{max_retries})")
181
+ return
182
+
183
+ except Exception as e:
184
+ if attempt == max_retries - 1:
185
+ logging.error(
186
+ f"消息发送失败(已尝试 {max_retries} 次): {str(e)}", exc_info=True)
187
+ raise
188
+
189
+ logging.warning(
190
+ f"消息发送失败(尝试 {attempt+1}/{max_retries}): {str(e)},"
191
+ f"{retry_delay}秒后重试..."
192
+ )
193
+ await asyncio.sleep(retry_delay)
194
+
195
+ @staticmethod
196
+ async def shutdown():
197
+ """关闭所有服务"""
198
+ # 取消所有MQ任务
199
+ for task in Services._mq_tasks:
200
+ if not task.done():
201
+ task.cancel()
202
+ try:
203
+ await task
204
+ except asyncio.CancelledError:
205
+ pass
206
+
207
+ # 关闭RabbitMQ服务
208
+ await RabbitMQService.shutdown()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.8
3
+ Version: 0.1.9
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -1,217 +0,0 @@
1
- from typing import Any, Callable, Dict, List, Tuple, Union, Optional, Awaitable
2
- import asyncio
3
- import logging
4
- from contextlib import asynccontextmanager
5
- from dotenv import load_dotenv
6
- from fastapi import FastAPI
7
- from pydantic import BaseModel
8
- import yaml
9
- from sycommon.config.Config import SingletonMeta
10
- from sycommon.models.mqlistener_config import RabbitMQListenerConfig
11
- from sycommon.models.mqsend_config import RabbitMQSendConfig
12
- from sycommon.rabbitmq.rabbitmq_service import RabbitMQService
13
-
14
-
15
- class Services(metaclass=SingletonMeta):
16
- _loop: Optional[asyncio.AbstractEventLoop] = None
17
- _config: Optional[dict] = None
18
- _initialized: bool = False
19
- _registered_senders: List[str] = []
20
- _mq_tasks: List[asyncio.Task] = []
21
- _pending_setup: Optional[Callable[..., Awaitable[None]]] = None
22
- _instance: Optional['Services'] = None
23
-
24
- def __init__(self, config):
25
- if not Services._config:
26
- Services._config = config
27
- Services._instance = self
28
-
29
- @classmethod
30
- def get_lifespan(cls, config):
31
- """返回 FastAPI 的 lifespan 管理器"""
32
- cls._config = config
33
-
34
- @asynccontextmanager
35
- async def lifespan(app):
36
- # 应用启动时初始化
37
- cls._loop = asyncio.get_running_loop()
38
- cls._initialized = True
39
- logging.info("Services initialized with FastAPI event loop")
40
-
41
- # 执行之前缓存的MQ设置
42
- if cls._pending_setup:
43
- try:
44
- await cls._pending_setup()
45
- except Exception as e:
46
- logging.error(f"执行MQ初始化失败: {str(e)}", exc_info=True)
47
- finally:
48
- cls._pending_setup = None
49
-
50
- try:
51
- yield
52
- finally:
53
- # 应用关闭时清理
54
- await cls.shutdown()
55
- logging.info("Services shutdown completed")
56
-
57
- return lifespan
58
-
59
- @classmethod
60
- def plugins(cls,
61
- middleware: Optional[Tuple[Callable, FastAPI]] = None,
62
- nacos_service: Optional[Callable] = None,
63
- logging_service: Optional[Callable] = None,
64
- database_service: Optional[Union[Tuple[Callable, str],
65
- List[Tuple[Callable, str]]]] = None,
66
- rabbitmq_listeners: Optional[List[RabbitMQListenerConfig]] = None,
67
- rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None
68
- ) -> None:
69
- """
70
- 类方法:注册各种服务插件
71
- 确保单例实例存在后调用实例方法的register_plugins
72
- """
73
- # 确保实例已创建
74
- if not cls._instance:
75
- if not cls._config:
76
- raise ValueError("Services尚未初始化,请先提供配置")
77
- cls._instance = Services(cls._config)
78
-
79
- cls._instance.register_plugins(
80
- middleware=middleware,
81
- nacos_service=nacos_service,
82
- logging_service=logging_service,
83
- database_service=database_service,
84
- rabbitmq_listeners=rabbitmq_listeners,
85
- rabbitmq_senders=rabbitmq_senders
86
- )
87
-
88
- def register_plugins(
89
- self,
90
- middleware: Optional[Tuple[Callable, FastAPI]] = None,
91
- nacos_service: Optional[Callable] = None,
92
- logging_service: Optional[Callable] = None,
93
- database_service: Optional[Union[Tuple[Callable,
94
- str], List[Tuple[Callable, str]]]] = None,
95
- rabbitmq_listeners: Optional[List[RabbitMQListenerConfig]] = None,
96
- rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None
97
- ) -> None:
98
- """实例方法:实际执行各种服务插件的注册逻辑"""
99
- # 注册非异步服务
100
- if middleware:
101
- self._setup_middleware(middleware)
102
- if nacos_service:
103
- nacos_service(self._config)
104
- if logging_service:
105
- logging_service(self._config)
106
- if database_service:
107
- self._setup_database(database_service)
108
-
109
- RabbitMQService.init(self._config)
110
-
111
- # MQ设置异步函数
112
- async def setup_mq_components():
113
- if rabbitmq_senders:
114
- self._setup_senders(rabbitmq_senders)
115
- if rabbitmq_listeners:
116
- self._setup_listeners(rabbitmq_listeners)
117
-
118
- # 存储到_pending_setup,等待lifespan异步执行
119
- Services._pending_setup = setup_mq_components
120
-
121
- def _setup_database(self, database_service):
122
- if isinstance(database_service, tuple):
123
- db_setup, db_name = database_service
124
- db_setup(self._config, db_name)
125
- elif isinstance(database_service, list):
126
- for db_setup, db_name in database_service:
127
- db_setup(self._config, db_name)
128
-
129
- def _setup_middleware(self, middleware):
130
- if isinstance(middleware, tuple):
131
- middleware_setup, app = middleware
132
- middleware_setup(app, self._config)
133
-
134
- def _setup_senders(self, rabbitmq_senders):
135
- Services._registered_senders = [
136
- sender.queue_name for sender in rabbitmq_senders]
137
- if self._loop: # 确保loop存在
138
- task = self._loop.create_task(
139
- self._setup_and_wait(
140
- RabbitMQService.setup_senders, rabbitmq_senders)
141
- )
142
- self._mq_tasks.append(task)
143
- logging.info(f"已注册的RabbitMQ发送器: {Services._registered_senders}")
144
-
145
- def _setup_listeners(self, rabbitmq_listeners):
146
- if self._loop: # 确保loop存在
147
- task = self._loop.create_task(
148
- self._setup_and_wait(
149
- RabbitMQService.setup_listeners, rabbitmq_listeners)
150
- )
151
- self._mq_tasks.append(task)
152
-
153
- async def _setup_and_wait(self, setup_func, *args, **kwargs):
154
- try:
155
- await setup_func(*args, **kwargs)
156
- except Exception as e:
157
- logging.error(
158
- f"Error in {setup_func.__name__}: {str(e)}", exc_info=True)
159
-
160
- @classmethod
161
- async def send_message(
162
- cls,
163
- queue_name: str,
164
- data: Union[str, Dict[str, Any], BaseModel, None],
165
- max_retries: int = 3, # 最大重试次数
166
- retry_delay: float = 1.0, # 重试间隔(秒)
167
- **kwargs
168
- ) -> None:
169
- """发送消息,添加重试机制处理发送器不存在的情况"""
170
- if not cls._initialized or not cls._loop:
171
- logging.error("Services not properly initialized!")
172
- raise ValueError("服务未正确初始化")
173
-
174
- # 重试逻辑
175
- for attempt in range(max_retries):
176
- try:
177
- # 检查发送器是否已注册
178
- if queue_name not in cls._registered_senders:
179
- # 可能是初始化尚未完成,尝试刷新注册列表
180
- cls._registered_senders = RabbitMQService.sender_client_names
181
- if queue_name not in cls._registered_senders:
182
- raise ValueError(f"发送器 {queue_name} 未注册")
183
-
184
- # 获取发送器
185
- sender = RabbitMQService.get_sender(queue_name)
186
- if not sender:
187
- raise ValueError(f"发送器 '{queue_name}' 不存在")
188
-
189
- # 发送消息
190
- await RabbitMQService.send_message(data, queue_name, ** kwargs)
191
- logging.info(f"消息发送成功(尝试 {attempt+1}/{max_retries})")
192
- return # 成功发送,退出函数
193
-
194
- except Exception as e:
195
- # 最后一次尝试失败则抛出异常
196
- if attempt == max_retries - 1:
197
- logging.error(
198
- f"消息发送失败(已尝试 {max_retries} 次): {str(e)}", exc_info=True)
199
- raise
200
-
201
- # 非最后一次尝试,记录警告并等待重试
202
- logging.warning(
203
- f"消息发送失败(尝试 {attempt+1}/{max_retries}): {str(e)},"
204
- f"{retry_delay}秒后重试..."
205
- )
206
- await asyncio.sleep(retry_delay)
207
-
208
- @staticmethod
209
- async def shutdown():
210
- for task in Services._mq_tasks:
211
- if not task.done():
212
- task.cancel()
213
- try:
214
- await task
215
- except asyncio.CancelledError:
216
- pass
217
- await RabbitMQService.shutdown()