internal 1.1.10__py3-none-any.whl → 1.1.12__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.
Potentially problematic release.
This version of internal might be problematic. Click here for more details.
internal/base_factory.py
CHANGED
|
@@ -19,7 +19,7 @@ from .const import LOG_FMT, LOG_FMT_NO_DT, LOG_DT_FMT, DEFAULT_LOGGER_NAME, CORR
|
|
|
19
19
|
from .exception.base_exception import InternalBaseException
|
|
20
20
|
from .exception.internal_exception import BadGatewayException, GatewayTimeoutException
|
|
21
21
|
from .ext.amazon import aws
|
|
22
|
-
from .http.requests import
|
|
22
|
+
from .http.requests import send_webhook_message
|
|
23
23
|
from .http.responses import async_response
|
|
24
24
|
from .middleware.log_request import LogRequestMiddleware
|
|
25
25
|
from .utils import update_dict_with_cast
|
|
@@ -157,19 +157,11 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
157
157
|
if isinstance(exc, BadGatewayException):
|
|
158
158
|
if self.get_app_config().WEBHOOK_BASE_URL:
|
|
159
159
|
message = f"【{self.DEFAULT_APP_NAME}】Bad gateway, request:{request.__dict__}, exc:{exc}"
|
|
160
|
-
|
|
161
|
-
try:
|
|
162
|
-
await async_request(app, "POST", self.get_app_config().WEBHOOK_BASE_URL, json=payload)
|
|
163
|
-
except Exception as e:
|
|
164
|
-
app.state.logger.warn(f"Notify failure, Exception:{e}")
|
|
160
|
+
await send_webhook_message(app, message)
|
|
165
161
|
elif isinstance(exc, GatewayTimeoutException):
|
|
166
162
|
if self.get_app_config().WEBHOOK_BASE_URL:
|
|
167
163
|
message = f"【{self.DEFAULT_APP_NAME}】Gateway timeout, request:{request.__dict__}, exc:{exc}"
|
|
168
|
-
|
|
169
|
-
try:
|
|
170
|
-
await async_request(app, "POST", self.get_app_config().WEBHOOK_BASE_URL, json=payload)
|
|
171
|
-
except Exception as e:
|
|
172
|
-
app.state.logger.warn(f"Notify failure, Exception:{e}")
|
|
164
|
+
await send_webhook_message(app, message)
|
|
173
165
|
|
|
174
166
|
return await async_response(data=detail.get("data"), code=detail.get("code"), message=detail.get("message"),
|
|
175
167
|
status_code=exc.status_code)
|
|
@@ -187,13 +179,8 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
187
179
|
async def http_exception_handler(request: Request, exc: Exception):
|
|
188
180
|
app.state.logger.warn(f"Exception, request:{request.__dict__}, exc:{exc}")
|
|
189
181
|
app.state.logger.warn(traceback.format_exc())
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
payload = {"text": message}
|
|
193
|
-
try:
|
|
194
|
-
await async_request(app, "POST", self.get_app_config().WEBHOOK_BASE_URL, json=payload)
|
|
195
|
-
except Exception as e:
|
|
196
|
-
app.state.logger.warn(f"Notify failure, Exception:{e}")
|
|
182
|
+
message = f"【{self.DEFAULT_APP_NAME}】Unprocessed Exception, request:{request.__dict__}, exc:{exc}"
|
|
183
|
+
await send_webhook_message(app, message)
|
|
197
184
|
|
|
198
185
|
return await async_response(code="error_internal_server", message="Internal server error",
|
|
199
186
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
internal/http/requests.py
CHANGED
|
@@ -24,11 +24,13 @@ async def invoke_request(timeout: httpx.Timeout, method: str, url: str, app: Fas
|
|
|
24
24
|
)
|
|
25
25
|
return response
|
|
26
26
|
except httpx.TimeoutException as exc:
|
|
27
|
-
|
|
27
|
+
message = f"invoke_request(), TimeoutException, exc: {exc}, url: {url}, method: {method}, kwargs: {kwargs}"
|
|
28
|
+
app.state.logger.warn(message)
|
|
28
29
|
raise GatewayTimeoutException(str(exc)) from exc
|
|
29
30
|
except Exception as exc:
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
message = f"invoke_request(), Exception, exc: {exc}, url: {url}, method: {method}, kwargs: {kwargs}"
|
|
32
|
+
app.state.logger.warn(message)
|
|
33
|
+
await send_webhook_message(app, message)
|
|
32
34
|
raise BadGatewayException(str(exc))
|
|
33
35
|
|
|
34
36
|
|
|
@@ -76,7 +78,7 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
|
76
78
|
current_delay = float(request_retry_delay)
|
|
77
79
|
|
|
78
80
|
while retries <= request_retry_count:
|
|
79
|
-
app.state.logger.warn(f"嘗試送請求 (第 {retries + 1} 次嘗試)...")
|
|
81
|
+
app.state.logger.warn(f"url: {url}, method: {method}, kwargs: {kwargs}, 嘗試送請求 (第 {retries + 1} 次嘗試)...")
|
|
80
82
|
try:
|
|
81
83
|
# 使用 await 關鍵字等待異步請求完成
|
|
82
84
|
response = await invoke_request(timeout, method, url, app, **kwargs)
|
|
@@ -85,12 +87,13 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
|
85
87
|
if retries < request_retry_count:
|
|
86
88
|
# 計算下一次的延遲時間:current_delay * 2^retries + 隨機抖動
|
|
87
89
|
sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
|
|
88
|
-
app.state.logger.warn(f"等待 {sleep_time:.2f} 秒後重試...")
|
|
90
|
+
app.state.logger.warn(f"url: {url}, method: {method}, kwargs: {kwargs}, 等待 {sleep_time:.2f} 秒後重試...")
|
|
89
91
|
# 使用 asyncio.sleep 進行異步等待,不會阻塞主執行緒
|
|
90
92
|
await asyncio.sleep(sleep_time)
|
|
91
93
|
retries += 1
|
|
92
94
|
else:
|
|
93
|
-
|
|
95
|
+
message = f"url: {url}, method: {method}, kwargs: {kwargs}, 已達到最大重試次數 ({request_retry_count}),放棄發送請求。"
|
|
96
|
+
app.state.logger.warn(message)
|
|
94
97
|
raise # 重新拋出最後一個異常
|
|
95
98
|
|
|
96
99
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
internal/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
internal/base_config.py,sha256=gbzgQklOzdQUY63b5k15dgk1sxVyYR4OcWXP0RHVNTI,2280
|
|
3
|
-
internal/base_factory.py,sha256=
|
|
3
|
+
internal/base_factory.py,sha256=PUhPehpdB8wAUUm0QhbfzzIMeIL87ko8peIromoumnQ,10575
|
|
4
4
|
internal/cache_redis.py,sha256=YMsrUXHd-wKjsjsboD79Y-ciBBowzT6aAjdJZ36-yEY,697
|
|
5
5
|
internal/common_enum/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
internal/common_enum/contact_type.py,sha256=7QkTQ71UxpaT1YHI40FpjmLz3r-UbRU-sd0m5ajH1as,142
|
|
@@ -23,7 +23,7 @@ internal/ext/amazon/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSu
|
|
|
23
23
|
internal/ext/amazon/aws/__init__.py,sha256=2YFjb-rHG1JaZGZiZffYDesgTAJjDshOqQbswOYzhP8,834
|
|
24
24
|
internal/ext/amazon/aws/const.py,sha256=l4WMg5bKWujwOKABBkCO2zclNg3abnYOfbhD7DG8GsA,109
|
|
25
25
|
internal/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
|
-
internal/http/requests.py,sha256=
|
|
26
|
+
internal/http/requests.py,sha256=JEblqezuQH2jMODUM9vEd0O8yfzOBV_O-htcVJkQt4I,7907
|
|
27
27
|
internal/http/responses.py,sha256=zvU0iRQ9-qxeEZfKmuvTi8lv9DFcaNAsHlQKOTCpVzw,2945
|
|
28
28
|
internal/interface/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
29
29
|
internal/interface/base_interface.py,sha256=3YaVjIgLi_pZpLk5SEIk8WVkuICM8qPavT8rB0MdB5U,1536
|
|
@@ -34,6 +34,6 @@ internal/model/base_model.py,sha256=hxleV8fYNvFgUoYmCv_inEP3kA4tD4HhCBCNFVK8SZg,
|
|
|
34
34
|
internal/model/operate.py,sha256=QSM6yXYXpJMwrqkUGEWZLrEBaUgqHwVHY_Fi4S42hKc,3190
|
|
35
35
|
internal/utils.py,sha256=i6YZdiXsiWnOjRdlJ6afNCGpyMe3Uo9mhm3xlQBy3Ls,2824
|
|
36
36
|
internal/validator_utils.py,sha256=CqjaVFoAu5MqvBG_AkTP-r7AliWawtUWB851USj4moI,1519
|
|
37
|
-
internal-1.1.
|
|
38
|
-
internal-1.1.
|
|
39
|
-
internal-1.1.
|
|
37
|
+
internal-1.1.12.dist-info/METADATA,sha256=F0VpGummXYYHUr1L5Hdc2TlI75Grti0PmKSMS7luE18,939
|
|
38
|
+
internal-1.1.12.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
|
|
39
|
+
internal-1.1.12.dist-info/RECORD,,
|
|
File without changes
|