From b44582653b7b411f8f3cd249927be3523abfbe51 Mon Sep 17 00:00:00 2001 From: Frederico Castro Date: Mon, 15 Dec 2025 07:40:05 -0300 Subject: [PATCH] =?UTF-8?q?fix(oracle):=20corrigir=20persist=C3=AAncia=20e?= =?UTF-8?q?=20carregamento=20do=20ranking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- backend/src/infrastructure/oracle/client.py | 21 ++++++++++------ backend/src/interface/api/app.py | 28 ++++++++++----------- docker-compose.yml | 5 +++- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/backend/src/infrastructure/oracle/client.py b/backend/src/infrastructure/oracle/client.py index 536b29f..ccf07b5 100644 --- a/backend/src/infrastructure/oracle/client.py +++ b/backend/src/infrastructure/oracle/client.py @@ -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: diff --git a/backend/src/interface/api/app.py b/backend/src/interface/api/app.py index 073bd1b..9ea2eff 100644 --- a/backend/src/interface/api/app.py +++ b/backend/src/interface/api/app.py @@ -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: + oracle_client.connect() + 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}") diff --git a/docker-compose.yml b/docker-compose.yml index 7929e19..7b8977e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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