hello-datap-component-base 0.2.2__py3-none-any.whl → 0.2.3__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.
- hello_datap_component_base/__init__.py +1 -1
- hello_datap_component_base/cli.py +1 -1
- hello_datap_component_base/mns_client.py +52 -1
- {hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/METADATA +1 -1
- {hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/RECORD +8 -8
- {hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/WHEEL +0 -0
- {hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/entry_points.txt +0 -0
- {hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/top_level.txt +0 -0
|
@@ -9,6 +9,11 @@ import logging
|
|
|
9
9
|
|
|
10
10
|
logger = logging.getLogger(__name__)
|
|
11
11
|
|
|
12
|
+
# MNS 消息体最大长度(64KB),预留一些空间给 JSON 结构
|
|
13
|
+
MNS_MAX_MESSAGE_SIZE = 64 * 1024
|
|
14
|
+
# 错误消息最大长度(预留空间给其他字段)
|
|
15
|
+
MAX_ERROR_MESSAGE_LENGTH = 8 * 1024 # 8KB
|
|
16
|
+
|
|
12
17
|
|
|
13
18
|
class MNSClient:
|
|
14
19
|
"""阿里云 MNS 消息队列客户端"""
|
|
@@ -145,6 +150,49 @@ class MNSClient:
|
|
|
145
150
|
self._queue.send_message(msg)
|
|
146
151
|
return True
|
|
147
152
|
|
|
153
|
+
def _truncate_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
|
|
154
|
+
"""
|
|
155
|
+
截断消息中过长的字段,确保消息体不超过 MNS 限制
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
message: 原始消息字典
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
截断后的消息字典
|
|
162
|
+
"""
|
|
163
|
+
# 深拷贝,避免修改原始数据
|
|
164
|
+
import copy
|
|
165
|
+
truncated = copy.deepcopy(message)
|
|
166
|
+
|
|
167
|
+
# 截断 message 字段(错误信息)
|
|
168
|
+
if 'message' in truncated and isinstance(truncated['message'], str):
|
|
169
|
+
original_len = len(truncated['message'])
|
|
170
|
+
if original_len > MAX_ERROR_MESSAGE_LENGTH:
|
|
171
|
+
truncated['message'] = (
|
|
172
|
+
truncated['message'][:MAX_ERROR_MESSAGE_LENGTH] +
|
|
173
|
+
f"\n... [truncated, original length: {original_len}]"
|
|
174
|
+
)
|
|
175
|
+
logger.warning(
|
|
176
|
+
f"消息的 message 字段过长({original_len} 字符),已截断至 {MAX_ERROR_MESSAGE_LENGTH} 字符"
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# 检查整体消息大小,如果仍然过大,进一步截断 out_put
|
|
180
|
+
message_body = json.dumps(truncated, ensure_ascii=False)
|
|
181
|
+
if len(message_body.encode('utf-8')) > MNS_MAX_MESSAGE_SIZE:
|
|
182
|
+
if 'data' in truncated and isinstance(truncated['data'], dict):
|
|
183
|
+
if 'out_put' in truncated['data'] and truncated['data']['out_put'] is not None:
|
|
184
|
+
out_put_str = json.dumps(truncated['data']['out_put'], ensure_ascii=False)
|
|
185
|
+
if len(out_put_str) > MAX_ERROR_MESSAGE_LENGTH:
|
|
186
|
+
truncated['data']['out_put'] = {
|
|
187
|
+
'_truncated': True,
|
|
188
|
+
'_message': f'Output too large ({len(out_put_str)} chars), truncated'
|
|
189
|
+
}
|
|
190
|
+
logger.warning(
|
|
191
|
+
f"消息的 out_put 字段过大({len(out_put_str)} 字符),已截断"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
return truncated
|
|
195
|
+
|
|
148
196
|
def send_message(self, message: Dict[str, Any]) -> bool:
|
|
149
197
|
"""
|
|
150
198
|
发送消息到队列(带重试逻辑)
|
|
@@ -161,8 +209,11 @@ class MNSClient:
|
|
|
161
209
|
logger.warning("MNS 队列未初始化,跳过消息发送")
|
|
162
210
|
return False
|
|
163
211
|
|
|
212
|
+
# 截断过长的消息字段
|
|
213
|
+
truncated_message = self._truncate_message(message)
|
|
214
|
+
|
|
164
215
|
# 将消息转换为 JSON 字符串
|
|
165
|
-
message_body = json.dumps(
|
|
216
|
+
message_body = json.dumps(truncated_message, ensure_ascii=False)
|
|
166
217
|
|
|
167
218
|
# 重试逻辑
|
|
168
219
|
last_exception = None
|
{hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/RECORD
RENAMED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
hello_datap_component_base/__init__.py,sha256=
|
|
1
|
+
hello_datap_component_base/__init__.py,sha256=5bGlAUivYRN9_H91I9tYoUrokl6YJJgJGtlM4PkLU_A,811
|
|
2
2
|
hello_datap_component_base/base.py,sha256=jkjoUx9QQ4IqiwR0WZrb2-ZX9KEKF1_fOF_415qwsxc,6436
|
|
3
|
-
hello_datap_component_base/cli.py,sha256=
|
|
3
|
+
hello_datap_component_base/cli.py,sha256=chRlcOTaqHJFdKNPe0OTFYvw_M4c_1_kHBlQ82BwB4g,8161
|
|
4
4
|
hello_datap_component_base/config.py,sha256=XV4OY0iCEjVf0PNxLRdWLgOB5pPB0OvASdkysZXukms,6992
|
|
5
5
|
hello_datap_component_base/discover.py,sha256=70sFO9iVnpsjo_eTViQsStI-n0N_eJNvDRvLvm_dqZQ,7459
|
|
6
6
|
hello_datap_component_base/logger.py,sha256=JIvy_gctDf0Vpe_itSQCwf-ZhVigMdDFwpwbmMlcNNE,10606
|
|
7
|
-
hello_datap_component_base/mns_client.py,sha256=
|
|
7
|
+
hello_datap_component_base/mns_client.py,sha256=uaWb4P99iRk8vpREqEDqjD9HqaKwlhOYEtHfaW_Y2Pg,12521
|
|
8
8
|
hello_datap_component_base/oss_client.py,sha256=C4Dajeey3JBKh_EgGJzzNMMdpzkm85Z6tpO1kC1wU_s,5057
|
|
9
9
|
hello_datap_component_base/runner.py,sha256=korZY_Qoa-ZzwIFsWK4zaetLF17BE9oT-IRDueoaSbs,11576
|
|
10
|
-
hello_datap_component_base-0.2.
|
|
11
|
-
hello_datap_component_base-0.2.
|
|
12
|
-
hello_datap_component_base-0.2.
|
|
13
|
-
hello_datap_component_base-0.2.
|
|
14
|
-
hello_datap_component_base-0.2.
|
|
10
|
+
hello_datap_component_base-0.2.3.dist-info/METADATA,sha256=fHLMMFXgwraItePBCFDAHvCUf4bnW8JHE85yAyeiobQ,19865
|
|
11
|
+
hello_datap_component_base-0.2.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
12
|
+
hello_datap_component_base-0.2.3.dist-info/entry_points.txt,sha256=Q2YteaAVN0UW9MEBfPZ3EY6FI6dRaoCmQZpcvAzmSVQ,74
|
|
13
|
+
hello_datap_component_base-0.2.3.dist-info/top_level.txt,sha256=V4aKCkt0lrJbO4RBPymJ6dz5mXo6vjYy02kusdWKGqM,27
|
|
14
|
+
hello_datap_component_base-0.2.3.dist-info/RECORD,,
|
{hello_datap_component_base-0.2.2.dist-info → hello_datap_component_base-0.2.3.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|