-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbackend.py
51 lines (36 loc) · 1.24 KB
/
backend.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
from os import getenv
from redis import Redis
from redis.exceptions import ConnectionError
def get_redis_password() -> str:
return getenv("REDIS_PASS", "password")
def get_redis_port() -> str:
return getenv("REDIS_PORT", "6379")
def get_redis_dbnum() -> str:
return getenv("REDIS_DB", "0")
def get_redis_host() -> str:
return getenv("REDIS_HOST", "127.0.0.1")
def get_backend_url() -> str:
pw = get_redis_password()
port = get_redis_port()
db = get_redis_dbnum()
return "redis://{password}{hostname}{port}{db}".format(
hostname=get_redis_host(),
password=':' + pw + '@' if len(pw) != 0 else '',
port=':' + port if len(port) != 0 else '',
db='/' + db if len(db) != 0 else ''
)
def is_backend_running() -> bool:
try:
conn = Redis(
host=get_redis_host(),
port=int(get_redis_port()),
db=int(get_redis_dbnum()),
password=get_redis_password()
)
conn.client_list() # Must perform an operation to check connection.
except ConnectionError as e:
print("Failed to connect to Redis instance at %s", get_redis_host())
print(repr(e))
return False
conn.close() # type: ignore
return True