Skip to content

Commit 962f496

Browse files
authored
Fix/redis import lazy (#235)
* fix: defer redis import unitl runtime to avoid ImportError Signed-off-by: reevebarreto <[email protected]> * updated test cases for redis_isready Signed-off-by: Reeve Barreto <[email protected]> --------- Signed-off-by: reevebarreto <[email protected]> Signed-off-by: Reeve Barreto <[email protected]>
1 parent b9d3af4 commit 962f496

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

mcpgateway/utils/redis_isready.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
wait_for_redis_ready(sync=True) # synchronous / blocking
5555
"""
5656

57-
5857
# Standard
5958
import argparse
6059
import asyncio
@@ -64,15 +63,8 @@
6463
import time
6564
from typing import Any, Optional
6665

67-
# ---------------------------------------------------------------------------
68-
# Third-party imports - abort early if redis is missing
69-
# ---------------------------------------------------------------------------
70-
try:
71-
# Third-Party
72-
from redis import Redis
73-
except ImportError: # pragma: no cover - handled at runtime for the CLI
74-
sys.stderr.write("redis library not installed - aborting (pip install redis)\n")
75-
sys.exit(2)
66+
# First Party imports
67+
from mcpgateway.config import settings
7668

7769
# Environment variables
7870
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
@@ -139,6 +131,13 @@ def _probe(*_: Any) -> None:
139131
Raises:
140132
RuntimeError: Forwarded after exhausting ``max_retries`` attempts.
141133
"""
134+
try:
135+
# Import redis here to avoid dependency issues if not used
136+
from redis import Redis
137+
except ImportError: # pragma: no cover - handled at runtime for the CLI
138+
sys.stderr.write("redis library not installed - aborting (pip install redis)\n")
139+
sys.exit(2)
140+
142141
redis_client = Redis.from_url(redis_url)
143142
for attempt in range(1, max_retries + 1):
144143
try:
@@ -220,4 +219,9 @@ def main() -> None: # pragma: no cover
220219

221220

222221
if __name__ == "__main__": # pragma: no cover
223-
main()
222+
if settings.cache_type == "redis":
223+
# Ensure Redis is ready before proceeding
224+
main()
225+
else:
226+
# If not using Redis, just exit with success
227+
sys.exit(0)

tests/unit/mcpgateway/utils/test_redis_isready.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Authors: Reeve Barreto, Mihai Criveti
77
88
"""
9+
910
# Standard
1011
import asyncio
1112
from unittest.mock import patch
@@ -84,7 +85,7 @@ def test_wait_for_redis_ready_success(monkeypatch):
8485

8586
monkeypatch.setattr(redis_isready.time, "sleep", lambda *_: None)
8687

87-
with patch("mcpgateway.utils.redis_isready.Redis", MockRedis):
88+
with patch("redis.Redis", MockRedis):
8889
redis_isready.wait_for_redis_ready(
8990
redis_url="redis://localhost:6379/0",
9091
max_retries=3,
@@ -113,7 +114,7 @@ class MockRedisWithFromUrl:
113114
def from_url(cls, url):
114115
return mock
115116

116-
with patch("mcpgateway.utils.redis_isready.Redis", MockRedisWithFromUrl):
117+
with patch("redis.Redis", MockRedisWithFromUrl):
117118
redis_isready.wait_for_redis_ready(
118119
redis_url="redis://localhost:6379/0",
119120
max_retries=5,
@@ -137,7 +138,7 @@ class MockRedisWithFromUrl:
137138
def from_url(cls, url):
138139
return mock
139140

140-
with patch("mcpgateway.utils.redis_isready.Redis", MockRedisWithFromUrl):
141+
with patch("redis.Redis", MockRedisWithFromUrl):
141142
with pytest.raises(RuntimeError, match="Redis not ready after"):
142143
redis_isready.wait_for_redis_ready(
143144
redis_url="redis://localhost:6379/0",
@@ -188,7 +189,7 @@ class MockRedisWithFromUrl:
188189
def from_url(cls, url):
189190
return mock
190191

191-
with patch("mcpgateway.utils.redis_isready.Redis", MockRedisWithFromUrl):
192+
with patch("redis.Redis", MockRedisWithFromUrl):
192193
redis_isready.wait_for_redis_ready(
193194
redis_url="redis://localhost:6379/0",
194195
max_retries=2,

0 commit comments

Comments
 (0)