-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcallback_handler.py
More file actions
266 lines (231 loc) · 8.75 KB
/
callback_handler.py
File metadata and controls
266 lines (231 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# callback_handler.py
import asyncio
import datetime
import logging
from enum import Enum
from typing import Dict, Optional, Callable, Union, List, Coroutine
import httpx
from pydantic import BaseModel
from config_manager import config_manager
# 配置常量
MAX_RETRIES = 3
RETRY_DELAY = 1.0
TIMEOUT = 10.0
class EventType(Enum):
"""回调事件类型枚举"""
LOGIN_REDIRECT = "login_redirect"
PAGE_NAVIGATED = "page_navigated"
PAGE_LOADED = "page_loaded"
STATUS_UPDATE = "status_update"
REALTIME_MESSAGE = "realtime_message"
LOGIN_CONFIRM = "login_confirm"
CLOSE_PAGE = "close_page"
NEW_MESSAGE = "new_message"
MOVE_CONVERSATION = "move_conversation"
LOGOUT = "logout"
ERROR = "error"
class CallbackConfig(BaseModel):
"""回调配置模型"""
endpoint: List[str] = []
timeout: float = TIMEOUT
retries: int = MAX_RETRIES
headers: Dict[str, str] = {"Content-Type": "application/json"}
def __init__(self):
super().__init__()
try:
self.endpoint = config_manager.get_config("callback.endpoint", [])
self.timeout = config_manager.get_config("callback.timeout", TIMEOUT)
self.retries = config_manager.get_config("callback.max_retries", MAX_RETRIES)
except KeyError as e:
logging.error(f"Configuration key not found: {e}")
# 使用默认值继续运行
except ValueError as e:
logging.error(f"Invalid configuration value: {e}")
# 使用默认值继续运行
except Exception as e:
logging.error(f"Error loading config: {e}")
raise e
class CallbackRequest(BaseModel):
"""回调请求数据模型"""
event: EventType
workstation_id: str
data: Dict
timestamp: datetime.datetime
retry_count: int = 0
class CallbackHandler:
"""异步回调处理器"""
def __init__(
self,
config: CallbackConfig,
logger: Optional[logging.Logger] = None
):
self._processing_task = None
self.config = config
self.logger = logger or logging.getLogger(__name__)
self._queue = asyncio.Queue()
self._active_tasks: Dict[str, asyncio.Task] = {}
self._custom_handlers: Dict[EventType, List[Callable]] = {}
self._running = True
def add_callback_endpoint(self, new_endpoint: str):
"""
动态设置回调地址
:param new_endpoint: 新的回调地址
"""
self.config.endpoint.append(new_endpoint)
config_manager.set_config("callback.endpoint", self.config.endpoint)
self.logger.info(f"Callback endpoint updated to: {new_endpoint}")
return self.config.endpoint
def delete_callback_endpoint(self, endpoint: str):
"""
删除回调地址
:param endpoint: 要删除的回调地址
"""
if endpoint in self.config.endpoint:
self.config.endpoint.remove(endpoint)
config_manager.set_config("callback.endpoint", self.config.endpoint)
self.logger.info(f"Callback endpoint deleted: {endpoint}")
return True
else:
self.logger.warning(f"Callback endpoint not found: {endpoint}")
return False
async def start(self):
"""启动后台处理任务"""
self._processing_task = asyncio.create_task(self._process_queue())
self.logger.info("Callback handler started")
async def stop(self):
"""停止处理器并等待任务完成"""
self._running = False
await self._queue.join()
self._processing_task.cancel()
self.logger.info("Callback handler stopped")
def register_handler(
self,
event_type: EventType,
handler: Callable[[CallbackRequest], Union[None, Coroutine]]
):
"""注册自定义事件处理器"""
if event_type not in self._custom_handlers:
self._custom_handlers[event_type] = []
self._custom_handlers[event_type].append(handler)
self.logger.debug(f"Registered custom handler for {event_type}")
async def trigger(
self,
event: EventType,
workstation_id: str,
data: Dict,
immediate: bool = False
):
"""
触发回调事件
:param data:
:param immediate: 是否立即发送(跳过队列)
"""
request = CallbackRequest(
event=event,
workstation_id=workstation_id,
data=data,
timestamp=datetime.datetime.now()
)
# 先执行本地注册的处理器
await self._run_local_handlers(request)
# HTTP回调加入队列处理
if immediate:
await self._send_request(request)
else:
await self._queue.put(request)
self.logger.debug(f"Queued callback: {event.value} for {workstation_id}")
async def _run_local_handlers(self, request: CallbackRequest):
"""执行本地注册的事件处理器"""
if handlers := self._custom_handlers.get(request.event):
for handler in handlers:
try:
if asyncio.iscoroutinefunction(handler):
await handler(request)
else:
handler(request)
except Exception as e:
self.logger.error(
f"Local handler failed for {request.event}: {str(e)}",
exc_info=True
)
async def _process_queue(self):
"""处理队列中的回调请求"""
while self._running:
request = await self._queue.get()
task_id = f"{request.workstation_id}-{request.event.value}-{id(request)}"
try:
self._active_tasks[task_id] = asyncio.create_task(
self._send_with_retry(request)
)
await self._active_tasks[task_id]
except Exception as e:
self.logger.error(f"Failed processing task {task_id}: {str(e)}")
finally:
self._queue.task_done()
await self._active_tasks.pop(task_id, None)
async def _send_with_retry(self, request: CallbackRequest):
"""带重试机制的发送逻辑"""
for attempt in range(self.config.retries + 1):
try:
return await self._send_request(request)
except (httpx.RequestError, httpx.HTTPStatusError) as e:
if attempt == self.config.retries:
error_msg = f"Callback failed after {attempt} retries: {str(e)}"
await self._handle_failure(request, error_msg)
break
delay = RETRY_DELAY * (2 ** attempt)
self.logger.warning(
f"Retrying {request.event} in {delay}s (attempt {attempt + 1})"
)
await asyncio.sleep(delay)
request.retry_count += 1
async def _send_request(self, request: CallbackRequest):
"""实际发送HTTP请求"""
async with httpx.AsyncClient() as client:
response = None
for endpoint in self.config.endpoint:
response = await client.post(
endpoint,
content=request.json(),
headers=self.config.headers,
timeout=self.config.timeout
)
response.raise_for_status()
self.logger.info(
f"Callback succeeded: {request.event.value} "
f"to {self.config.endpoint} (status {response.status_code})"
)
return response
async def _handle_failure(self, request: CallbackRequest, error_msg: str):
"""失败处理"""
self.logger.error(error_msg)
# 触发错误回调
error_data = {
"original_event": request.dict(),
"error": error_msg
}
await self.trigger(
EventType.ERROR,
request.workstation_id,
error_data,
immediate=True
)
class CallbackMetrics:
"""回调监控指标"""
def __init__(self):
self.total_events = 0
self.success_count = 0
self.failure_count = 0
self.retry_count = 0
def increment(self, success: bool):
self.total_events += 1
if success:
self.success_count += 1
else:
self.failure_count += 1
def get_stats(self) -> Dict:
return {
"total": self.total_events,
"success_rate": self.success_count / self.total_events if self.total_events else 0,
"avg_retries": self.retry_count / self.total_events if self.total_events else 0
}