fix(oracle): corrigir persistência e carregamento do ranking
- Corrigir conexão Oracle com teste antes do pool - Carregar ranking em batches de 10k (limite de 10k por query) - Aumentar timeout de carregamento para 5 minutos - Adicionar depends_on para Oracle no docker-compose - Padronizar nome do container Oracle para oracle18c
This commit is contained in:
@@ -17,24 +17,29 @@ class OracleClient:
|
||||
|
||||
def connect(self) -> None:
|
||||
try:
|
||||
test_conn = oracledb.connect(
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
dsn=self.dsn,
|
||||
)
|
||||
test_conn.ping()
|
||||
test_conn.close()
|
||||
|
||||
self._pool = oracledb.create_pool(
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
dsn=self.dsn,
|
||||
min=1,
|
||||
max=20,
|
||||
increment=5,
|
||||
timeout=30,
|
||||
wait_timeout=10,
|
||||
getmode=oracledb.POOL_GETMODE_TIMEDWAIT,
|
||||
max=10,
|
||||
increment=1,
|
||||
)
|
||||
self._connected = True
|
||||
logger.info("Pool Oracle conectado com sucesso")
|
||||
logger.info(f"Pool Oracle conectado: {self.dsn}")
|
||||
except oracledb.Error as e:
|
||||
logger.error(f"Oracle database error: {e}", exc_info=True)
|
||||
logger.error(f"Oracle database error: {e}")
|
||||
self._connected = False
|
||||
except Exception as e:
|
||||
logger.error(f"Oracle connection error: {e}", exc_info=True)
|
||||
logger.error(f"Oracle connection error: {e}")
|
||||
self._connected = False
|
||||
|
||||
def close(self) -> None:
|
||||
|
||||
@@ -32,12 +32,20 @@ async def carregar_ranking_do_oracle() -> int:
|
||||
if total == 0:
|
||||
return []
|
||||
|
||||
consultores = ranking_oracle_repo.buscar_paginado(page=1, size=total)
|
||||
consultores = []
|
||||
batch_size = 10000
|
||||
total_pages = (total + batch_size - 1) // batch_size
|
||||
logger.info(f"Carregando {total} consultores do Oracle em {total_pages} batches...")
|
||||
|
||||
for page in range(1, total_pages + 1):
|
||||
batch = ranking_oracle_repo.buscar_paginado(page=page, size=batch_size)
|
||||
consultores.extend(batch)
|
||||
|
||||
return consultores
|
||||
|
||||
consultores = await asyncio.wait_for(
|
||||
asyncio.get_event_loop().run_in_executor(None, _sync_load),
|
||||
timeout=30.0
|
||||
timeout=300.0
|
||||
)
|
||||
|
||||
if not consultores:
|
||||
@@ -83,16 +91,8 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
try:
|
||||
if oracle_client:
|
||||
def _connect_oracle():
|
||||
oracle_client.connect()
|
||||
return oracle_client.is_connected
|
||||
|
||||
connected = await asyncio.wait_for(
|
||||
asyncio.get_event_loop().run_in_executor(None, _connect_oracle),
|
||||
timeout=10.0
|
||||
)
|
||||
|
||||
if connected:
|
||||
if oracle_client.is_connected:
|
||||
logger.info("Conectado ao Oracle")
|
||||
total = await carregar_ranking_do_oracle()
|
||||
if total > 0:
|
||||
@@ -100,9 +100,7 @@ async def lifespan(app: FastAPI):
|
||||
else:
|
||||
logger.info("Ranking vazio no Oracle - aguardando processamento")
|
||||
else:
|
||||
logger.warning("Não foi possível conectar ao Oracle - ranking será carregado do ES")
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning("Timeout ao conectar ao Oracle - ranking será carregado do ES")
|
||||
logger.warning("Não foi possível conectar ao Oracle")
|
||||
except Exception as e:
|
||||
logger.warning(f"Erro ao inicializar Oracle: {e}")
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ services:
|
||||
build:
|
||||
context: ./backend
|
||||
dockerfile: Dockerfile
|
||||
depends_on:
|
||||
oracle18c:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "8010:8000"
|
||||
env_file:
|
||||
@@ -45,7 +48,7 @@ services:
|
||||
restart: unless-stopped
|
||||
|
||||
oracle18c:
|
||||
container_name: mqapilc_oracle18c
|
||||
container_name: oracle18c
|
||||
image: gvenzl/oracle-xe:18-slim
|
||||
environment:
|
||||
- ORACLE_PASSWORD=local123
|
||||
|
||||
Reference in New Issue
Block a user