better handle socket URLs

This commit is contained in:
shamoon 2024-03-14 07:57:41 -07:00
parent d303f14f02
commit 8c0df93137
2 changed files with 32 additions and 5 deletions

View File

@ -46,7 +46,7 @@ class TestSystemStatus(APITestCase):
self.assertEqual(response.data["database"]["status"], "OK")
self.assertIsNone(response.data["database"]["error"])
self.assertIsNotNone(response.data["database"]["migration_status"])
self.assertEqual(response.data["tasks"]["redis_url"], "localhost:6379")
self.assertEqual(response.data["tasks"]["redis_url"], "redis://localhost:6379")
self.assertEqual(response.data["tasks"]["redis_status"], "ERROR")
self.assertIsNotNone(response.data["tasks"]["redis_error"])
@ -109,11 +109,35 @@ class TestSystemStatus(APITestCase):
THEN:
- The response contains the redis URL but no credentials
"""
with override_settings(_CELERY_REDIS_URL="redis://:password@localhost:6379/0"):
with override_settings(
_CHANNELS_REDIS_URL="redis://username:password@localhost:6379",
):
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["tasks"]["redis_url"], "localhost:6379")
self.assertEqual(
response.data["tasks"]["redis_url"],
"redis://localhost:6379",
)
def test_system_status_redis_socket(self):
"""
GIVEN:
- Redis URL is socket
WHEN:
- The user requests the system status
THEN:
- The response contains the redis URL
"""
with override_settings(_CHANNELS_REDIS_URL="unix:///path/to/redis.sock"):
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["tasks"]["redis_url"],
"unix:///path/to/redis.sock",
)
@mock.patch("celery.app.control.Inspect.ping")
def test_system_status_celery_ping(self, mock_ping):

View File

@ -1618,7 +1618,10 @@ class SystemStatusView(GenericAPIView, PassUserMixin):
media_stats = os.statvfs(settings.MEDIA_ROOT)
redis_url = settings._CHANNELS_REDIS_URL
redis_url_parsed = urlparse(settings._CHANNELS_REDIS_URL)
redis_url_parsed = urlparse(redis_url)
redis_constructed_url = f"{redis_url_parsed.scheme}://{redis_url_parsed.path or redis_url_parsed.hostname}"
if redis_url_parsed.hostname is not None:
redis_constructed_url += f":{redis_url_parsed.port}"
redis_error = None
with Redis.from_url(url=redis_url) as client:
try:
@ -1730,7 +1733,7 @@ class SystemStatusView(GenericAPIView, PassUserMixin):
},
},
"tasks": {
"redis_url": f"{redis_url_parsed.hostname}:{redis_url_parsed.port}",
"redis_url": redis_constructed_url,
"redis_status": redis_status,
"redis_error": redis_error,
"celery_status": celery_active,