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:
Frederico Castro
2025-12-15 07:40:05 -03:00
parent 3a0dd036f4
commit b44582653b
3 changed files with 30 additions and 24 deletions

View File

@@ -17,24 +17,29 @@ class OracleClient:
def connect(self) -> None: def connect(self) -> None:
try: 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( self._pool = oracledb.create_pool(
user=self.user, user=self.user,
password=self.password, password=self.password,
dsn=self.dsn, dsn=self.dsn,
min=1, min=1,
max=20, max=10,
increment=5, increment=1,
timeout=30,
wait_timeout=10,
getmode=oracledb.POOL_GETMODE_TIMEDWAIT,
) )
self._connected = True self._connected = True
logger.info("Pool Oracle conectado com sucesso") logger.info(f"Pool Oracle conectado: {self.dsn}")
except oracledb.Error as e: 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 self._connected = False
except Exception as e: except Exception as e:
logger.error(f"Oracle connection error: {e}", exc_info=True) logger.error(f"Oracle connection error: {e}")
self._connected = False self._connected = False
def close(self) -> None: def close(self) -> None:

View File

@@ -32,12 +32,20 @@ async def carregar_ranking_do_oracle() -> int:
if total == 0: if total == 0:
return [] 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 return consultores
consultores = await asyncio.wait_for( consultores = await asyncio.wait_for(
asyncio.get_event_loop().run_in_executor(None, _sync_load), asyncio.get_event_loop().run_in_executor(None, _sync_load),
timeout=30.0 timeout=300.0
) )
if not consultores: if not consultores:
@@ -83,16 +91,8 @@ async def lifespan(app: FastAPI):
try: try:
if oracle_client: if oracle_client:
def _connect_oracle(): oracle_client.connect()
oracle_client.connect() if oracle_client.is_connected:
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:
logger.info("Conectado ao Oracle") logger.info("Conectado ao Oracle")
total = await carregar_ranking_do_oracle() total = await carregar_ranking_do_oracle()
if total > 0: if total > 0:
@@ -100,9 +100,7 @@ async def lifespan(app: FastAPI):
else: else:
logger.info("Ranking vazio no Oracle - aguardando processamento") logger.info("Ranking vazio no Oracle - aguardando processamento")
else: else:
logger.warning("Não foi possível conectar ao Oracle - ranking será carregado do ES") logger.warning("Não foi possível conectar ao Oracle")
except asyncio.TimeoutError:
logger.warning("Timeout ao conectar ao Oracle - ranking será carregado do ES")
except Exception as e: except Exception as e:
logger.warning(f"Erro ao inicializar Oracle: {e}") logger.warning(f"Erro ao inicializar Oracle: {e}")

View File

@@ -4,6 +4,9 @@ services:
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile dockerfile: Dockerfile
depends_on:
oracle18c:
condition: service_healthy
ports: ports:
- "8010:8000" - "8010:8000"
env_file: env_file:
@@ -45,7 +48,7 @@ services:
restart: unless-stopped restart: unless-stopped
oracle18c: oracle18c:
container_name: mqapilc_oracle18c container_name: oracle18c
image: gvenzl/oracle-xe:18-slim image: gvenzl/oracle-xe:18-slim
environment: environment:
- ORACLE_PASSWORD=local123 - ORACLE_PASSWORD=local123