From dcbc8d1ade5ed9fbac170ca24bdf58875aaaecb0 Mon Sep 17 00:00:00 2001 From: husaynirfan1 Date: Fri, 18 Apr 2025 00:55:29 +0800 Subject: [PATCH 1/4] Test redis direct connection, fallback to docker if failed --- start_server.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/start_server.py b/start_server.py index f914bbd1..5a721fe1 100644 --- a/start_server.py +++ b/start_server.py @@ -44,7 +44,16 @@ def wait_for_redis(host="localhost", port=6379, timeout=20): return False def check_and_start_redis(): - """Check if the Redis container is running, start if necessary.""" + """Check if the Redis service is available (via Docker or directly), and start the Docker container if necessary.""" + redis_host = "localhost" # or use any custom host if required + redis_port = 6379 + + # Try to connect directly to Redis + if wait_for_redis(host=redis_host, port=redis_port): + logging.info("Redis is already running and available.") + return # Return early if Redis is available + + # If not available, check if the Docker container is running or stopped try: # Check if container exists and is running check_running_cmd = ["docker", "ps", "-q", "-f", "name=morphik-redis"] @@ -80,6 +89,7 @@ def check_and_start_redis(): sys.exit(1) + def start_arq_worker(): """Start the ARQ worker as a subprocess.""" global worker_process @@ -317,4 +327,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file From 88f4e32d8e4418511af82a455986d7d1dd9ddcf0 Mon Sep 17 00:00:00 2001 From: Husayn Irfan <112241008+husaynirfan1@users.noreply.github.com> Date: Fri, 18 Apr 2025 01:00:55 +0800 Subject: [PATCH 2/4] Update start_server.py Changed to use env var Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --- start_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start_server.py b/start_server.py index 5a721fe1..9d772ac9 100644 --- a/start_server.py +++ b/start_server.py @@ -45,7 +45,7 @@ def wait_for_redis(host="localhost", port=6379, timeout=20): def check_and_start_redis(): """Check if the Redis service is available (via Docker or directly), and start the Docker container if necessary.""" - redis_host = "localhost" # or use any custom host if required + redis_host = os.environ.get("REDIS_HOST", "127.0.0.1") # or use any custom host if required redis_port = 6379 # Try to connect directly to Redis From fb9e4029d53839dcd806dde4cebcdb88d7ea4138 Mon Sep 17 00:00:00 2001 From: Husayn Irfan <112241008+husaynirfan1@users.noreply.github.com> Date: Fri, 18 Apr 2025 12:02:56 +0800 Subject: [PATCH 3/4] Update start_server.py --- start_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/start_server.py b/start_server.py index 9d772ac9..636aeb62 100644 --- a/start_server.py +++ b/start_server.py @@ -46,7 +46,7 @@ def wait_for_redis(host="localhost", port=6379, timeout=20): def check_and_start_redis(): """Check if the Redis service is available (via Docker or directly), and start the Docker container if necessary.""" redis_host = os.environ.get("REDIS_HOST", "127.0.0.1") # or use any custom host if required - redis_port = 6379 + redis_port = int(os.getenv("REDIS_PORT", 6379)) # Try to connect directly to Redis if wait_for_redis(host=redis_host, port=redis_port): @@ -327,4 +327,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() From fd39abfed99ef8198d35fe2628d220e88a848e3d Mon Sep 17 00:00:00 2001 From: Husayn Irfan <112241008+husaynirfan1@users.noreply.github.com> Date: Fri, 18 Apr 2025 12:11:48 +0800 Subject: [PATCH 4/4] Wrap try catch for redis port checking. --- start_server.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/start_server.py b/start_server.py index 636aeb62..323b603a 100644 --- a/start_server.py +++ b/start_server.py @@ -46,7 +46,12 @@ def wait_for_redis(host="localhost", port=6379, timeout=20): def check_and_start_redis(): """Check if the Redis service is available (via Docker or directly), and start the Docker container if necessary.""" redis_host = os.environ.get("REDIS_HOST", "127.0.0.1") # or use any custom host if required - redis_port = int(os.getenv("REDIS_PORT", 6379)) + + try: + redis_port = int(os.getenv("REDIS_PORT", 6379)) + except ValueError: + logging.info("❌ Invalid REDIS_PORT value. Must be an integer. Defaulting to 6379.") + redis_port = 6379 # Try to connect directly to Redis if wait_for_redis(host=redis_host, port=redis_port):