melhorias basicas e log
This commit is contained in:
35
app/main.py
35
app/main.py
@@ -1,4 +1,6 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from nicegui import ui, app
|
||||
from ui import layout, dashboard, settings, manual_tools
|
||||
from database import init_db
|
||||
@@ -13,17 +15,33 @@ init_db()
|
||||
bot = TelegramManager()
|
||||
watcher = DirectoryWatcher(bot)
|
||||
|
||||
# --- LIFECYCLE ---
|
||||
# --- CONFIGURAÇÃO DE LOG REATIVA ---
|
||||
class NiceGuiLogHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
try:
|
||||
msg = self.format(record)
|
||||
# Enviamos para o estado que o Dashboard lê
|
||||
state.log(msg)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
async def startup():
|
||||
# 1. Inicia o Watcher (Ele começa pausado, safe)
|
||||
asyncio.create_task(watcher.start())
|
||||
# CONFIGURAR LOGS AQUI (Depois que o servidor iniciou)
|
||||
root_logger = logging.getLogger()
|
||||
|
||||
# 2. Inicia o Bot com atraso e em background
|
||||
# Isso evita que a falha de conexão trave a UI
|
||||
# Adiciona o handler que manda para o Dashboard
|
||||
ui_handler = NiceGuiLogHandler()
|
||||
ui_handler.setFormatter(logging.Formatter('%(message)s'))
|
||||
root_logger.addHandler(ui_handler)
|
||||
|
||||
logging.info("🚀 Dashboard conectado aos logs do sistema.")
|
||||
|
||||
# Inicia os serviços
|
||||
asyncio.create_task(watcher.start())
|
||||
asyncio.create_task(delayed_bot_start())
|
||||
|
||||
async def delayed_bot_start():
|
||||
print("⏳ Aguardando rede estabilizar (5s)...")
|
||||
logging.info("⏳ Aguardando rede estabilizar (5s)...")
|
||||
await asyncio.sleep(5)
|
||||
await bot.start()
|
||||
|
||||
@@ -33,7 +51,7 @@ async def shutdown():
|
||||
app.on_startup(startup)
|
||||
app.on_shutdown(shutdown)
|
||||
|
||||
# --- ROTAS ---
|
||||
# --- ROTAS (Permanecem iguais) ---
|
||||
@ui.page('/')
|
||||
def index_page():
|
||||
ui.colors(primary='#5898d4', secondary='#263238')
|
||||
@@ -56,4 +74,5 @@ def explorer_page():
|
||||
manual_tools.show()
|
||||
|
||||
if __name__ in {"__main__", "__mp_main__"}:
|
||||
ui.run(title='Clei-Flow', port=8080, storage_secret='clei-secret', reload=True)
|
||||
# reload=False é essencial em Docker para evitar processos duplicados
|
||||
ui.run(title='Clei-Flow', port=8080, storage_secret='clei-secret', reload=False)
|
||||
Reference in New Issue
Block a user