]>
Commit | Line | Data |
---|---|---|
1 | import psycopg2 | |
2 | import redis as _redis | |
3 | ||
4 | redis = None | |
5 | psql = None | |
6 | ||
7 | def 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 | ||
17 | def psql_connected(): | |
18 | global psql | |
19 | return psql is not None and psql.closed == 0 | |
20 | ||
21 | def 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 | ||
39 | def 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) |