-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move RQ redis_connection helper into a mixin
- Loading branch information
Showing
3 changed files
with
77 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
from redis import Redis | ||
|
||
|
||
class RedisHelper: | ||
def redis_connection(self, redis_config: dict = None) -> Redis: | ||
""" | ||
Return a Redis connection from an RQ queue configuration | ||
""" | ||
if redis_config is not None: | ||
config = {k.lower(): v for k, v in redis_config.items()} | ||
if redis_url := config.get("url"): | ||
del config["url"] | ||
return Redis.from_url(redis_url, **config) | ||
else: | ||
return Redis(**config) | ||
elif redis_url := os.getenv("REDIS_URL"): | ||
return Redis.from_url(redis_url) | ||
else: | ||
raise RuntimeError( | ||
"Missing Redis connection configuration. Please set either " | ||
"settings.JUDOSCALE['REDIS'] or REDIS_URL environment variable." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters