|
| 1 | +import os |
| 2 | +from urllib.parse import quote |
| 3 | + |
1 | 4 | from passari_workflow.config import CONFIG
|
2 | 5 |
|
3 | 6 | from redis import Redis
|
4 | 7 |
|
5 | 8 |
|
6 |
| -def get_redis_connection(db=0): |
| 9 | +def get_redis_connection(): |
7 | 10 | """
|
8 | 11 | Get Redis connection used for the workflow, distributed locks and other
|
9 | 12 | miscellaneous tasks
|
10 | 13 | """
|
11 |
| - password = CONFIG["redis"].get("password", None) |
12 |
| - redis = Redis( |
13 |
| - host=CONFIG["redis"]["host"], |
14 |
| - port=CONFIG["redis"]["port"], |
15 |
| - db=db, |
16 |
| - password=password if password else None |
17 |
| - ) |
18 |
| - |
19 |
| - return redis |
| 14 | + redis_url = get_redis_url() |
| 15 | + return Redis.from_url(redis_url) |
| 16 | + |
| 17 | + |
| 18 | +def get_redis_url(): |
| 19 | + redis_config = CONFIG.get("redis", {}) |
| 20 | + url = redis_config.get("url") |
| 21 | + host = redis_config.get("host") |
| 22 | + port = redis_config.get("port") |
| 23 | + db = redis_config.get("db") or 0 |
| 24 | + password = redis_config.get("password") or None |
| 25 | + |
| 26 | + if not url and not host: # Get URL from environment |
| 27 | + return os.getenv( |
| 28 | + "PASSARI_WORKFLOW_REDIS_URL", |
| 29 | + os.getenv("REDIS_URL", "redis://localhost/0"), |
| 30 | + ) |
| 31 | + elif url and (host or port or db or password): |
| 32 | + raise EnvironmentError("The 'url' config for Redis is exclusive") |
| 33 | + |
| 34 | + password_part = f":{quote(password)}@" if password else "" |
| 35 | + port_part = f":{port}" if port else "" |
| 36 | + return f"redis://{password_part}{host}{port_part}/{db}" |
0 commit comments