hello-datap-component-base 0.2.1__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/oss_client.py +6 -6
- hello_datap_component_base/runner.py +3 -1
- {hello_datap_component_base-0.2.1.dist-info → hello_datap_component_base-0.2.3.dist-info}/METADATA +1 -1
- hello_datap_component_base-0.2.3.dist-info/RECORD +14 -0
- {hello_datap_component_base-0.2.1.dist-info → hello_datap_component_base-0.2.3.dist-info}/WHEEL +1 -1
- hello_datap_component_base-0.2.1.dist-info/RECORD +0 -14
- {hello_datap_component_base-0.2.1.dist-info → hello_datap_component_base-0.2.3.dist-info}/entry_points.txt +0 -0
- {hello_datap_component_base-0.2.1.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
|
|
@@ -29,10 +29,10 @@ class OSSClient:
|
|
|
29
29
|
import oss2
|
|
30
30
|
|
|
31
31
|
# 从环境变量获取 OSS 配置
|
|
32
|
-
endpoint = os.environ.get('
|
|
33
|
-
access_key_id = os.environ.get('
|
|
34
|
-
access_key_secret = os.environ.get('
|
|
35
|
-
bucket_name = os.environ.get('
|
|
32
|
+
endpoint = os.environ.get('OSS_ENDPOINT_FOR_LOG')
|
|
33
|
+
access_key_id = os.environ.get('OSS_ACCESS_KEY_ID_FOR_LOG')
|
|
34
|
+
access_key_secret = os.environ.get('OSS_ACCESS_KEY_SECRET_FOR_LOG')
|
|
35
|
+
bucket_name = os.environ.get('OSS_BUCKET_NAME_FOR_LOG')
|
|
36
36
|
|
|
37
37
|
# 如果 OSS_ENDPOINT 不存在,静默跳过(向下兼容)
|
|
38
38
|
if not endpoint:
|
|
@@ -43,7 +43,7 @@ class OSSClient:
|
|
|
43
43
|
if not all([access_key_id, access_key_secret, bucket_name]):
|
|
44
44
|
logger.warning(
|
|
45
45
|
"OSS 配置不完整,无法上传日志文件。"
|
|
46
|
-
"需要设置环境变量:
|
|
46
|
+
"需要设置环境变量: OSS_ACCESS_KEY_ID_FOR_LOG, OSS_ACCESS_KEY_SECRET_FOR_LOG, OSS_BUCKET_NAME_FOR_LOG"
|
|
47
47
|
)
|
|
48
48
|
self._initialized = True
|
|
49
49
|
return
|
|
@@ -139,7 +139,7 @@ def get_oss_client() -> Optional[OSSClient]:
|
|
|
139
139
|
OSSClient 实例,如果配置不完整则返回 None
|
|
140
140
|
"""
|
|
141
141
|
# 如果环境变量中没有 OSS_ENDPOINT 配置,则不使用 OSS,直接返回 None(向下兼容)
|
|
142
|
-
if not os.environ.get('
|
|
142
|
+
if not os.environ.get('OSS_ENDPOINT_FOR_LOG'):
|
|
143
143
|
return None
|
|
144
144
|
|
|
145
145
|
client = OSSClient()
|
|
@@ -124,7 +124,9 @@ class ServiceRunner:
|
|
|
124
124
|
print(f"Starting service: {self.config.name}")
|
|
125
125
|
if self.config.version:
|
|
126
126
|
print(f"Version: {self.config.version}")
|
|
127
|
-
|
|
127
|
+
import json
|
|
128
|
+
params_to_print = self.config.params if self.config.params is not None else {}
|
|
129
|
+
print(f"Param: {json.dumps(params_to_print, indent=2, ensure_ascii=False)}")
|
|
128
130
|
print("\n" + "=" * 60)
|
|
129
131
|
print("Processing request...")
|
|
130
132
|
print("=" * 60 + "\n")
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
hello_datap_component_base/__init__.py,sha256=5bGlAUivYRN9_H91I9tYoUrokl6YJJgJGtlM4PkLU_A,811
|
|
2
|
+
hello_datap_component_base/base.py,sha256=jkjoUx9QQ4IqiwR0WZrb2-ZX9KEKF1_fOF_415qwsxc,6436
|
|
3
|
+
hello_datap_component_base/cli.py,sha256=chRlcOTaqHJFdKNPe0OTFYvw_M4c_1_kHBlQ82BwB4g,8161
|
|
4
|
+
hello_datap_component_base/config.py,sha256=XV4OY0iCEjVf0PNxLRdWLgOB5pPB0OvASdkysZXukms,6992
|
|
5
|
+
hello_datap_component_base/discover.py,sha256=70sFO9iVnpsjo_eTViQsStI-n0N_eJNvDRvLvm_dqZQ,7459
|
|
6
|
+
hello_datap_component_base/logger.py,sha256=JIvy_gctDf0Vpe_itSQCwf-ZhVigMdDFwpwbmMlcNNE,10606
|
|
7
|
+
hello_datap_component_base/mns_client.py,sha256=uaWb4P99iRk8vpREqEDqjD9HqaKwlhOYEtHfaW_Y2Pg,12521
|
|
8
|
+
hello_datap_component_base/oss_client.py,sha256=C4Dajeey3JBKh_EgGJzzNMMdpzkm85Z6tpO1kC1wU_s,5057
|
|
9
|
+
hello_datap_component_base/runner.py,sha256=korZY_Qoa-ZzwIFsWK4zaetLF17BE9oT-IRDueoaSbs,11576
|
|
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,,
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
hello_datap_component_base/__init__.py,sha256=uEkEyRNAWoC3Lh6TkCvA1hCaQjMghjuJ_nOsGnNVlqU,811
|
|
2
|
-
hello_datap_component_base/base.py,sha256=jkjoUx9QQ4IqiwR0WZrb2-ZX9KEKF1_fOF_415qwsxc,6436
|
|
3
|
-
hello_datap_component_base/cli.py,sha256=FVHMk599_L8u_SMPTxGAiyHUkimD-Wbt6upeMA9229s,8161
|
|
4
|
-
hello_datap_component_base/config.py,sha256=XV4OY0iCEjVf0PNxLRdWLgOB5pPB0OvASdkysZXukms,6992
|
|
5
|
-
hello_datap_component_base/discover.py,sha256=70sFO9iVnpsjo_eTViQsStI-n0N_eJNvDRvLvm_dqZQ,7459
|
|
6
|
-
hello_datap_component_base/logger.py,sha256=JIvy_gctDf0Vpe_itSQCwf-ZhVigMdDFwpwbmMlcNNE,10606
|
|
7
|
-
hello_datap_component_base/mns_client.py,sha256=wEPlU23_O8x1lUkz51knEUHjPzzMnLsmQEVVO_Un9gI,10152
|
|
8
|
-
hello_datap_component_base/oss_client.py,sha256=9DD26Mu9TxX9z0mhLgDKQxqx8VI1yuxUNlYAcLDgKnE,4993
|
|
9
|
-
hello_datap_component_base/runner.py,sha256=eccCY78v69hEWu0fJ4Z9mTWKauG8t5o_gWagafn0qMo,11450
|
|
10
|
-
hello_datap_component_base-0.2.1.dist-info/METADATA,sha256=ruYGekCeTnMcVbpQz8k4mcRtaNeiQnBoJACQsn4r_QY,19865
|
|
11
|
-
hello_datap_component_base-0.2.1.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
|
|
12
|
-
hello_datap_component_base-0.2.1.dist-info/entry_points.txt,sha256=Q2YteaAVN0UW9MEBfPZ3EY6FI6dRaoCmQZpcvAzmSVQ,74
|
|
13
|
-
hello_datap_component_base-0.2.1.dist-info/top_level.txt,sha256=V4aKCkt0lrJbO4RBPymJ6dz5mXo6vjYy02kusdWKGqM,27
|
|
14
|
-
hello_datap_component_base-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|