Revert "Corrige pool esgotado e erros do Holmes sem mensagem"
This reverts commit 006687f250.
This commit is contained in:
parent
006687f250
commit
61f16b100b
4 changed files with 76 additions and 115 deletions
|
|
@ -2,13 +2,9 @@ import logging
|
|||
|
||||
from sqlalchemy.pool import PoolProxiedConnection
|
||||
|
||||
from app.core.database import engine
|
||||
|
||||
logger = logging.getLogger('api_podium')
|
||||
|
||||
|
||||
def registrar_contador(
|
||||
conn: PoolProxiedConnection | None,
|
||||
conn: PoolProxiedConnection,
|
||||
api: str,
|
||||
endpoint: str,
|
||||
metodo: str,
|
||||
|
|
@ -17,36 +13,25 @@ def registrar_contador(
|
|||
duracao_ms: float,
|
||||
erro: str | None = None,
|
||||
) -> None:
|
||||
"""Grava uma linha na tabela contadores_api. Falha aqui não derruba a chamada principal.
|
||||
|
||||
Sem conn, pega uma conexão do pool só pro INSERT e devolve logo em seguida.
|
||||
"""
|
||||
conexao_propria = conn is None
|
||||
"""Grava uma linha na tabela contadores_api. Falha aqui não derruba a chamada principal."""
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
if conexao_propria:
|
||||
conn = engine.raw_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("""
|
||||
INSERT INTO contadores_api
|
||||
(api, endpoint, metodo_http, status_code, sucesso, duracao_ms, mensagem_erro)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
api,
|
||||
endpoint,
|
||||
metodo,
|
||||
status_code,
|
||||
'S' if sucesso else 'N',
|
||||
round(duracao_ms, 2),
|
||||
erro[:500] if erro else None,
|
||||
))
|
||||
conn.commit()
|
||||
finally:
|
||||
cursor.close()
|
||||
cursor.execute("""
|
||||
INSERT INTO contadores_api
|
||||
(api, endpoint, metodo_http, status_code, sucesso, duracao_ms, mensagem_erro)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
api,
|
||||
endpoint,
|
||||
metodo,
|
||||
status_code,
|
||||
'S' if sucesso else 'N',
|
||||
round(duracao_ms, 2),
|
||||
erro[:500] if erro else None,
|
||||
))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
if conn is not None:
|
||||
conn.rollback()
|
||||
logger.error(f'Falha ao gravar contador: {type(e).__name__}: {e}')
|
||||
conn.rollback()
|
||||
logging.error(f'Falha ao gravar contador: {e}')
|
||||
finally:
|
||||
if conexao_propria and conn is not None:
|
||||
conn.close()
|
||||
cursor.close()
|
||||
|
|
|
|||
|
|
@ -10,11 +10,6 @@ from sqlalchemy.pool import PoolProxiedConnection
|
|||
from app.core.configs import settings
|
||||
from app.utils.api_tracker import registrar_contador
|
||||
|
||||
logger = logging.getLogger("api_podium")
|
||||
|
||||
# O padrão do httpx é 5s, pouco pro Holmes em horário de pico
|
||||
HOLMES_TIMEOUT = httpx.Timeout(30.0, connect=10.0)
|
||||
|
||||
_TOKEN_FILE = os.path.join(tempfile.gettempdir(), "holmes_token_cache.json")
|
||||
|
||||
async def get_header() -> tuple[dict, str]:
|
||||
|
|
@ -27,7 +22,7 @@ async def _holmes_request(
|
|||
api_nome: str = "holmes"
|
||||
for tentativa in range(2):
|
||||
header, api_nome = await get_header()
|
||||
async with httpx.AsyncClient(timeout=HOLMES_TIMEOUT) as client:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await getattr(client, method)(url, headers=header, **kwargs)
|
||||
return response, api_nome
|
||||
raise RuntimeError("Holmes: falha de autenticação após retry")
|
||||
|
|
@ -51,12 +46,12 @@ async def get_holmes_process(id_processo: str, conn: PoolProxiedConnection):
|
|||
return response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return None
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -91,12 +86,12 @@ async def get_holmes_process_details(id_processo: str, conn: PoolProxiedConnecti
|
|||
return response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes details (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes details (ID {id_processo}): {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes details: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes details: {e}")
|
||||
return None
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -112,7 +107,7 @@ async def get_holmes_process_details(id_processo: str, conn: PoolProxiedConnecti
|
|||
)
|
||||
|
||||
|
||||
async def get_holmes_history(id_processo: str, conn: PoolProxiedConnection | None = None):
|
||||
async def get_holmes_history(id_processo: str, conn: PoolProxiedConnection):
|
||||
"""
|
||||
Busca o historico no holmes (importante para pegar o id da ultima task, para avançar futuramente)
|
||||
Args:
|
||||
|
|
@ -139,12 +134,12 @@ async def get_holmes_history(id_processo: str, conn: PoolProxiedConnection | Non
|
|||
return response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return None
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -176,12 +171,12 @@ async def get_holmes_rateio(id_processo: str, conn: PoolProxiedConnection):
|
|||
return response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return None
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -212,12 +207,12 @@ async def get_holmes_dados_tabela(id_processo: str, id_tabela: str, conn: PoolPr
|
|||
return response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return None
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -345,12 +340,12 @@ async def buscar_processo(
|
|||
return {"status": True, "dados": {**dados, "docs": docs, "total": len(docs)}}
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {chave}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {chave}): {e}")
|
||||
return {"status": False, "error": e}
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return {"status": False, "error": e}
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -426,12 +421,12 @@ async def buscar_processo_por_chaves(
|
|||
return {"status": True, "dados": {**dados, "docs": docs, "total": len(docs)}}
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (combinacoes {combinacoes}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (combinacoes {combinacoes}): {e}")
|
||||
return {"status": False, "error": e}
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return {"status": False, "error": e}
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -456,7 +451,7 @@ async def action(payload: dict, id_task: str, id_processo: str, conn: PoolProxie
|
|||
api_nome = "holmes"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=HOLMES_TIMEOUT) as client:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url, headers={"api_token": settings.holmes.token}, json=payload
|
||||
)
|
||||
|
|
@ -466,12 +461,12 @@ async def action(payload: dict, id_task: str, id_processo: str, conn: PoolProxie
|
|||
return True, response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes (ID {id_processo}): {e}")
|
||||
return False, e
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao consultar Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao consultar Holmes: {e}")
|
||||
return False, e
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -507,12 +502,12 @@ async def cria_processo(
|
|||
return True, response.json()
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro na API Holmes - Criar Processo ({payload}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro na API Holmes - Criar Processo ({payload}): {e}")
|
||||
return False, str(e)
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao criar processo no Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao criar processo no Holmes: {e}")
|
||||
return False, str(e)
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -549,7 +544,7 @@ async def enviar_documento(
|
|||
|
||||
try:
|
||||
files = {"file": (nome_arquivo, arquivo, "application/pdf")}
|
||||
async with httpx.AsyncClient(timeout=HOLMES_TIMEOUT) as client:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url, headers={"api_token": settings.holmes.token}, files=files
|
||||
)
|
||||
|
|
@ -559,12 +554,12 @@ async def enviar_documento(
|
|||
return True, {"task_id": task_id}
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro ao enviar documento Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro ao enviar documento Holmes (ID {id_processo}): {e}")
|
||||
return False, str(e)
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao enviar documento Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao enviar documento Holmes: {e}")
|
||||
return False, str(e)
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
@ -617,12 +612,12 @@ async def cancela_processo(
|
|||
}
|
||||
except httpx.HTTPStatusError as e:
|
||||
status = e.response.status_code
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.error(f"Erro ao cancelar processo Holmes (ID {id_processo}): {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro ao cancelar processo Holmes (ID {id_processo}): {e}")
|
||||
return False, str(e)
|
||||
except Exception as e:
|
||||
erro = f"{type(e).__name__}: {e}"
|
||||
logger.exception(f"Erro inesperado ao cancelar processo Holmes: {type(e).__name__}: {e}")
|
||||
erro = str(e)
|
||||
logging.error(f"Erro inesperado ao cancelar processo Holmes: {e}")
|
||||
return False, str(e)
|
||||
finally:
|
||||
duracao = (time.perf_counter() - inicio) * 1000
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from fastapi import APIRouter, HTTPException, status
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
|
||||
from app.core.database import engine
|
||||
from app.core.database import Conexao
|
||||
from app.utils.holmes import get_holmes_history
|
||||
from app.v1.modules.escrituracao.models.modelo_notas import NotasHolmesInput
|
||||
from app.v1.modules.escrituracao.service import NotasDespesas
|
||||
|
|
@ -10,30 +10,23 @@ escrituracao_router = APIRouter(
|
|||
prefix='/escrituracao'
|
||||
)
|
||||
|
||||
|
||||
def gravar_nota(dados_nota_holmes: NotasHolmesInput, historico: dict | None) -> str:
|
||||
# Pega a conexão só na hora de gravar: segurar ela enquanto espera o Holmes esgotava o pool
|
||||
conexao = engine.raw_connection()
|
||||
try:
|
||||
return NotasDespesas(conexao).inserir_nota_despesas(dados_nota_holmes, historico)
|
||||
finally:
|
||||
conexao.close()
|
||||
|
||||
|
||||
# Rota que recebe a nota do Holmes e grava na PD_HOLMES_APOLLO
|
||||
@escrituracao_router.post(
|
||||
path='/notas_despesas'
|
||||
)
|
||||
async def inserir_nota_despesas(
|
||||
dados_nota_holmes: NotasHolmesInput,
|
||||
conexao: Conexao,
|
||||
):
|
||||
|
||||
# History traz a hora de cada "Aprovar"; se o Holmes falhar, volta None e grava sem a hora do history
|
||||
historico = await get_holmes_history(dados_nota_holmes.id)
|
||||
repo = NotasDespesas(conexao)
|
||||
|
||||
# History traz a hora de cada "Aprovar"; se o Holmes falhar, volta None e grava só Feito / Não Feito
|
||||
historico = await get_holmes_history(dados_nota_holmes.id, conexao)
|
||||
|
||||
try:
|
||||
# O banco é síncrono: roda fora do event loop pra não travar as outras requisições
|
||||
acao = await run_in_threadpool(gravar_nota, dados_nota_holmes, historico)
|
||||
acao = await run_in_threadpool(repo.inserir_nota_despesas, dados_nota_holmes, historico)
|
||||
except ValueError as erro:
|
||||
raise HTTPException(
|
||||
status_code= status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
|
|
|
|||
14
main.py
14
main.py
|
|
@ -1,7 +1,5 @@
|
|||
import json
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from pathlib import Path
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
|
@ -14,20 +12,10 @@ logger = logging.getLogger('api_podium')
|
|||
logger.setLevel(logging.INFO)
|
||||
logger.propagate = False # não repassa pro root, senão a mensagem pode sair duplicada
|
||||
|
||||
formato = logging.Formatter('%(asctime)s | %(levelname)s | %(name)s | %(message)s')
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(formato)
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(name)s | %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
|
||||
# Também grava em api.log (ao lado do main.py), girando a cada 5 MB e guardando os 5 últimos
|
||||
arquivo_log = RotatingFileHandler(
|
||||
Path(__file__).resolve().parent / 'api.log', maxBytes=5_000_000, backupCount=5, encoding='utf-8'
|
||||
)
|
||||
arquivo_log.setFormatter(formato)
|
||||
logger.addHandler(arquivo_log)
|
||||
logging.getLogger('uvicorn.error').addHandler(arquivo_log) # erros 500 com traceback (ex.: pool esgotado)
|
||||
|
||||
app = FastAPI(
|
||||
title = 'API - Podium',
|
||||
version= '0.0.1'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue