internal 1.1.11__py3-none-any.whl → 1.1.13__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_config.py +7 -7
- internal/base_factory.py +7 -22
- internal/http/requests.py +21 -21
- {internal-1.1.11.dist-info → internal-1.1.13.dist-info}/METADATA +1 -1
- {internal-1.1.11.dist-info → internal-1.1.13.dist-info}/RECORD +6 -6
- {internal-1.1.11.dist-info → internal-1.1.13.dist-info}/WHEEL +0 -0
internal/base_config.py
CHANGED
|
@@ -13,12 +13,12 @@ class BaseConfig(BaseSettings):
|
|
|
13
13
|
# Request
|
|
14
14
|
REQUEST_VERIFY_SSL: bool = False
|
|
15
15
|
REQUEST_PROXY: str = ''
|
|
16
|
-
REQUEST_RETRY_COUNT: int =
|
|
17
|
-
REQUEST_RETRY_DELAY_INITIAL_SECONDS:
|
|
18
|
-
REQUEST_CONN_POOL_TIMEOUT: float = 5
|
|
19
|
-
REQUEST_CONN_TIMEOUT: float = 5
|
|
20
|
-
REQUEST_WRITE_TIMEOUT: float = 5
|
|
21
|
-
RESPONSE_READ_TIMEOUT: float = 5
|
|
16
|
+
REQUEST_RETRY_COUNT: int = 2
|
|
17
|
+
REQUEST_RETRY_DELAY_INITIAL_SECONDS: float = 1.0
|
|
18
|
+
REQUEST_CONN_POOL_TIMEOUT: float = 5.0
|
|
19
|
+
REQUEST_CONN_TIMEOUT: float = 5.0
|
|
20
|
+
REQUEST_WRITE_TIMEOUT: float = 5.0
|
|
21
|
+
RESPONSE_READ_TIMEOUT: float = 5.0
|
|
22
22
|
|
|
23
23
|
# AWS
|
|
24
24
|
AWS_ACCESS_KEY_ID: str = ""
|
|
@@ -54,7 +54,7 @@ class BaseConfig(BaseSettings):
|
|
|
54
54
|
# Exception Notify
|
|
55
55
|
WEBHOOK_BASE_URL: str = ""
|
|
56
56
|
WEBHOOK_RETRY_COUNT: int = 5
|
|
57
|
-
WEBHOOK_RETRY_DELAY_INITIAL_SECONDS:
|
|
57
|
+
WEBHOOK_RETRY_DELAY_INITIAL_SECONDS: float = 1.0
|
|
58
58
|
|
|
59
59
|
# Default System Account Password
|
|
60
60
|
SYSTEM_ACCOUNT: str = "cruisys"
|
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
|
|
@@ -155,21 +155,11 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
155
155
|
detail = exc.detail
|
|
156
156
|
|
|
157
157
|
if isinstance(exc, BadGatewayException):
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
payload = {"text": message}
|
|
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}")
|
|
158
|
+
message = f"【{self.DEFAULT_APP_NAME}】Bad gateway, request:{request.__dict__}, exc:{exc}"
|
|
159
|
+
await send_webhook_message(app, message)
|
|
165
160
|
elif isinstance(exc, GatewayTimeoutException):
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
payload = {"text": message}
|
|
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}")
|
|
161
|
+
message = f"【{self.DEFAULT_APP_NAME}】Gateway timeout, request:{request.__dict__}, exc:{exc}"
|
|
162
|
+
await send_webhook_message(app, message)
|
|
173
163
|
|
|
174
164
|
return await async_response(data=detail.get("data"), code=detail.get("code"), message=detail.get("message"),
|
|
175
165
|
status_code=exc.status_code)
|
|
@@ -187,13 +177,8 @@ class BaseFactory(metaclass=ABCMeta):
|
|
|
187
177
|
async def http_exception_handler(request: Request, exc: Exception):
|
|
188
178
|
app.state.logger.warn(f"Exception, request:{request.__dict__}, exc:{exc}")
|
|
189
179
|
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}")
|
|
180
|
+
message = f"【{self.DEFAULT_APP_NAME}】Unprocessed Exception, request:{request.__dict__}, exc:{exc}"
|
|
181
|
+
await send_webhook_message(app, message)
|
|
197
182
|
|
|
198
183
|
return await async_response(code="error_internal_server", message="Internal server error",
|
|
199
184
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
internal/http/requests.py
CHANGED
|
@@ -17,40 +17,39 @@ async def invoke_request(timeout: httpx.Timeout, method: str, url: str, app: Fas
|
|
|
17
17
|
if "json" in kwargs:
|
|
18
18
|
kwargs["json"] = jsonable_encoder(kwargs["json"])
|
|
19
19
|
|
|
20
|
-
app.state.logger.info(f"
|
|
20
|
+
app.state.logger.info(f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request() request")
|
|
21
21
|
response = await client.request(method, url, **kwargs)
|
|
22
22
|
app.state.logger.info(
|
|
23
|
-
f"
|
|
23
|
+
f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request() response \n\nstatus_code: {response.status_code} \ntext: {response.text}"
|
|
24
24
|
)
|
|
25
25
|
return response
|
|
26
26
|
except httpx.TimeoutException as exc:
|
|
27
|
-
message = f"
|
|
27
|
+
message = f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request(), TimeoutException, exc: {exc}"
|
|
28
28
|
app.state.logger.warn(message)
|
|
29
29
|
raise GatewayTimeoutException(str(exc)) from exc
|
|
30
30
|
except Exception as exc:
|
|
31
|
-
message = f"
|
|
31
|
+
message = f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \ninvoke_request(), Exception, exc: {exc}"
|
|
32
32
|
app.state.logger.warn(message)
|
|
33
|
-
await send_webhook_message(app, message)
|
|
34
33
|
raise BadGatewayException(str(exc))
|
|
35
34
|
|
|
36
35
|
|
|
37
36
|
async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
38
|
-
request_conn_pool_timeout: float = 0, request_conn_timeout: float = 0,
|
|
39
|
-
request_write_timeout: float = 0, response_read_timeout: float = 0,
|
|
40
|
-
request_retry_count: int =
|
|
37
|
+
request_conn_pool_timeout: float = -1.0, request_conn_timeout: float = -1.0,
|
|
38
|
+
request_write_timeout: float = -1.0, response_read_timeout: float = -1.0,
|
|
39
|
+
request_retry_count: int = -1, request_retry_delay_initial_seconds: float = -1.0,
|
|
41
40
|
**kwargs):
|
|
42
|
-
if request_conn_pool_timeout
|
|
41
|
+
if request_conn_pool_timeout < 0:
|
|
43
42
|
request_conn_pool_timeout = app.state.config.REQUEST_CONN_POOL_TIMEOUT
|
|
44
|
-
if request_conn_timeout
|
|
43
|
+
if request_conn_timeout < 0:
|
|
45
44
|
request_conn_timeout = app.state.config.REQUEST_CONN_TIMEOUT
|
|
46
|
-
if request_write_timeout
|
|
45
|
+
if request_write_timeout < 0:
|
|
47
46
|
request_write_timeout = app.state.config.REQUEST_WRITE_TIMEOUT
|
|
48
|
-
if response_read_timeout
|
|
47
|
+
if response_read_timeout < 0:
|
|
49
48
|
response_read_timeout = app.state.config.RESPONSE_READ_TIMEOUT
|
|
50
|
-
if request_retry_count
|
|
49
|
+
if request_retry_count < 0:
|
|
51
50
|
request_retry_count = app.state.config.REQUEST_RETRY_COUNT
|
|
52
|
-
if
|
|
53
|
-
|
|
51
|
+
if request_retry_delay_initial_seconds < 0:
|
|
52
|
+
request_retry_delay_initial_seconds = app.state.config.REQUEST_RETRY_DELAY_INITIAL_SECONDS
|
|
54
53
|
|
|
55
54
|
timeout = httpx.Timeout(connect=request_conn_timeout, read=response_read_timeout,
|
|
56
55
|
write=request_write_timeout, pool=request_conn_pool_timeout)
|
|
@@ -75,10 +74,10 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
|
75
74
|
return response
|
|
76
75
|
else:
|
|
77
76
|
retries = 0
|
|
78
|
-
current_delay =
|
|
77
|
+
current_delay = request_retry_delay_initial_seconds
|
|
79
78
|
|
|
80
79
|
while retries <= request_retry_count:
|
|
81
|
-
app.state.logger.warn(f"
|
|
80
|
+
app.state.logger.warn(f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \n嘗試送請求 (第 {retries + 1} 次嘗試)...")
|
|
82
81
|
try:
|
|
83
82
|
# 使用 await 關鍵字等待異步請求完成
|
|
84
83
|
response = await invoke_request(timeout, method, url, app, **kwargs)
|
|
@@ -87,19 +86,20 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
|
87
86
|
if retries < request_retry_count:
|
|
88
87
|
# 計算下一次的延遲時間:current_delay * 2^retries + 隨機抖動
|
|
89
88
|
sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
|
|
90
|
-
app.state.logger.warn(f"
|
|
89
|
+
app.state.logger.warn(f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \n等待 {sleep_time:.2f} 秒後重試...")
|
|
91
90
|
# 使用 asyncio.sleep 進行異步等待,不會阻塞主執行緒
|
|
92
91
|
await asyncio.sleep(sleep_time)
|
|
93
92
|
retries += 1
|
|
94
93
|
else:
|
|
95
|
-
message = f"
|
|
94
|
+
message = f"【{app.title}】 \nURL: {method} {url} \nkwargs: {kwargs} \n已達到最大重試次數 ({request_retry_count}),放棄發送請求。"
|
|
96
95
|
app.state.logger.warn(message)
|
|
97
96
|
raise # 重新拋出最後一個異常
|
|
98
97
|
|
|
99
98
|
|
|
100
99
|
async def invoke_webhook_message_api(app: FastAPI, message: str):
|
|
101
100
|
payload = {"text": message}
|
|
102
|
-
response = await async_request(app, "POST", app.state.config.WEBHOOK_BASE_URL,
|
|
101
|
+
response = await async_request(app, "POST", app.state.config.WEBHOOK_BASE_URL, request_retry_count=0,
|
|
102
|
+
request_retry_delay_initial_seconds=0.0, json=payload)
|
|
103
103
|
response.raise_for_status()
|
|
104
104
|
return response
|
|
105
105
|
|
|
@@ -118,7 +118,7 @@ async def send_webhook_message(app: FastAPI, message: str):
|
|
|
118
118
|
app.state.logger.warn(f"Notify failure, Exception:{e}")
|
|
119
119
|
else:
|
|
120
120
|
retries = 0
|
|
121
|
-
current_delay =
|
|
121
|
+
current_delay = app.state.config.WEBHOOK_RETRY_DELAY_INITIAL_SECONDS
|
|
122
122
|
|
|
123
123
|
while retries <= retry_count:
|
|
124
124
|
app.state.logger.warn(f"嘗試發送訊息 (第 {retries + 1} 次嘗試)...")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
internal/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
internal/base_config.py,sha256=
|
|
3
|
-
internal/base_factory.py,sha256=
|
|
2
|
+
internal/base_config.py,sha256=MxCX2PKRgFZnL_IH2ukLwdoBJzQBHeV86GxxNxILlsc,2296
|
|
3
|
+
internal/base_factory.py,sha256=ZzXow5iKXyiZqFy815Jy-S4_spNCoyLPVz637deie7w,10441
|
|
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=SD1jClXQ1rQjSY143cL-vd8bC6yHcue7REDetWU3Sso,8106
|
|
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.13.dist-info/METADATA,sha256=VPqzG5UnLdEAxu2d8NYv2wQ8TDlEhiV_zzYHY84peQA,939
|
|
38
|
+
internal-1.1.13.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
|
|
39
|
+
internal-1.1.13.dist-info/RECORD,,
|
|
File without changes
|