Spaces:
Running
Running
Commit Β·
8bd7457
1
Parent(s): 6b12e79
Fix 502: retry Neo4j connection at startup, delay backend 50s to wait for Neo4j
Browse files- backend/neo4j_setup.py +34 -7
- docker/entrypoint.sh +2 -0
- docker/supervisord.conf +4 -4
backend/neo4j_setup.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from neo4j import GraphDatabase
|
| 2 |
import os
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
load_dotenv()
|
|
@@ -7,22 +8,48 @@ load_dotenv()
|
|
| 7 |
|
| 8 |
class Neo4jConnection:
|
| 9 |
def __init__(self, uri: str, user: str, password: str, database: str = "neo4j"):
|
| 10 |
-
self.
|
|
|
|
| 11 |
self.database = database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def close(self):
|
| 14 |
-
self.driver
|
|
|
|
| 15 |
|
| 16 |
def run_query(self, query: str, parameters: dict | None = None) -> list:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
neo4j_conn = Neo4jConnection(
|
| 23 |
-
uri=os.getenv("NEO4J_URI", ""),
|
| 24 |
user=os.getenv("NEO4J_USERNAME", "neo4j"),
|
| 25 |
-
password=os.getenv("NEO4J_PASSWORD", ""),
|
| 26 |
database=os.getenv("NEO4J_DATABASE", "neo4j"),
|
| 27 |
)
|
| 28 |
|
|
|
|
| 1 |
from neo4j import GraphDatabase
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
|
|
|
| 8 |
|
| 9 |
class Neo4jConnection:
|
| 10 |
def __init__(self, uri: str, user: str, password: str, database: str = "neo4j"):
|
| 11 |
+
self._uri = uri
|
| 12 |
+
self._auth = (user, password)
|
| 13 |
self.database = database
|
| 14 |
+
self.driver = None
|
| 15 |
+
self._connect_with_retry()
|
| 16 |
+
|
| 17 |
+
def _connect_with_retry(self, retries: int = 10, delay: int = 6):
|
| 18 |
+
for attempt in range(retries):
|
| 19 |
+
try:
|
| 20 |
+
self.driver = GraphDatabase.driver(self._uri, auth=self._auth)
|
| 21 |
+
self.driver.verify_connectivity()
|
| 22 |
+
print(f"[neo4j] Connected on attempt {attempt + 1}")
|
| 23 |
+
return
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"[neo4j] Attempt {attempt + 1}/{retries} failed: {e}")
|
| 26 |
+
if attempt < retries - 1:
|
| 27 |
+
time.sleep(delay)
|
| 28 |
+
print("[neo4j] WARNING: Could not connect β queries will fail until Neo4j is ready")
|
| 29 |
|
| 30 |
def close(self):
|
| 31 |
+
if self.driver:
|
| 32 |
+
self.driver.close()
|
| 33 |
|
| 34 |
def run_query(self, query: str, parameters: dict | None = None) -> list:
|
| 35 |
+
if not self.driver:
|
| 36 |
+
self._connect_with_retry()
|
| 37 |
+
try:
|
| 38 |
+
with self.driver.session(database=self.database) as session:
|
| 39 |
+
result = session.run(query, parameters or {})
|
| 40 |
+
return [record.data() for record in result]
|
| 41 |
+
except Exception:
|
| 42 |
+
# Driver may have gone stale β reconnect once and retry
|
| 43 |
+
self._connect_with_retry(retries=3, delay=3)
|
| 44 |
+
with self.driver.session(database=self.database) as session:
|
| 45 |
+
result = session.run(query, parameters or {})
|
| 46 |
+
return [record.data() for record in result]
|
| 47 |
|
| 48 |
|
| 49 |
neo4j_conn = Neo4jConnection(
|
| 50 |
+
uri=os.getenv("NEO4J_URI", "bolt://127.0.0.1:7687"),
|
| 51 |
user=os.getenv("NEO4J_USERNAME", "neo4j"),
|
| 52 |
+
password=os.getenv("NEO4J_PASSWORD", "clinicalmatch2024"),
|
| 53 |
database=os.getenv("NEO4J_DATABASE", "neo4j"),
|
| 54 |
)
|
| 55 |
|
docker/entrypoint.sh
CHANGED
|
@@ -58,4 +58,6 @@ fi
|
|
| 58 |
mkdir -p /tmp/nginx-cache /tmp/nginx-body
|
| 59 |
|
| 60 |
log "Starting all services via supervisord..."
|
|
|
|
|
|
|
| 61 |
exec /usr/bin/supervisord -c /app/docker/supervisord.conf
|
|
|
|
| 58 |
mkdir -p /tmp/nginx-cache /tmp/nginx-body
|
| 59 |
|
| 60 |
log "Starting all services via supervisord..."
|
| 61 |
+
# nginx needs its tmp dirs writable
|
| 62 |
+
mkdir -p /tmp/nginx-cache /tmp/nginx-body /tmp/nginx-run
|
| 63 |
exec /usr/bin/supervisord -c /app/docker/supervisord.conf
|
docker/supervisord.conf
CHANGED
|
@@ -19,16 +19,16 @@ command=/opt/neo4j/bin/neo4j console
|
|
| 19 |
environment=NEO4J_HOME=/opt/neo4j,JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
|
| 20 |
autostart=true
|
| 21 |
autorestart=true
|
| 22 |
-
startsecs=
|
| 23 |
startretries=3
|
| 24 |
stdout_logfile=/tmp/neo4j.log
|
| 25 |
stderr_logfile=/tmp/neo4j.log
|
| 26 |
redirect_stderr=true
|
| 27 |
priority=10
|
| 28 |
|
| 29 |
-
# ββ FastAPI backend ββ
|
| 30 |
[program:backend]
|
| 31 |
-
command=python3 -m uvicorn main:app --host 127.0.0.1 --port 8000 --workers
|
| 32 |
directory=/app/backend
|
| 33 |
environment=
|
| 34 |
NEO4J_URI="bolt://127.0.0.1:7687",
|
|
@@ -40,7 +40,7 @@ environment=
|
|
| 40 |
OPENAI_MODEL="%(ENV_OPENAI_MODEL)s"
|
| 41 |
autostart=true
|
| 42 |
autorestart=true
|
| 43 |
-
startsecs=
|
| 44 |
startretries=5
|
| 45 |
stdout_logfile=/tmp/backend.log
|
| 46 |
stderr_logfile=/tmp/backend.log
|
|
|
|
| 19 |
environment=NEO4J_HOME=/opt/neo4j,JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
|
| 20 |
autostart=true
|
| 21 |
autorestart=true
|
| 22 |
+
startsecs=45
|
| 23 |
startretries=3
|
| 24 |
stdout_logfile=/tmp/neo4j.log
|
| 25 |
stderr_logfile=/tmp/neo4j.log
|
| 26 |
redirect_stderr=true
|
| 27 |
priority=10
|
| 28 |
|
| 29 |
+
# ββ FastAPI backend (starts after Neo4j is ready via retry logic in neo4j_setup.py) ββ
|
| 30 |
[program:backend]
|
| 31 |
+
command=bash -c "sleep 50 && python3 -m uvicorn main:app --host 127.0.0.1 --port 8000 --workers 1"
|
| 32 |
directory=/app/backend
|
| 33 |
environment=
|
| 34 |
NEO4J_URI="bolt://127.0.0.1:7687",
|
|
|
|
| 40 |
OPENAI_MODEL="%(ENV_OPENAI_MODEL)s"
|
| 41 |
autostart=true
|
| 42 |
autorestart=true
|
| 43 |
+
startsecs=15
|
| 44 |
startretries=5
|
| 45 |
stdout_logfile=/tmp/backend.log
|
| 46 |
stderr_logfile=/tmp/backend.log
|