-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror_handler.py
More file actions
85 lines (63 loc) · 2.34 KB
/
error_handler.py
File metadata and controls
85 lines (63 loc) · 2.34 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
import asyncio
import copy
import socketio # type: ignore
from socketio.exceptions import ConnectionError # type: ignore
from typing import Any, Callable, Dict, List, Optional
from polyapi.config import get_api_key_and_url
# all active webhook handlers, used by unregister_all to cleanup
active_handlers: List[Dict[str, Any]] = []
# global client shared by all error handlers, will be initialized by webhook.start
client = None
def prepare():
loop = asyncio.get_event_loop()
loop.run_until_complete(get_client_and_connect())
print("Client initialized!")
async def get_client_and_connect():
_, base_url = get_api_key_and_url()
global client
client = socketio.AsyncClient()
await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
async def unregister(data: Dict[str, Any]):
print(f"Stopping error handler for {data['path']}...")
assert client
await client.emit(
"unregisterErrorHandler",
data,
"/events",
)
async def unregister_all():
_, base_url = get_api_key_and_url()
# need to reconnect because maybe socketio client disconnected after Ctrl+C?
try:
await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
except ConnectionError:
pass
await asyncio.gather(*[unregister(handler) for handler in active_handlers])
async def on(
path: str, callback: Callable, options: Optional[Dict[str, Any]] = None
) -> None:
print(f"Starting error handler for {path}...")
if not client:
raise Exception("Client not initialized. Abort!")
api_key, _ = get_api_key_and_url()
handler_id = None
data = copy.deepcopy(options or {})
data["path"] = path
data["apiKey"] = api_key
def registerCallback(id: int):
nonlocal handler_id
handler_id = id
client.on(f"handleError:{handler_id}", callback, namespace="/events")
active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
await client.emit("registerErrorHandler", data, "/events", registerCallback)
def start(*args):
loop = asyncio.get_event_loop()
loop.run_until_complete(get_client_and_connect())
asyncio.gather(*args)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.run_until_complete(unregister_all())
loop.stop()