sycommon-python-lib 0.1.7__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.
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/PKG-INFO +1 -1
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/pyproject.toml +1 -1
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/traceid.py +1 -1
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_client.py +68 -40
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_service.py +103 -46
- sycommon_python_lib-0.1.9/src/sycommon/services.py +208 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/feign.py +76 -86
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/PKG-INFO +1 -1
- sycommon_python_lib-0.1.7/src/sycommon/services.py +0 -217
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/README.md +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/setup.cfg +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/Config.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/DatabaseConfig.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/EmbeddingConfig.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/LLMConfig.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/MQConfig.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/RerankerConfig.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/config/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/database/base_db_service.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/database/database_service.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/health/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/health/health_check.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/health/ping.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/logging/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/logging/kafka_log.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/logging/logger_wrapper.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/context.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/cors.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/exception.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/middleware.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/monitor_memory.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/mq.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/middleware/timeout.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/base_http.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/log.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqlistener_config.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqmsg_model.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/mqsend_config.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/models/sso_user.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/synacos/nacos_service.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/tools/__init__.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/tools/snowflake.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/tools/timing.py +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/SOURCES.txt +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/requires.txt +0 -0
- {sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
|
@@ -95,7 +95,7 @@ def setup_trace_id_handler(app):
|
|
|
95
95
|
if "application/json" in content_type and not content_disposition.startswith("attachment"):
|
|
96
96
|
try:
|
|
97
97
|
data = json.loads(response_body)
|
|
98
|
-
data["
|
|
98
|
+
data["traceId"] = trace_id
|
|
99
99
|
new_body = json.dumps(
|
|
100
100
|
data, ensure_ascii=False).encode()
|
|
101
101
|
response = Response(
|
{sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_client.py
RENAMED
|
@@ -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,
|
|
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"
|
|
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 <
|
|
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
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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(
|
|
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}/
|
|
333
|
+
f"连接失败({retries}/3): {str(e)}, create_if_not_exists={self.create_if_not_exists}, 重试中...")
|
|
319
334
|
|
|
320
|
-
if retries <
|
|
335
|
+
if retries < 3:
|
|
321
336
|
await asyncio.sleep(self.reconnection_delay)
|
|
322
337
|
|
|
323
|
-
logging.error(f"最终连接失败: {str(last_exception)}"
|
|
338
|
+
logging.error(f"最终连接失败: {str(last_exception)}")
|
|
324
339
|
raise Exception(
|
|
325
|
-
f"经过{
|
|
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
|
-
|
|
543
|
-
|
|
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
|
-
|
|
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("未设置消息处理函数")
|
{sycommon_python_lib-0.1.7 → sycommon_python_lib-0.1.9}/src/sycommon/rabbitmq/rabbitmq_service.py
RENAMED
|
@@ -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
|
-
#
|
|
61
|
-
|
|
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
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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=
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
163
|
+
|
|
164
|
+
await client.connect(declare_queue=False)
|
|
129
165
|
cls.clients[client_name] = client
|
|
130
166
|
return client
|
|
131
167
|
|
|
132
|
-
#
|
|
133
|
-
|
|
168
|
+
# 监听器逻辑
|
|
169
|
+
kwargs['create_if_not_exists'] = True
|
|
134
170
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
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
|
-
|
|
153
|
-
|
|
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"
|
|
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, **
|
|
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服务已完全关闭")
|