]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blame_incremental - dbs.py
Add increasing delay to fetching cryptoportfolio
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / dbs.py
... / ...
CommitLineData
1import psycopg2
2import redis as _redis
3
4redis = None
5psql = None
6
7def redis_connected():
8 global redis
9 if redis is None:
10 return False
11 else:
12 try:
13 return redis.ping()
14 except Exception:
15 return False
16
17def psql_connected():
18 global psql
19 return psql is not None and psql.closed == 0
20
21def connect_redis(args):
22 global redis
23
24 redis_config = {
25 "host": args.redis_host,
26 "port": args.redis_port,
27 "db": args.redis_database,
28 }
29 if redis_config["host"].startswith("/"):
30 redis_config["unix_socket_path"] = redis_config.pop("host")
31 del(redis_config["port"])
32 del(args.redis_host)
33 del(args.redis_port)
34 del(args.redis_database)
35
36 if redis is None:
37 redis = _redis.Redis(**redis_config)
38
39def connect_psql(args):
40 global psql
41 pg_config = {
42 "host": args.db_host,
43 "port": args.db_port,
44 "user": args.db_user,
45 "password": args.db_password,
46 "database": args.db_database,
47 }
48 del(args.db_host)
49 del(args.db_port)
50 del(args.db_user)
51 del(args.db_password)
52 del(args.db_database)
53
54 if psql is None:
55 psql = psycopg2.connect(**pg_config)