mirror of
https://github.com/KeymonSoft/Kelloggs_v4.git
synced 2026-04-19 19:19:14 +00:00
VERSION 6.03.30
- Cambios en DBRequestManagerW para evitar un error cuando no hay internet
This commit is contained in:
@@ -4,167 +4,153 @@ ModulesStructureVersion=1
|
||||
Type=Class
|
||||
Version=12.8
|
||||
@EndOfDesignText@
|
||||
'Class module: jRDC1Wrapper
|
||||
'Version 1.3 - Thread-Safe with ExecuteCommand support
|
||||
'#######################################################################################
|
||||
' Módulo de Clase: jRDC1Wrapper
|
||||
' Versión: 1.9.2 - Thread-Safe, Async Support, B4A Memory Safe, State Persistent & Dynamic Link Support
|
||||
' Autor: Cheve (José Alberto)
|
||||
'
|
||||
' DESCRIPCIÓN:
|
||||
' Este módulo actúa como un "Wrapper" (envoltorio) especializado para DBRequestManager
|
||||
' en entornos jRDC1. Su objetivo principal es modernizar el flujo de trabajo de
|
||||
' versiones antiguas de B4X, permitiendo el uso de la sintaxis "Wait For" en lugar
|
||||
' de depender exclusivamente del evento JobDone global.
|
||||
'
|
||||
' Ejemplo de uso:
|
||||
|
||||
' Dim reqManagerW As DBRequestManagerW
|
||||
' reqManagerW.Initialize
|
||||
' cmd.Initialize
|
||||
' cmd.Name = "selectAlgoDeAlgunLado"
|
||||
' cmd.Parameters = Array As Object(user.Text, pass.Text)
|
||||
' reqManagerW.ExecuteQuery(Starter.DBReqServer, cmd, Me, "loSeleccionado")
|
||||
' Wait For loSeleccionado_Completed (res1 As TResultado)
|
||||
' Log("tag: " & res1.tag & " Success: " & res1.Success)
|
||||
' If res1.Success Then
|
||||
' Subs.logJobDoneResultados(res1.resultado)
|
||||
' Else
|
||||
' Log(res1.ErrorMessage)
|
||||
' End If
|
||||
'#######################################################################################
|
||||
|
||||
Public Sub Class_Globals
|
||||
' Public properties to be accessed after the wait for completes
|
||||
Type TResultado(Tag As String, Success As Boolean, resultado As DBResult, ErrorMessage As String)
|
||||
' Type TCommandResult(Tag As String, Success As Boolean, RowsAffected As Int, ErrorMessage As String)
|
||||
|
||||
'C <<< Definimos un tipo para almacenar la información de cada job
|
||||
Type TJobInfo (Target As Object, EventName As String, IsQuery As Boolean)
|
||||
|
||||
'C <<< Un mapa para mantener un registro de los jobs activos
|
||||
Private activeJobs As Map
|
||||
|
||||
'C <<< Un contador para generar tags únicos para cada job
|
||||
Private jobCounter As Int
|
||||
|
||||
Public reqManager As DBRequestManager
|
||||
Public cmd As DBCommand
|
||||
Public resultado As TResultado
|
||||
' Public commandResult As TCommandResult
|
||||
Private logger As Boolean = False
|
||||
|
||||
' Instancia centralizada.
|
||||
Private reqManager As DBRequestManager
|
||||
|
||||
' Rastreo del EndPoint activo para evitar reinicializaciones redundantes
|
||||
Private currentActiveLink As String = ""
|
||||
End Sub
|
||||
|
||||
'Initializes the object.
|
||||
Public Sub Initialize
|
||||
'C <<< Inicializamos el mapa y el contador
|
||||
activeJobs.Initialize
|
||||
If activeJobs.IsInitialized = False Then
|
||||
activeJobs.Initialize
|
||||
End If
|
||||
jobCounter = 0
|
||||
currentActiveLink = ""
|
||||
End Sub
|
||||
|
||||
'Executes the query using the old jRDC1 mechanism.
|
||||
'Parameters:
|
||||
' - rdcLink: The link for the reqManager initialization.
|
||||
' - Command: The DBCommand to execute.
|
||||
' - Target: The module (like 'Me') where the completed event should be raised.
|
||||
' - Event: The name of the event to raise when the query completes (e.g., "WrapperEvent").
|
||||
Public Sub ExecuteQuery(rdcLink As String, Command As DBCommand, Target As Object, Event As String)
|
||||
'<<< Incrementamos el contador para obtener un ID único
|
||||
jobCounter = jobCounter + 1
|
||||
Dim currentJobTag As String = "reqManagerWJob_" & jobCounter
|
||||
currentJobTag = Event
|
||||
Dim currentJobTag As String = $"reqManagerWJob_${DateTime.Now}_${jobCounter}"$
|
||||
|
||||
If logger Then Log($"ExecuteQuery (Tag: ${currentJobTag}): Command=${Command.Name}, Event=${Event}"$)
|
||||
|
||||
'<<< Creamos una instancia de TJobInfo para guardar el target y el evento
|
||||
' Actualización dinámica de EndPoint solo si el Link cambió
|
||||
If reqManager.IsInitialized = False Or currentActiveLink <> rdcLink Then
|
||||
reqManager.Initialize(Me, rdcLink)
|
||||
currentActiveLink = rdcLink
|
||||
End If
|
||||
|
||||
Dim jobInfo As TJobInfo
|
||||
jobInfo.Initialize
|
||||
jobInfo.Target = Target
|
||||
jobInfo.EventName = Event
|
||||
jobInfo.IsQuery = True ' Mark as query job
|
||||
jobInfo.IsQuery = True
|
||||
|
||||
'<<< Guardamos la información del job en el mapa, usando el tag único como llave
|
||||
activeJobs.Put(currentJobTag, jobInfo)
|
||||
|
||||
reqManager.Initialize(Me, rdcLink)
|
||||
cmd = Command
|
||||
|
||||
'<<< Ejecutamos la consulta pasando nuestro TAG ÚNICO
|
||||
reqManager.ExecuteQuery(cmd, 0, currentJobTag)
|
||||
reqManager.ExecuteQuery(Command, 0, currentJobTag)
|
||||
End Sub
|
||||
|
||||
'Executes a command (INSERT, UPDATE, DELETE) using the old jRDC1 mechanism.
|
||||
'Parameters:
|
||||
' - rdcLink: The link for the reqManager initialization.
|
||||
' - Command: The DBCommand to execute.
|
||||
' - Target: The module (like 'Me') where the completed event should be raised.
|
||||
' - Event: The name of the event to raise when the command completes (e.g., "WrapperEvent").
|
||||
Public Sub ExecuteCommand(rdcLink As String, Command As DBCommand, Target As Object, Event As String)
|
||||
'<<< Incrementamos el contador para obtener un ID único
|
||||
jobCounter = jobCounter + 1
|
||||
Dim currentJobTag As String = "reqManagerWJob_" & jobCounter
|
||||
currentJobTag = Event
|
||||
Dim currentJobTag As String = $"reqManagerWJob_${DateTime.Now}_${jobCounter}"$
|
||||
|
||||
If logger Then Log($"ExecuteCommand (Tag: ${currentJobTag}): Command=${Command.Name}, Event=${Event}"$)
|
||||
|
||||
'<<< Creamos una instancia de TJobInfo para guardar el target y el evento
|
||||
' Actualización dinámica de EndPoint solo si el Link cambió
|
||||
If reqManager.IsInitialized = False Or currentActiveLink <> rdcLink Then
|
||||
reqManager.Initialize(Me, rdcLink)
|
||||
currentActiveLink = rdcLink
|
||||
End If
|
||||
|
||||
Dim jobInfo As TJobInfo
|
||||
jobInfo.Initialize
|
||||
jobInfo.Target = Target
|
||||
jobInfo.EventName = Event
|
||||
jobInfo.IsQuery = False ' Mark as command job
|
||||
jobInfo.IsQuery = False
|
||||
|
||||
'<<< Guardamos la información del job en el mapa, usando el tag único como llave
|
||||
activeJobs.Put(currentJobTag, jobInfo)
|
||||
|
||||
reqManager.Initialize(Me, rdcLink)
|
||||
cmd = Command
|
||||
|
||||
'<<< Ejecutamos el comando pasando nuestro TAG ÚNICO
|
||||
reqManager.ExecuteCommand(cmd, currentJobTag)
|
||||
reqManager.ExecuteCommand(Command, currentJobTag)
|
||||
End Sub
|
||||
|
||||
'This sub will be called by the DBRequestManager when the job is done
|
||||
Public Sub JobDone(job As HttpJob)
|
||||
Log("===== JDDBRW =====")
|
||||
LogColor("JobDone: '" & reqManager.HandleJob(job).tag & "' - Registros: " & reqManager.HandleJob(job).Rows.Size, Colors.Green) 'Mod por CHV - 211110
|
||||
'<<< Obtenemos el Tag único que asignamos al job
|
||||
Dim currentJobTag As String = job.Tag
|
||||
|
||||
'C <<< Verificamos si este job fue iniciado por nuestro wrapper
|
||||
If activeJobs.ContainsKey(currentJobTag) = False Then
|
||||
If logger Then Log($"JobDone: Se recibió un job con un tag desconocido: ${currentJobTag}"$)
|
||||
job.Release
|
||||
Return
|
||||
End If
|
||||
|
||||
'<<< Recuperamos la información específica de este job desde el mapa
|
||||
Dim jobInfo As TJobInfo = activeJobs.Get(currentJobTag)
|
||||
If logger Then Log("===== JDDBRW =====")
|
||||
Dim currentJobTag As String = ""
|
||||
|
||||
Try
|
||||
If jobInfo.IsQuery Then
|
||||
' Handle query result
|
||||
resultado.Initialize
|
||||
resultado.Tag = jobInfo.EventName ' Usamos el nombre del evento original como Tag del resultado
|
||||
|
||||
If job.Success Then
|
||||
Dim dbResult As DBResult = reqManager.HandleJob(job)
|
||||
resultado.Success = True
|
||||
resultado.Resultado = dbResult
|
||||
resultado.ErrorMessage = ""
|
||||
Else
|
||||
resultado.Success = False
|
||||
resultado.Resultado = Null
|
||||
resultado.ErrorMessage = job.ErrorMessage
|
||||
End If
|
||||
job.Release
|
||||
|
||||
'<<< Usamos la información recuperada del mapa para llamar al Sub correcto
|
||||
If logger Then LogColor($"EVENTO: ${jobInfo.EventName}_Completed"$, Colors.Magenta)
|
||||
CallSubDelayed2(jobInfo.Target, jobInfo.EventName & "_Completed", resultado)
|
||||
Else
|
||||
' Handle command result
|
||||
resultado.Initialize
|
||||
resultado.Tag = jobInfo.EventName
|
||||
|
||||
If job.Success Then
|
||||
Dim dbResult As DBResult = reqManager.HandleJob(job)
|
||||
' Dim rowsAffected As Int = reqManager.HandleCommandResult(job)
|
||||
' For Each records() As Object In dbResult.Rows
|
||||
' Dim rowsAffected As Int = records(dbResult.Columns.Get("AffectedRows"))
|
||||
' Next
|
||||
resultado.Success = True
|
||||
resultado.resultado = dbResult
|
||||
resultado.ErrorMessage = ""
|
||||
Else
|
||||
resultado.Success = False
|
||||
resultado.resultado = Null
|
||||
resultado.ErrorMessage = job.ErrorMessage
|
||||
End If
|
||||
job.Release
|
||||
|
||||
'<<< Usamos la información recuperada del mapa para llamar al Sub correcto
|
||||
If logger Then LogColor($"EVENTO: ${jobInfo.EventName}_Completed"$, Colors.Magenta)
|
||||
CallSubDelayed2(jobInfo.Target, jobInfo.EventName & "_Completed", resultado)
|
||||
If job <> Null And job.Tag <> Null And job.Tag Is String Then
|
||||
currentJobTag = job.Tag
|
||||
End If
|
||||
|
||||
If activeJobs.ContainsKey(currentJobTag) Then
|
||||
|
||||
Dim jobInfo As TJobInfo = activeJobs.Get(currentJobTag)
|
||||
Dim res As TResultado
|
||||
res.Initialize
|
||||
res.Tag = jobInfo.EventName
|
||||
|
||||
If job.Success Then
|
||||
Dim dbResult As DBResult = reqManager.HandleJob(job)
|
||||
If logger Then LogColor("JobDone: '" & dbResult.tag & "' - Registros: " & dbResult.Rows.Size, Colors.Green)
|
||||
res.Success = True
|
||||
res.resultado = dbResult
|
||||
res.ErrorMessage = ""
|
||||
Else
|
||||
res.Success = False
|
||||
res.resultado = Null
|
||||
res.ErrorMessage = job.ErrorMessage
|
||||
End If
|
||||
|
||||
activeJobs.Remove(currentJobTag)
|
||||
|
||||
If logger Then LogColor($"EVENTO: ${jobInfo.EventName}_Completed"$, Colors.Magenta)
|
||||
CallSubDelayed2(jobInfo.Target, jobInfo.EventName & "_Completed", res)
|
||||
|
||||
Else
|
||||
If logger Then Log($"JobDone: Job descartado (Tag desconocido o nulo): ${currentJobTag}"$)
|
||||
End If
|
||||
|
||||
'C <<< Se remueve el job del mapa en caso de ÉXITO
|
||||
activeJobs.Remove(currentJobTag)
|
||||
Catch
|
||||
If logger Then LogColor("Error en jRDC1Wrapper.JobDone: " & LastException, Colors.Red)
|
||||
'<<< MUY IMPORTANTE: Remover el job del mapa para no tener fugas de memoria
|
||||
activeJobs.Remove(currentJobTag)
|
||||
If logger Then LogColor("Error Crítico en jRDC1Wrapper.JobDone: " & LastException, Colors.Red)
|
||||
|
||||
If currentJobTag <> "" And activeJobs.ContainsKey(currentJobTag) Then
|
||||
Dim jobInfo As TJobInfo = activeJobs.Get(currentJobTag)
|
||||
Dim resErr As TResultado
|
||||
resErr.Initialize
|
||||
resErr.Tag = jobInfo.EventName
|
||||
resErr.Success = False
|
||||
resErr.ErrorMessage = "Error interno procesando la respuesta DB: " & LastException.Message
|
||||
|
||||
activeJobs.Remove(currentJobTag)
|
||||
CallSubDelayed2(jobInfo.Target, jobInfo.EventName & "_Completed", resErr)
|
||||
End If
|
||||
End Try
|
||||
|
||||
If job <> Null Then job.Release
|
||||
End Sub
|
||||
Reference in New Issue
Block a user