mirror of
https://github.com/KeymonSoft/jRDC-Multi.git
synced 2026-04-17 21:06:24 +00:00
- VERSION 5.09.17
- fix(handlers, logs): Reporte robusto de AffectedRows (simbólico) y limpieza de tabla de errores - Aborda dos problemas críticos para la estabilidad y fiabilidad del servidor: el manejo del conteo de filas afectadas en DMLs y la gestión del crecimiento de la tabla de logs de errores. - Cambios Principales: 1. Fix AffectedRows (ExecuteBatch V1 y DBHandlerJSON): Dada la imposibilidad de capturar el conteo de filas afectadas real (Null) de forma segura o la falla total en tiempo de ejecución (Method: ExecNonQuery2 not matched) al usar reflexión, se revierte la lógica a la llamada directa de ExecNonQuery2. Si el comando DML se ejecuta sin lanzar una excepción SQL, se reporta simbólicamente '1' fila afectada al cliente (en el Protocolo V1 y en la respuesta JSON para executecommand) para confirmar el éxito de la operación. 2. Limpieza de Tabla de Errores: Se corrigió la subrutina Main.borraArribaDe15000Logs para incluir la tabla `errores` en la limpieza periódica. Esto asegura que el log de errores no crezca indefinidamente, manteniendo solo los 15,000 registros más recientes y realizando la optimización de espacio en disco con `vacuum`.
This commit is contained in:
657
Manager.bas
657
Manager.bas
@@ -2,7 +2,7 @@
|
||||
Group=Default Group
|
||||
ModulesStructureVersion=1
|
||||
Type=Class
|
||||
Version=8.8
|
||||
Version=10.3
|
||||
@EndOfDesignText@
|
||||
' Módulo de clase: Manager
|
||||
' Este handler proporciona un panel de administración web para el servidor jRDC2-Multi.
|
||||
@@ -24,398 +24,291 @@ End Sub
|
||||
' Método principal que maneja las peticiones HTTP para el panel de administración.
|
||||
' req: El objeto ServletRequest que contiene la información de la petición entrante.
|
||||
' resp: El objeto ServletResponse para construir y enviar la respuesta al cliente.
|
||||
' Módulo de clase: Manager
|
||||
' ... (tu código de Class_Globals e Initialize se queda igual) ...
|
||||
|
||||
' Método principal que maneja las peticiones HTTP para el panel de administración.
|
||||
' Refactorizado para funcionar como una API con un frontend estático.
|
||||
Sub Handle(req As ServletRequest, resp As ServletResponse)
|
||||
' --- 1. Bloque de Seguridad: Autenticación de Usuario ---
|
||||
' Verifica si el usuario actual ha iniciado sesión y está autorizado.
|
||||
' Si no está autorizado, se le redirige a la página de login.
|
||||
' --- 1. Bloque de Seguridad (sin cambios) ---
|
||||
If req.GetSession.GetAttribute2("user_is_authorized", False) = False Then
|
||||
resp.SendRedirect("/login")
|
||||
Return ' Termina la ejecución si no está autorizado.
|
||||
Return
|
||||
End If
|
||||
|
||||
' Obtiene el comando solicitado de los parámetros de la URL (ej. "?command=reload").
|
||||
Dim Command As String = req.GetParameter("command")
|
||||
If Command = "" Then Command = "ping" ' Si no se especifica un comando, por defecto es "ping".
|
||||
|
||||
Log($"Command: ${Command}"$)
|
||||
|
||||
' --- MANEJO ESPECIAL PARA SNAPSHOT ---
|
||||
' El comando "snapshot" no devuelve HTML, sino una imagen. Lo manejamos por separado al principio.
|
||||
If Command = "snapshot" Then
|
||||
' Try
|
||||
' resp.ContentType = "image/png"
|
||||
' Dim robot, toolkit As JavaObject
|
||||
' robot.InitializeNewInstance("java.awt.Robot", Null)
|
||||
' toolkit.InitializeStatic("java.awt.Toolkit")
|
||||
' Dim screenRect As JavaObject
|
||||
' screenRect.InitializeNewInstance("java.awt.Rectangle", Array As Object( _
|
||||
' toolkit.RunMethodJO("getDefaultToolkit", Null).RunMethod("getScreenSize", Null)))
|
||||
' Dim image As JavaObject = robot.RunMethod("createScreenCapture", Array As Object(screenRect))
|
||||
' Dim ImageIO As JavaObject
|
||||
' ImageIO.InitializeStatic("javax.imageio.ImageIO").RunMethod("write", Array As Object(image, "png", resp.OutputStream))
|
||||
' Catch
|
||||
' resp.SendError(500, LastException.Message)
|
||||
' End Try
|
||||
' Return ' Detenemos la ejecución aquí para no enviar más HTML.
|
||||
' --- 2. Servidor de la Página Principal ---
|
||||
' Si NO se especifica un comando, servimos la página principal del manager desde la carpeta 'www'.
|
||||
If Command = "" Then
|
||||
Try
|
||||
resp.ContentType = "text/html; charset=utf-8"
|
||||
resp.Write(File.ReadString(File.DirApp, "www/manager.html"))
|
||||
Catch
|
||||
resp.SendError(500, "Error: No se pudo encontrar el archivo principal del panel (www/manager.html). " & LastException.Message)
|
||||
End Try
|
||||
Return
|
||||
End If
|
||||
' --- FIN DE MANEJO ESPECIAL ---
|
||||
|
||||
' Para todos los demás comandos, construimos la página HTML de respuesta.
|
||||
resp.ContentType = "text/html" ' Establece el tipo de contenido como HTML.
|
||||
Dim sb As StringBuilder ' Usamos StringBuilder para construir eficientemente el HTML.
|
||||
sb.Initialize
|
||||
|
||||
' --- Estilos y JavaScript (igual que antes) ---
|
||||
sb.Append("<html><head><style>")
|
||||
sb.Append("body {font-family: sans-serif; margin: 2em; background-color: #f9f9f9;} ")
|
||||
sb.Append("h1, h2 {color: #333;} hr {margin: 2em 0; border: 0; border-top: 1px solid #ddd;} ")
|
||||
sb.Append("input {display: block; width: 95%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 4px;} ")
|
||||
sb.Append("button {padding: 10px 15px; border: none; background-color: #007bff; color: white; cursor: pointer; border-radius: 4px; margin-right: 1em;} ")
|
||||
sb.Append(".nav a, .logout a {text-decoration: none; margin-right: 10px; color: #007bff;} ")
|
||||
sb.Append(".output {background: #fff; padding: 1em; border: 1px solid #eee; border-radius: 8px; font-family: monospace; white-space: pre-wrap; word-wrap: break-word;} ")
|
||||
sb.Append("#changePassForm {background: #f0f0f0; padding: 1.5em; border-radius: 8px; max-width: 400px; margin-top: 1em;} ")
|
||||
sb.Append("</style>")
|
||||
sb.Append("<script>function toggleForm() {var form = document.getElementById('changePassForm'); if (form.style.display === 'none') {form.style.display = 'block';} else {form.style.display = 'none';}}</script>")
|
||||
sb.Append("</head><body>")
|
||||
|
||||
' --- Cabecera de la Página y Mensaje de Bienvenida ---
|
||||
sb.Append("<h1>Panel de Administración jRDC</h1>")
|
||||
sb.Append($"<p>Bienvenido, <strong>${req.GetSession.GetAttribute("username")}</strong></p>"$)
|
||||
|
||||
' --- Menú de Navegación del Manager ---
|
||||
' Este menú incluye las opciones para interactuar con el servidor.
|
||||
sb.Append("<div class='menu'>")
|
||||
sb.Append("<a href='/manager?command=test'>Test</a> | ")
|
||||
sb.Append("<a href='/manager?command=ping'>Ping</a> | ")
|
||||
sb.Append("<a href='/manager?command=reload'>Reload</a> | ")
|
||||
sb.Append("<a href='/manager?command=slowqueries'>Queries Lentas</a> | ") ' Nuevo enlace para queries lentas.
|
||||
sb.Append("<a href='/manager?command=totalcon'>Estadísticas Pool</a> | ") ' Nuevo enlace para estadísticas del pool.
|
||||
sb.Append("<a href='/manager?command=rpm2'>Reiniciar (pm2)</a> | ")
|
||||
sb.Append("<a href='/manager?command=reviveBow'>Revive Bow</a>")
|
||||
sb.Append("</div>")
|
||||
sb.Append("<hr>")
|
||||
|
||||
sb.Append("<div id='changePassForm' style='display:none;'>")
|
||||
sb.Append("<h2>Cambiar Contraseña</h2><form action='/changepass' method='post'>")
|
||||
sb.Append("Contraseña Actual: <input type='password' name='current_password' required><br>")
|
||||
sb.Append("Nueva Contraseña: <input type='password' name='new_password' required><br>")
|
||||
sb.Append("Confirmar Nueva Contraseña: <input type='password' name='confirm_password' required><br>")
|
||||
sb.Append("<button type='submit'>Actualizar Contraseña</button> <button onclick='toggleForm()'>Cancelar</button></form></div>")
|
||||
|
||||
' --- Resultado del Comando ---
|
||||
sb.Append("<h2>Resultado del Comando: '" & Command & "'</h2>")
|
||||
sb.Append("<div class='output'>")
|
||||
|
||||
' =========================================================================
|
||||
' ### INICIO DE TU LÓGICA DE COMANDOS INTEGRADA ###
|
||||
' =========================================================================
|
||||
If Command = "reload" Then
|
||||
|
||||
Dim sbTemp As StringBuilder
|
||||
sbTemp.Initialize
|
||||
sbTemp.Append($"Iniciando recarga de configuración (Hot-Swap) ($DateTime{DateTime.Now})"$).Append(" " & CRLF)
|
||||
|
||||
' <<< PASO CLAVE 1: DETENER TIMER DE LOGS (ZONA SEGURA DE SQLite) >>>
|
||||
' Detenemos el timer incondicionalmente al inicio para evitar conflictos de bloqueo con SQLite
|
||||
' durante la inicialización de conectores.
|
||||
Dim oldTimerState As Boolean = Main.timerLogs.Enabled
|
||||
If oldTimerState Then
|
||||
Main.timerLogs.Enabled = False
|
||||
sbTemp.Append(" -> Timer de limpieza de logs (SQLite) detenido temporalmente.").Append(" " & CRLF)
|
||||
End If
|
||||
' --- 3. Manejo de Comandos como API ---
|
||||
' La variable 'j' (JSONGenerator) está en Class_Globals
|
||||
|
||||
' 1. Crear un nuevo mapa temporal para almacenar los conectores recién inicializados.
|
||||
Dim newConnectors As Map
|
||||
newConnectors.Initialize
|
||||
|
||||
Dim oldConnectors As Map
|
||||
Dim reloadSuccessful As Boolean = True
|
||||
|
||||
' *** INICIO DEL BLOQUE CRÍTICO 1: Obtener oldConnectors con ReentrantLock ***
|
||||
Dim lock1Acquired As Boolean = False
|
||||
Try
|
||||
Main.MainConnectorsLock.RunMethod("lock", Null)
|
||||
lock1Acquired = True
|
||||
oldConnectors = Main.Connectors
|
||||
Catch
|
||||
sbTemp.Append($" -> ERROR CRÍTICO: No se pudo adquirir el bloqueo para leer conectores antiguos: ${LastException.Message}"$).Append(" " & CRLF)
|
||||
reloadSuccessful = False
|
||||
End Try
|
||||
|
||||
If lock1Acquired Then
|
||||
Main.MainConnectorsLock.RunMethod("unlock", Null)
|
||||
End If
|
||||
|
||||
If Not(reloadSuccessful) Then
|
||||
' Si el bloqueo inicial falló, restauramos el Timer al estado anterior y salimos.
|
||||
If oldTimerState Then Main.timerLogs.Enabled = True
|
||||
sb.Append(sbTemp.ToString)
|
||||
sb.Append($"¡ERROR: La recarga de configuración falló en la fase de bloqueo inicial! Los conectores antiguos siguen activos."$).Append(" " & CRLF)
|
||||
Return
|
||||
End If
|
||||
|
||||
' 2. Iterar sobre las bases de datos configuradas y crear *nuevas* instancias de RDCConnector.
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
|
||||
Try
|
||||
Dim newRDC As RDCConnector
|
||||
newRDC.Initialize(dbKey) ' Inicializa la nueva instancia con la configuración fresca.
|
||||
|
||||
' <<< PASO CLAVE 2: LEER Y ALMACENAR EL NUEVO ESTADO DE LOGS PARA CADA DB >>>
|
||||
' Leemos la configuración 'enableSQLiteLogs' de esta DBkey.
|
||||
Dim enableLogsSetting As Int = newRDC.config.GetDefault("enableSQLiteLogs", 0).As(Int)
|
||||
Dim isEnabled As Boolean = (enableLogsSetting = 1)
|
||||
|
||||
' Almacenamos el estado temporalmente en el mapa newConnectors bajo una clave única.
|
||||
newConnectors.Put(dbKey & "_LOG_STATE", isEnabled)
|
||||
sbTemp.Append($" -> Logs de ${dbKey} activados: ${isEnabled}"$).Append(" " & CRLF)
|
||||
' <<< FIN PASO CLAVE 2 >>>
|
||||
|
||||
newConnectors.Put(dbKey, newRDC)
|
||||
|
||||
Dim newPoolStats As Map = newRDC.GetPoolStats
|
||||
sbTemp.Append($" -> ${dbKey}: Nuevo conector inicializado. Conexiones: ${newPoolStats.Get("TotalConnections")}"$).Append(" " & CRLF)
|
||||
|
||||
Catch
|
||||
sbTemp.Append($" -> ERROR CRÍTICO al inicializar nuevo conector para ${dbKey}: ${LastException.Message}"$).Append(" " & CRLF)
|
||||
reloadSuccessful = False
|
||||
Exit ' Si uno falla, abortamos la recarga.
|
||||
End Try
|
||||
|
||||
Next
|
||||
|
||||
sb.Append(sbTemp.ToString)
|
||||
|
||||
If reloadSuccessful Then
|
||||
Select Command.ToLowerCase
|
||||
|
||||
' 3. Si todo fue exitoso, hacemos el Hot-Swap atómico.
|
||||
' *** INICIO DEL BLOQUE CRÍTICO 2: Reemplazar Main.Connectors con ReentrantLock ***
|
||||
|
||||
Dim lock2Acquired As Boolean = False
|
||||
Try
|
||||
Main.MainConnectorsLock.RunMethod("lock", Null)
|
||||
lock2Acquired = True
|
||||
Main.Connectors = newConnectors ' Reemplazamos el mapa de conectores completo por el nuevo.
|
||||
Catch
|
||||
sb.Append($" -> ERROR CRÍTICO: No se pudo adquirir el bloqueo para reemplazar conectores: ${LastException.Message}"$).Append(" " & CRLF)
|
||||
reloadSuccessful = False
|
||||
End Try
|
||||
|
||||
If lock2Acquired Then
|
||||
Main.MainConnectorsLock.RunMethod("unlock", Null)
|
||||
End If
|
||||
|
||||
If reloadSuccessful Then ' Si el swap fue exitoso
|
||||
|
||||
' <<< PASO CLAVE 3: APLICAR EL NUEVO ESTADO GLOBAL GRANULAR Y REINICIAR TIMER >>>
|
||||
|
||||
' 3a. Reemplazar el mapa de estados de logging granular
|
||||
Main.SQLiteLoggingStatusByDB.Clear ' Limpiamos el mapa global
|
||||
|
||||
Dim isAnyEnabled As Boolean = False
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
' Recuperamos el estado logueado temporalmente.
|
||||
Dim isEnabled As Boolean = newConnectors.Get(dbKey & "_LOG_STATE").As(Boolean)
|
||||
Main.SQLiteLoggingStatusByDB.Put(dbKey, isEnabled) ' Aplicamos el estado al mapa global
|
||||
|
||||
If isEnabled Then isAnyEnabled = True ' Calculamos el flag general
|
||||
Next
|
||||
|
||||
' 3b. Controlar el Timer y el flag global
|
||||
Main.IsAnySQLiteLoggingEnabled = isAnyEnabled ' Actualizamos el flag global
|
||||
|
||||
If Main.IsAnySQLiteLoggingEnabled Then
|
||||
Main.timerLogs.Enabled = True
|
||||
sb.Append($" -> Logs de SQLite HABILITADOS (Granular). Timer de limpieza ACTIVADO."$).Append(" " & CRLF)
|
||||
Else
|
||||
Main.timerLogs.Enabled = False
|
||||
sb.Append($" -> Logs de SQLite DESHABILITADOS (Total). Timer de limpieza PERMANECERÁ DETENIDO."$).Append(" " & CRLF)
|
||||
End If
|
||||
' <<< FIN PASO CLAVE 3 >>>
|
||||
|
||||
sb.Append($"¡Recarga de configuración completada con éxito (Hot-Swap)!"$).Append(" " & CRLF)
|
||||
|
||||
' ... (Resto del código: Mostrar estado de pools y Cierre explícito de oldConnectors) ...
|
||||
|
||||
Else
|
||||
sb.Append($"¡ERROR: La recarga de configuración falló en la fase de reemplazo de conectores! Los conectores antiguos siguen activos."$).Append(" " & CRLF)
|
||||
End If
|
||||
|
||||
Else ' Falla en inicialización (Punto 2)
|
||||
|
||||
' Si falla la recarga, restauramos el Timer al estado anterior.
|
||||
If oldTimerState Then
|
||||
Main.timerLogs.Enabled = True
|
||||
sb.Append(" -> Restaurando Timer de limpieza de logs (SQLite) al estado ACTIVO debido a fallo en recarga.").Append(" " & CRLF)
|
||||
End If
|
||||
|
||||
sb.Append($"¡ERROR: La recarga de configuración falló durante la inicialización de nuevos conectores! Los conectores antiguos siguen activos."$).Append(" " & CRLF)
|
||||
End If
|
||||
|
||||
Else If Command = "slowqueries" Then ' <<< INICIO: NUEVA Lógica para mostrar las queries lentas
|
||||
sb.Append("<h2 style=""margin-top:0px;margin-bottom:0px;"">Consultas Lentas Recientes</h2>")
|
||||
sb.Append("(Este registro depende de que los logs estén habilitados con del parámetro ""enableSQLiteLogs=1"" en config properties)<br><br>")
|
||||
Try
|
||||
' 1. Calcular el límite de tiempo: el tiempo actual (en milisegundos) menos 1 hora (3,600,000 ms).
|
||||
Dim oneHourAgoMs As Long = DateTime.Now - 3600000
|
||||
|
||||
' Ajusta la consulta SQL para obtener las 20 queries más lentas.
|
||||
' Utilizamos datetime con 'unixepoch' y 'localtime' para una visualización legible del timestamp.
|
||||
Dim rs As ResultSet = Main.SQL1.ExecQuery($"SELECT query_name, duration_ms, datetime(timestamp / 1000, 'unixepoch', 'localtime') as timestamp_local, db_key, client_ip, busy_connections, handler_active_requests FROM query_logs WHERE timestamp >= ${oneHourAgoMs} ORDER BY duration_ms DESC LIMIT 20"$)
|
||||
|
||||
sb.Append("<table border='1' style='width:100%; text-align:left; border-collapse: collapse;'>")
|
||||
sb.Append("<thead><tr><th>Query</th><th>Duración (ms)</th><th>Fecha/Hora Local</th><th>DB Key</th><th>Cliente IP</th><th>Conex. Ocupadas</th><th>Peticiones Activas</th></tr></thead>")
|
||||
sb.Append("<tbody>")
|
||||
|
||||
Do While rs.NextRow
|
||||
sb.Append("<tr>")
|
||||
sb.Append($"<td>${rs.GetString("query_name")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetLong("duration_ms")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetString("timestamp_local")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetString("db_key")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetString("client_ip")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetInt("busy_connections")}</td>"$)
|
||||
sb.Append($"<td>${rs.GetInt("handler_active_requests")}</td>"$)
|
||||
sb.Append("</tr>")
|
||||
Loop
|
||||
sb.Append("</tbody>")
|
||||
sb.Append("</table>")
|
||||
rs.Close
|
||||
Catch
|
||||
Log("Error al obtener queries lentas en Manager: " & LastException.Message)
|
||||
sb.Append($"<p style='color:red;'>Error al cargar queries lentas: ${LastException.Message}</p>"$)
|
||||
End Try
|
||||
Else If Command = "test" Then
|
||||
Try
|
||||
Dim con As SQL = Main.Connectors.Get("DB1").As(RDCConnector).GetConnection("")
|
||||
sb.Append("Connection successful.</br></br>")
|
||||
Private estaDB As String = ""
|
||||
Log(Main.listaDeCP)
|
||||
For i = 0 To Main.listaDeCP.Size - 1
|
||||
If Main.listaDeCP.get(i) <> "" Then estaDB = "." & Main.listaDeCP.get(i)
|
||||
sb.Append($"Using config${estaDB}.properties<br/>"$)
|
||||
Next
|
||||
con.Close
|
||||
Catch
|
||||
resp.Write("Error fetching connection.")
|
||||
End Try
|
||||
Else If Command = "stop" Then
|
||||
' Public shl As Shell...
|
||||
Else If Command = "rsx" Then
|
||||
Log($"Ejecutamos ${File.DirApp}\start.bat"$)
|
||||
sb.Append($"Ejecutamos ${File.DirApp}\start.bat"$)
|
||||
Public shl As Shell
|
||||
shl.Initialize("shl","cmd",Array("/c",File.DirApp & "\start.bat " & Main.srvr.Port))
|
||||
shl.WorkingDirectory = File.DirApp
|
||||
shl.Run(-1)
|
||||
Else If Command = "rpm2" Then
|
||||
Log($"Ejecutamos ${File.DirApp}\reiniciaProcesoPM2.bat"$)
|
||||
sb.Append($"Ejecutamos ${File.DirApp}\reiniciaProcesoPM2.bat"$)
|
||||
Public shl As Shell
|
||||
shl.Initialize("shl","cmd",Array("/c",File.DirApp & "\reiniciaProcesoPM2.bat " & Main.srvr.Port))
|
||||
shl.WorkingDirectory = File.DirApp
|
||||
shl.Run(-1)
|
||||
Else If Command = "reviveBow" Then
|
||||
Log($"Ejecutamos ${File.DirApp}\reiniciaProcesoBow.bat"$)
|
||||
sb.Append($"Ejecutamos ${File.DirApp}\reiniciaProcesoBow.bat<br><br>"$)
|
||||
sb.Append($"!!!BOW REINICIANDO!!!"$)
|
||||
Public shl As Shell
|
||||
shl.Initialize("shl","cmd",Array("/c",File.DirApp & "\reiniciaProcesoBow.bat " & Main.srvr.Port))
|
||||
shl.WorkingDirectory = File.DirApp
|
||||
shl.Run(-1)
|
||||
Else If Command = "paused" Then
|
||||
GlobalParameters.IsPaused = 1
|
||||
sb.Append("Servidor pausado.")
|
||||
Else If Command = "continue" Then
|
||||
GlobalParameters.IsPaused = 0
|
||||
sb.Append("Servidor reanudado.")
|
||||
Else If Command = "logs" Then
|
||||
If GlobalParameters.mpLogs.IsInitialized Then
|
||||
j.Initialize(GlobalParameters.mpLogs)
|
||||
sb.Append(j.ToString)
|
||||
End If
|
||||
Else If Command = "block" Then
|
||||
Dim BlockedConIP As String = req.GetParameter("IP")
|
||||
If GlobalParameters.mpBlockConnection.IsInitialized Then
|
||||
GlobalParameters.mpBlockConnection.Put(BlockedConIP, BlockedConIP)
|
||||
sb.Append("IP bloqueada: " & BlockedConIP)
|
||||
End If
|
||||
Else If Command = "unblock" Then
|
||||
Dim UnBlockedConIP As String = req.GetParameter("IP")
|
||||
If GlobalParameters.mpBlockConnection.IsInitialized Then
|
||||
GlobalParameters.mpBlockConnection.Remove(UnBlockedConIP)
|
||||
sb.Append("IP desbloqueada: " & UnBlockedConIP)
|
||||
End If
|
||||
Else If Command = "restartserver" Then
|
||||
Log($"Ejecutamos ${File.DirApp}/restarServer.bat"$)
|
||||
sb.Append("Reiniciando servidor...")
|
||||
Else If Command = "runatstartup" Then
|
||||
File.Copy("C:\jrdcinterface", "startup.bat", "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp", "startup.bat")
|
||||
sb.Append("Script de inicio añadido.")
|
||||
Else If Command = "stoprunatstartup" Then
|
||||
File.Delete("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp", "startup.bat")
|
||||
sb.Append("Script de inicio eliminado.")
|
||||
Else If Command = "totalrequests" Then
|
||||
If GlobalParameters.mpTotalRequests.IsInitialized Then
|
||||
j.Initialize(GlobalParameters.mpTotalRequests)
|
||||
sb.Append(j.ToString)
|
||||
End If
|
||||
Else If Command = "totalblocked" Then
|
||||
If GlobalParameters.mpBlockConnection.IsInitialized Then
|
||||
' j.Initialize(Global.mpBlockConnection)
|
||||
sb.Append(j.ToString)
|
||||
End If
|
||||
Else If Command = "ping" Then
|
||||
sb.Append($"Pong ($DateTime{DateTime.Now})"$)
|
||||
Else If Command = "totalcon" Then ' <<< Modificado: Ahora usa GetPoolStats para cada pool
|
||||
' Verificamos que el mapa global de conexiones esté inicializado.
|
||||
' Aunque no lo poblamos directamente, es un buen chequeo de estado.
|
||||
If GlobalParameters.mpTotalConnections.IsInitialized Then
|
||||
sb.Append("<h2>Estadísticas del Pool de Conexiones por DB:</h2>")
|
||||
|
||||
' Creamos un mapa LOCAL para almacenar las estadísticas de TODOS los pools de conexiones.
|
||||
' --- Comandos que devuelven JSON ---
|
||||
Case "getstats"
|
||||
resp.ContentType = "application/json; charset=utf-8"
|
||||
Dim allPoolStats As Map
|
||||
allPoolStats.Initialize
|
||||
|
||||
' Iteramos sobre cada clave de base de datos que tenemos configurada (DB1, DB2, etc.).
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
' Obtenemos el conector RDC para la base de datos actual.
|
||||
Dim connector As RDCConnector = Main.Connectors.Get(dbKey).As(RDCConnector)
|
||||
|
||||
' Si el conector no está inicializado (lo cual no debería ocurrir si Main.AppStart funcionó),
|
||||
' registramos un error y pasamos al siguiente.
|
||||
If connector.IsInitialized = False Then
|
||||
Log($"Manager: ADVERTENCIA: El conector para ${dbKey} no está inicializado."$)
|
||||
Dim errorMap As Map = CreateMap("Error": "Conector no inicializado o no cargado correctamente")
|
||||
allPoolStats.Put(dbKey, errorMap)
|
||||
Continue ' Salta a la siguiente iteración del bucle.
|
||||
End If
|
||||
|
||||
' Llamamos al método GetPoolStats del conector para obtener las métricas de su pool.
|
||||
Dim poolStats As Map = connector.GetPoolStats
|
||||
|
||||
' Añadimos las estadísticas de este pool (poolStats) al mapa general (allPoolStats),
|
||||
' usando la clave de la base de datos (dbKey) como su identificador.
|
||||
allPoolStats.Put(dbKey, poolStats)
|
||||
Next
|
||||
|
||||
' Inicializamos el generador JSON con el mapa 'allPoolStats' (que ahora sí debería contener datos).
|
||||
' (La variable 'j' ya está declarada en Class_Globals de Manager.bas, no la declares de nuevo aquí).
|
||||
j.Initialize(allPoolStats)
|
||||
|
||||
' Añadimos la representación JSON de las estadísticas al StringBuilder para la respuesta HTML.
|
||||
sb.Append(j.ToString)
|
||||
Else
|
||||
sb.Append("El mapa de conexiones GlobalParameters.mpTotalConnections no está inicializado.")
|
||||
End If
|
||||
End If
|
||||
' =========================================================================
|
||||
' ### FIN DE TU LÓGICA DE COMANDOS ###
|
||||
' =========================================================================
|
||||
|
||||
' --- Cerramos la página y la enviamos ---
|
||||
sb.Append("</div><p class='logout'><a href='/logout'>Cerrar Sesión</a> | <a href=# onclick='toggleForm()'>Cambiar Contraseña</a></p></body></html>")
|
||||
resp.Write(sb.ToString)
|
||||
|
||||
If GlobalParameters.mpLogs.IsInitialized Then GlobalParameters.mpLogs.Put(Command, "Manager : " & Command & " - Time : " & DateTime.Time(DateTime.Now))
|
||||
End Sub
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
Dim connector As RDCConnector = Main.Connectors.Get(dbKey)
|
||||
If connector.IsInitialized Then
|
||||
allPoolStats.Put(dbKey, connector.GetPoolStats)
|
||||
Else
|
||||
allPoolStats.Put(dbKey, CreateMap("Error": "Conector no inicializado"))
|
||||
End If
|
||||
Next
|
||||
|
||||
j.Initialize(allPoolStats)
|
||||
resp.Write(j.ToString)
|
||||
Return
|
||||
|
||||
Case "slowqueries"
|
||||
resp.ContentType = "application/json; charset=utf-8"
|
||||
Dim results As List
|
||||
results.Initialize
|
||||
Try
|
||||
' Verificamos si la tabla de logs existe antes de consultarla
|
||||
Dim tableExists As Boolean = Main.SQL1.ExecQuerySingleResult($"SELECT name FROM sqlite_master WHERE type='table' AND name='query_logs';"$) <> Null
|
||||
If tableExists = False Then
|
||||
' Si la tabla no existe, devolvemos un JSON con un mensaje claro y terminamos.
|
||||
j.Initialize(CreateMap("message": "La tabla de logs ('query_logs') no existe. Habilita 'enableSQLiteLogs=1' en la configuración."))
|
||||
resp.Write(j.ToString)
|
||||
Return
|
||||
End If
|
||||
|
||||
' La tabla existe, procedemos con la consulta original
|
||||
Dim oneHourAgoMs As Long = DateTime.Now - 3600000
|
||||
Dim rs As ResultSet = Main.SQL1.ExecQuery($"SELECT query_name, duration_ms, datetime(timestamp / 1000, 'unixepoch', 'localtime') as timestamp_local, db_key, client_ip, busy_connections, handler_active_requests FROM query_logs WHERE timestamp >= ${oneHourAgoMs} ORDER BY duration_ms DESC LIMIT 20"$)
|
||||
|
||||
Do While rs.NextRow
|
||||
Dim row As Map
|
||||
row.Initialize
|
||||
row.Put("Query", rs.GetString("query_name"))
|
||||
row.Put("Duracion_ms", rs.GetLong("duration_ms"))
|
||||
row.Put("Fecha_Hora", rs.GetString("timestamp_local"))
|
||||
row.Put("DB_Key", rs.GetString("db_key"))
|
||||
row.Put("Cliente_IP", rs.GetString("client_ip"))
|
||||
row.Put("Conexiones_Ocupadas", rs.GetInt("busy_connections"))
|
||||
row.Put("Peticiones_Activas", rs.GetInt("handler_active_requests"))
|
||||
results.Add(row)
|
||||
Loop
|
||||
rs.Close
|
||||
|
||||
' 1. Creamos un mapa "raíz" para contener nuestra lista.
|
||||
Dim root As Map
|
||||
root.Initialize
|
||||
root.Put("data", results) ' La llave puede ser lo que quieras, "data" es común.
|
||||
|
||||
' 2. Ahora sí, inicializamos el generador con el mapa raíz.
|
||||
j.Initialize(root)
|
||||
resp.Write(j.ToString)
|
||||
|
||||
Catch
|
||||
Log("Error CRÍTICO al obtener queries lentas en Manager API: " & LastException.Message)
|
||||
|
||||
' <<< CORRECCIÓN AQUÍ >>>
|
||||
' Se utiliza la propiedad .Status para asignar el código de error
|
||||
resp.Status = 500 ' Internal Server Error
|
||||
|
||||
' 1. Creamos un mapa "raíz" para contener nuestra lista.
|
||||
Dim root As Map
|
||||
root.Initialize
|
||||
root.Put("data", results) ' La llave puede ser lo que quieras, "data" es común.
|
||||
|
||||
' 2. Ahora sí, inicializamos el generador con el mapa raíz.
|
||||
j.Initialize(root)
|
||||
resp.Write(j.ToString)
|
||||
End Try
|
||||
Return
|
||||
|
||||
Case "logs", "totalrequests", "totalblocked"
|
||||
resp.ContentType = "application/json; charset=utf-8"
|
||||
Dim mp As Map
|
||||
If Command = "logs" And GlobalParameters.mpLogs.IsInitialized Then mp = GlobalParameters.mpLogs
|
||||
If Command = "totalrequests" And GlobalParameters.mpTotalRequests.IsInitialized Then mp = GlobalParameters.mpTotalRequests
|
||||
If Command = "totalblocked" And GlobalParameters.mpBlockConnection.IsInitialized Then mp = GlobalParameters.mpBlockConnection
|
||||
|
||||
If mp.IsInitialized Then
|
||||
j.Initialize(mp)
|
||||
resp.Write(j.ToString)
|
||||
Else
|
||||
resp.Write("{}")
|
||||
End If
|
||||
Return
|
||||
|
||||
' --- Comandos que devuelven TEXTO PLANO ---
|
||||
Case "ping"
|
||||
resp.ContentType = "text/plain"
|
||||
resp.Write($"Pong ($DateTime{DateTime.Now})"$)
|
||||
Return
|
||||
|
||||
Case "reload"
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
Dim sbTemp As StringBuilder
|
||||
sbTemp.Initialize
|
||||
|
||||
' <<< LÓGICA ORIGINAL: Se mantiene intacta toda la lógica de recarga >>>
|
||||
' (Copiada y pegada directamente de tu código anterior)
|
||||
sbTemp.Append($"Iniciando recarga de configuración (Hot-Swap) ($DateTime{DateTime.Now})"$).Append(" " & CRLF)
|
||||
Dim oldTimerState As Boolean = Main.timerLogs.Enabled
|
||||
If oldTimerState Then
|
||||
Main.timerLogs.Enabled = False
|
||||
sbTemp.Append(" -> Timer de limpieza de logs (SQLite) detenido temporalmente.").Append(" " & CRLF)
|
||||
End If
|
||||
Dim newConnectors As Map
|
||||
newConnectors.Initialize
|
||||
Dim oldConnectors As Map
|
||||
Dim reloadSuccessful As Boolean = True
|
||||
Main.MainConnectorsLock.RunMethod("lock", Null)
|
||||
oldConnectors = Main.Connectors
|
||||
Main.MainConnectorsLock.RunMethod("unlock", Null)
|
||||
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
Try
|
||||
Dim newRDC As RDCConnector
|
||||
newRDC.Initialize(dbKey)
|
||||
Dim enableLogsSetting As Int = newRDC.config.GetDefault("enableSQLiteLogs", 0)
|
||||
Dim isEnabled As Boolean = (enableLogsSetting = 1)
|
||||
newConnectors.Put(dbKey & "_LOG_STATE", isEnabled)
|
||||
sbTemp.Append($" -> Logs de ${dbKey} activados: ${isEnabled}"$).Append(" " & CRLF)
|
||||
newConnectors.Put(dbKey, newRDC)
|
||||
Dim newPoolStats As Map = newRDC.GetPoolStats
|
||||
sbTemp.Append($" -> ${dbKey}: Nuevo conector inicializado. Conexiones: ${newPoolStats.Get("TotalConnections")}"$).Append(" " & CRLF)
|
||||
Catch
|
||||
sbTemp.Append($" -> ERROR CRÍTICO al inicializar nuevo conector para ${dbKey}: ${LastException.Message}"$).Append(" " & CRLF)
|
||||
reloadSuccessful = False
|
||||
Exit
|
||||
End Try
|
||||
Next
|
||||
|
||||
If reloadSuccessful Then
|
||||
Main.MainConnectorsLock.RunMethod("lock", Null)
|
||||
Main.Connectors = newConnectors
|
||||
Main.MainConnectorsLock.RunMethod("unlock", Null)
|
||||
Main.SQLiteLoggingStatusByDB.Clear
|
||||
Dim isAnyEnabled As Boolean = False
|
||||
For Each dbKey As String In Main.listaDeCP
|
||||
Dim isEnabled As Boolean = newConnectors.Get(dbKey & "_LOG_STATE")
|
||||
Main.SQLiteLoggingStatusByDB.Put(dbKey, isEnabled)
|
||||
If isEnabled Then isAnyEnabled = True
|
||||
Next
|
||||
Main.IsAnySQLiteLoggingEnabled = isAnyEnabled
|
||||
If Main.IsAnySQLiteLoggingEnabled Then
|
||||
Main.timerLogs.Enabled = True
|
||||
sbTemp.Append($" -> Logs de SQLite HABILITADOS (Granular). Timer de limpieza ACTIVADO."$).Append(" " & CRLF)
|
||||
Else
|
||||
Main.timerLogs.Enabled = False
|
||||
sbTemp.Append($" -> Logs de SQLite DESHABILITADOS (Total). Timer de limpieza PERMANECERÁ DETENIDO."$).Append(" " & CRLF)
|
||||
End If
|
||||
sbTemp.Append($"¡Recarga de configuración completada con éxito (Hot-Swap)!"$).Append(" " & CRLF)
|
||||
Else
|
||||
If oldTimerState Then
|
||||
Main.timerLogs.Enabled = True
|
||||
sbTemp.Append(" -> Restaurando Timer de limpieza de logs (SQLite) al estado ACTIVO debido a fallo en recarga.").Append(" " & CRLF)
|
||||
End If
|
||||
sbTemp.Append($"¡ERROR: La recarga de configuración falló! Los conectores antiguos siguen activos."$).Append(" " & CRLF)
|
||||
End If
|
||||
|
||||
' <<< CAMBIO: Se devuelve el contenido del StringBuilder como texto plano >>>
|
||||
resp.Write(sbTemp.ToString)
|
||||
Return
|
||||
|
||||
Case "test"
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
Dim sb As StringBuilder
|
||||
sb.Initialize
|
||||
Try
|
||||
Dim con As SQL = Main.Connectors.Get("DB1").As(RDCConnector).GetConnection("")
|
||||
sb.Append("Connection successful." & CRLF & CRLF)
|
||||
Dim estaDB As String = ""
|
||||
Log(Main.listaDeCP)
|
||||
For i = 0 To Main.listaDeCP.Size - 1
|
||||
If Main.listaDeCP.get(i) <> "" Then estaDB = "." & Main.listaDeCP.get(i)
|
||||
sb.Append($"Using config${estaDB}.properties"$ & CRLF)
|
||||
Next
|
||||
con.Close
|
||||
resp.Write(sb.ToString)
|
||||
Catch
|
||||
resp.Write("Error fetching connection: " & LastException.Message)
|
||||
End Try
|
||||
Return
|
||||
|
||||
Case "rsx", "rpm2", "revivebow", "restartserver"
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
Dim batFile As String
|
||||
Select Command
|
||||
Case "rsx": batFile = "start.bat"
|
||||
Case "rpm2": batFile = "reiniciaProcesoPM2.bat"
|
||||
Case "reviveBow": batFile = "reiniciaProcesoBow.bat"
|
||||
Case "restartserver": batFile = "restarServer.bat"
|
||||
End Select
|
||||
Log($"Ejecutando ${File.DirApp}\${batFile}"$)
|
||||
Try
|
||||
Dim shl As Shell
|
||||
shl.Initialize("shl","cmd", Array("/c", File.DirApp & "\" & batFile & " " & Main.srvr.Port))
|
||||
shl.WorkingDirectory = File.DirApp
|
||||
shl.Run(-1)
|
||||
resp.Write($"Comando '${Command}' ejecutado. Script invocado: ${batFile}"$)
|
||||
Catch
|
||||
resp.Write($"Error al ejecutar el script para '${Command}': ${LastException.Message}"$)
|
||||
End Try
|
||||
Return
|
||||
|
||||
|
||||
Log($"Ejecutamos ${File.DirApp}\reiniciaProcesoPM2.bat"$)
|
||||
sb.Append($"Ejecutamos ${File.DirApp}\reiniciaProcesoPM2.bat"$)
|
||||
Public shl As Shell
|
||||
shl.Initialize("shl","cmd",Array("/c",File.DirApp & "\reiniciaProcesoPM2.bat " & Main.srvr.Port))
|
||||
shl.WorkingDirectory = File.DirApp
|
||||
shl.Run(-1)
|
||||
|
||||
Case "paused", "continue"
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
If Command = "paused" Then
|
||||
GlobalParameters.IsPaused = 1
|
||||
resp.Write("Servidor pausado.")
|
||||
Else
|
||||
GlobalParameters.IsPaused = 0
|
||||
resp.Write("Servidor reanudado.")
|
||||
End If
|
||||
Return
|
||||
|
||||
Case "block", "unblock"
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
Dim ip As String = req.GetParameter("IP")
|
||||
If ip = "" Then
|
||||
resp.Write("Error: El parámetro IP es requerido.")
|
||||
Return
|
||||
End If
|
||||
If GlobalParameters.mpBlockConnection.IsInitialized Then
|
||||
If Command = "block" Then
|
||||
GlobalParameters.mpBlockConnection.Put(ip, ip)
|
||||
resp.Write($"IP bloqueada: ${ip}"$)
|
||||
Else
|
||||
GlobalParameters.mpBlockConnection.Remove(ip)
|
||||
resp.Write($"IP desbloqueada: ${ip}"$)
|
||||
End If
|
||||
Else
|
||||
resp.Write("Error: El mapa de bloqueo no está inicializado.")
|
||||
End If
|
||||
Return
|
||||
|
||||
Case Else
|
||||
resp.ContentType = "text/plain; charset=utf-8"
|
||||
resp.SendError(404, $"Comando desconocido: '{Command}'"$)
|
||||
Return
|
||||
|
||||
End Select
|
||||
End Sub
|
||||
Reference in New Issue
Block a user