-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragile_db.py
43 lines (34 loc) · 1009 Bytes
/
fragile_db.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
import asyncio
import functools
import logging
import socket
from asyncpg.exceptions import PostgresConnectionError, InterfaceError, UndefinedTableError
logger = logging.getLogger()
CONNECTION_EXCEPTIONS = (
OSError,
socket.gaierror,
asyncio.TimeoutError,
PostgresConnectionError,
InterfaceError
)
QUERY_EXCEPTIONS = (
UndefinedTableError,
)
def fragile_db():
"""
Shuts down the whole app on any DB connection or table access error
"""
def wrapper(func):
@functools.wraps(func)
async def wrapped(*args, **kwargs):
try:
result = await func(*args, **kwargs)
except CONNECTION_EXCEPTIONS:
logger.critical('Postgres connection is gone, exiting')
raise SystemExit()
except QUERY_EXCEPTIONS:
logger.critical('Fatal Postgres query error, exiting')
raise SystemExit()
return result
return wrapped
return wrapper