Skip to content

Commit 9cfe2ea

Browse files
authored
Merge pull request #450 from revsys/feature/allow-redis-connection-kwargs
Feature/allow redis connection kwargs
2 parents 9ad0d65 + bab13f6 commit 9cfe2ea

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ ENV/
107107
.direnv
108108

109109
# mac
110-
.DS_Store
110+
.DS_Store
111+
.aider*

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ It can be customized by setting `HEALTHCHECK_CACHE_KEY` to another value:
138138
HEALTHCHECK_CACHE_KEY = "custom_healthcheck_key"
139139
```
140140

141+
Additional connection options may be specified by defining a variable ``HEALTHCHECK_REDIS_URL_OPTIONS`` on the settings module.
142+
141143
## Setting up monitoring
142144

143145
You can use tools like Pingdom, StatusCake or other uptime robots to monitor service status.

docs/settings.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ The cache backend uses the following setting:
5959
- String
6060
- `djangohealthcheck_test`
6161
- Specifies the name of the key to write to and read from to validate that the cache is working.
62+
* - `HEALTHCHECK_REDIS_URL_OPTIONS`
63+
- Dict
64+
- {}
65+
- Additional arguments which will be passed as keyword arguments to the Redis connection class initialiser.
6266

6367
`psutil`
6468
--------

health_check/contrib/redis/backends.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class RedisHealthCheck(BaseHealthCheckBackend):
1313
"""Health check for Redis."""
1414

1515
redis_url = getattr(settings, "REDIS_URL", "redis://localhost/1")
16+
redis_url_options = getattr(settings, "HEALTHCHECK_REDIS_URL_OPTIONS", {})
1617

1718
def check_status(self):
1819
"""Check Redis service by pinging the redis instance with a redis connection."""
@@ -21,7 +22,7 @@ def check_status(self):
2122
logger.debug("Attempting to connect to redis...")
2223
try:
2324
# conn is used as a context to release opened resources later
24-
with from_url(self.redis_url) as conn:
25+
with from_url(self.redis_url, **self.redis_url_options) as conn:
2526
conn.ping() # exceptions may be raised upon ping
2627
except ConnectionRefusedError as e:
2728
self.add_error(

tests/test_redis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_redis_refused_connection(self, mocked_connection, mocked_getattr):
2828
assert len(redis_healthchecker.errors), 1
2929

3030
# mock assertions
31-
mocked_connection.assert_called_once_with("redis://localhost/1")
31+
mocked_connection.assert_called_once_with("redis://localhost/1", **{})
3232

3333
@mock.patch("health_check.contrib.redis.backends.getattr")
3434
@mock.patch("health_check.contrib.redis.backends.from_url")
@@ -50,7 +50,7 @@ def test_redis_timeout_error(self, mocked_connection, mocked_getattr):
5050
assert len(redis_healthchecker.errors), 1
5151

5252
# mock assertions
53-
mocked_connection.assert_called_once_with("redis://localhost/1")
53+
mocked_connection.assert_called_once_with("redis://localhost/1", **{})
5454

5555
@mock.patch("health_check.contrib.redis.backends.getattr")
5656
@mock.patch("health_check.contrib.redis.backends.from_url")
@@ -72,7 +72,7 @@ def test_redis_con_limit_exceeded(self, mocked_connection, mocked_getattr):
7272
assert len(redis_healthchecker.errors), 1
7373

7474
# mock assertions
75-
mocked_connection.assert_called_once_with("redis://localhost/1")
75+
mocked_connection.assert_called_once_with("redis://localhost/1", **{})
7676

7777
@mock.patch("health_check.contrib.redis.backends.getattr")
7878
@mock.patch("health_check.contrib.redis.backends.from_url")
@@ -92,4 +92,4 @@ def test_redis_conn_ok(self, mocked_connection, mocked_getattr):
9292
assert len(redis_healthchecker.errors), 0
9393

9494
# mock assertions
95-
mocked_connection.assert_called_once_with("redis://localhost/1")
95+
mocked_connection.assert_called_once_with("redis://localhost/1", **{})

0 commit comments

Comments
 (0)