internal 1.1.5__tar.gz → 1.1.6__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 internal might be problematic. Click here for more details.

Files changed (39) hide show
  1. {internal-1.1.5 → internal-1.1.6}/PKG-INFO +1 -1
  2. {internal-1.1.5 → internal-1.1.6}/pyproject.toml +1 -1
  3. {internal-1.1.5 → internal-1.1.6}/src/internal/base_config.py +2 -0
  4. {internal-1.1.5 → internal-1.1.6}/src/internal/http/requests.py +59 -3
  5. {internal-1.1.5 → internal-1.1.6}/README.md +0 -0
  6. {internal-1.1.5 → internal-1.1.6}/src/internal/__init__.py +0 -0
  7. {internal-1.1.5 → internal-1.1.6}/src/internal/base_factory.py +0 -0
  8. {internal-1.1.5 → internal-1.1.6}/src/internal/cache_redis.py +0 -0
  9. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/__init__.py +0 -0
  10. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/contact_type.py +0 -0
  11. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/description_type.py +0 -0
  12. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/device_code.py +0 -0
  13. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/event_code.py +0 -0
  14. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/lpr_direction.py +0 -0
  15. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/notify_type.py +0 -0
  16. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/operator_type.py +0 -0
  17. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/order_type.py +0 -0
  18. {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/websocket_channel.py +0 -0
  19. {internal-1.1.5 → internal-1.1.6}/src/internal/const.py +0 -0
  20. {internal-1.1.5 → internal-1.1.6}/src/internal/database.py +0 -0
  21. {internal-1.1.5 → internal-1.1.6}/src/internal/exception/__init__.py +0 -0
  22. {internal-1.1.5 → internal-1.1.6}/src/internal/exception/app_exception.py +0 -0
  23. {internal-1.1.5 → internal-1.1.6}/src/internal/exception/base_exception.py +0 -0
  24. {internal-1.1.5 → internal-1.1.6}/src/internal/exception/internal_exception.py +0 -0
  25. {internal-1.1.5 → internal-1.1.6}/src/internal/ext/__init__.py +0 -0
  26. {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/__init__.py +0 -0
  27. {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/aws/__init__.py +0 -0
  28. {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/aws/const.py +0 -0
  29. {internal-1.1.5 → internal-1.1.6}/src/internal/http/__init__.py +0 -0
  30. {internal-1.1.5 → internal-1.1.6}/src/internal/http/responses.py +0 -0
  31. {internal-1.1.5 → internal-1.1.6}/src/internal/interface/__init__.py +0 -0
  32. {internal-1.1.5 → internal-1.1.6}/src/internal/interface/base_interface.py +0 -0
  33. {internal-1.1.5 → internal-1.1.6}/src/internal/middleware/__init__.py +0 -0
  34. {internal-1.1.5 → internal-1.1.6}/src/internal/middleware/log_request.py +0 -0
  35. {internal-1.1.5 → internal-1.1.6}/src/internal/model/__init__.py +0 -0
  36. {internal-1.1.5 → internal-1.1.6}/src/internal/model/base_model.py +0 -0
  37. {internal-1.1.5 → internal-1.1.6}/src/internal/model/operate.py +0 -0
  38. {internal-1.1.5 → internal-1.1.6}/src/internal/utils.py +0 -0
  39. {internal-1.1.5 → internal-1.1.6}/src/internal/validator_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: internal
3
- Version: 1.1.5
3
+ Version: 1.1.6
4
4
  Summary:
5
5
  Author: Ray
6
6
  Author-email: ray@cruisys.com
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "internal"
3
- version = "1.1.5"
3
+ version = "1.1.6"
4
4
  description = ""
5
5
  authors = [{ name = "Ray", email = "ray@cruisys.com" }]
6
6
  readme = "README.md"
@@ -52,6 +52,8 @@ class BaseConfig(BaseSettings):
52
52
 
53
53
  # Exception Notify
54
54
  WEBHOOK_BASE_URL: str = ""
55
+ WEBHOOK_RETRY_COUNT: int = 5
56
+ WEBHOOK_RETRY_DELAY_INITIAL_SECONDS: int = 1
55
57
 
56
58
  # Default System Account Password
57
59
  SYSTEM_ACCOUNT: str = "cruisys"
@@ -1,3 +1,6 @@
1
+ import asyncio
2
+ import random
3
+
1
4
  import httpx
2
5
 
3
6
  from fastapi import FastAPI
@@ -59,10 +62,63 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
59
62
  raise BadGatewayException(str(exc))
60
63
 
61
64
 
65
+ async def invoke_webhook_message_api(app: FastAPI, message: str):
66
+ payload = {"text": message}
67
+ response = await async_request(app, "POST", app.state.config.WEBHOOK_BASE_URL, json=payload)
68
+ response.raise_for_status()
69
+ return response
70
+
71
+
62
72
  async def send_webhook_message(app: FastAPI, message: str):
63
- if app.state.config.WEBHOOK_BASE_URL:
64
- payload = {"text": message}
73
+ if not app.state.config.WEBHOOK_BASE_URL:
74
+ app.state.logger.warn(f"Skip notify webhook url is null")
75
+ return None
76
+
77
+ retry_count = app.state.config.WEBHOOK_RETRY_COUNT
78
+ if retry_count <= 0:
65
79
  try:
66
- await async_request(app, "POST", app.state.config.WEBHOOK_BASE_URL, json=payload)
80
+ response = await invoke_webhook_message_api(app, message)
81
+ return response
67
82
  except Exception as e:
68
83
  app.state.logger.warn(f"Notify failure, Exception:{e}")
84
+ else:
85
+ retries = 0
86
+ current_delay = float(app.state.config.WEBHOOK_RETRY_DELAY_INITIAL_SECONDS)
87
+
88
+ while retries <= retry_count:
89
+ app.state.logger.warn(f"嘗試發送訊息 (第 {retries + 1} 次嘗試)...")
90
+ try:
91
+ # 使用 await 關鍵字等待異步請求完成
92
+ response = await invoke_webhook_message_api(app, message)
93
+ return response
94
+ except httpx.HTTPStatusError as e:
95
+ if e.response.status_code == 429:
96
+ app.state.logger.warn(f"收到 429 錯誤:{e.response.status_code} - {e.response.text}")
97
+ if retries < retry_count:
98
+ # 計算下一次的延遲時間:current_delay * 2^retries + 隨機抖動
99
+ sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
100
+ app.state.logger.warn(f"等待 {sleep_time:.2f} 秒後重試...")
101
+ # 使用 asyncio.sleep 進行異步等待,不會阻塞主執行緒
102
+ await asyncio.sleep(sleep_time)
103
+ retries += 1
104
+ else:
105
+ app.state.logger.warn(
106
+ f"已達到最大重試次數 ({app.state.config.WEBHOOK_RETRY_COUNT}),放棄發送訊息。")
107
+ raise # 重新拋出最後一個異常
108
+ else:
109
+ app.state.logger.warn(f"發生其他 HTTP 錯誤:{e.response.status_code} - {e.response.text}")
110
+ raise # 對於非 429 錯誤,直接拋出
111
+
112
+ except httpx.RequestError as e:
113
+ # 處理網絡錯誤,例如 DNS 查找失敗、連接超時等
114
+ app.state.logger.warn(f"發生網絡錯誤:{e}")
115
+ if retries < retry_count:
116
+ sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
117
+ app.state.logger.warn(f"等待 {sleep_time:.2f} 秒後重試...")
118
+ await asyncio.sleep(sleep_time)
119
+ retries += 1
120
+ else:
121
+ app.state.logger.warn(f"已達到最大重試次數 ({retry_count}),放棄發送訊息。")
122
+ raise
123
+
124
+ return None # 如果重試次數用盡仍未成功
File without changes
File without changes
File without changes