forked from fincubator/bitshares_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_practice_example.py
156 lines (111 loc) · 3.67 KB
/
best_practice_example.py
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
import asyncio
import signal
import random
import weakref
from typing import Any, Iterator
class BlockChainEx(Exception):
pass
class HTTPex(Exception):
pass
class CriticalEx(Exception):
pass
class DBConnector:
"""Example of EXTERNAL async object"""
is_connected: bool
def __init__(self):
self.is_connected = True
def __call__(self):
return self.is_connected
async def close(self):
self.is_connected = False
async def blockchain_app():
block = 0
while True:
if random.randint(1, 5) == 3:
raise BlockChainEx
print(f"Current block is {block}, database is ready: {ctx.database()()}")
block += 1
await asyncio.sleep(1)
async def http_app():
uptime = 0
while True:
if random.randint(1, 5) == 3:
raise HTTPex
print(
f"Server online dirung {uptime} seconds, database is ready: {ctx.database()()}"
)
uptime += 1
await asyncio.sleep(1)
class AppContext:
__slots__ = ("state", "DBConnector")
state: dict
def __init__(self):
self.state = {}
def __eq__(self, other: object) -> bool:
return self is other
def __getitem__(self, key: str) -> Any:
return self.state[key]
def __setitem__(self, key: str, value: Any) -> None:
self.state[key] = value
def __delitem__(self, key: str) -> None:
del self.state[key]
def __len__(self) -> int:
return len(self.state)
def __iter__(self) -> Iterator[str]:
return iter(self.state)
def add(self, app):
app_tmp = weakref.ref(app)
self.state[app.__class__.__name__] = app_tmp
@property
def database(self):
return self.state["DBConnector"]
def ex_handler(loop, ex_context):
ex = ex_context.get("exception")
message = ex_context["message"]
print(f"Error: {ex.__class__.__name__}: {message}")
if isinstance(ex, CriticalEx):
print("Need to stop all")
asyncio.create_task(shutdown(loop))
else:
coro_name = ex_context["future"].get_coro().__name__
print(f"Continue work, create new instance of coroutine {coro_name}")
if isinstance(ex, BlockChainEx) and coro_name == f"{blockchain_app.__name__}":
asyncio.create_task(blockchain_app())
elif isinstance(ex, HTTPex) and coro_name == f"{http_app.__name__}":
asyncio.create_task(http_app())
# More high-level exceptions processing...
else:
print(f"Catch unhandled exception, shutdown")
asyncio.create_task(shutdown(loop))
async def shutdown(loop, signal=None):
if signal:
print(f"Received exit signal {signal.name}...")
else:
print("No exit signal")
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
[task.cancel() for task in tasks]
print(f"Cancelling {len(tasks)} outstanding tasks")
await asyncio.gather(*tasks, return_exceptions=True)
print(f"Make some post-shutdown things")
loop.stop()
if __name__ == "__main__":
# setup and configure loop
loop = asyncio.get_event_loop()
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
for s in signals:
loop.add_signal_handler(
s, lambda s=s: asyncio.create_task(shutdown(loop, signal=s))
)
loop.set_exception_handler(ex_handler)
# Create context and add objects
ctx = AppContext()
db = DBConnector()
ctx.add(db)
# Execute
try:
loop.create_task(blockchain_app())
loop.create_task(http_app())
loop.run_forever()
finally:
loop.close()
print("Successfully shutdown the app")