mirror of
https://github.com/KeymonSoft/Kelloggs_v4.git
synced 2026-04-17 18:26:11 +00:00
- Cambios en DBRequestManagerW para evitar un error cuando no hay internet
156 lines
5.0 KiB
QBasic
156 lines
5.0 KiB
QBasic
B4A=true
|
|
Group=Default Group
|
|
ModulesStructureVersion=1
|
|
Type=Class
|
|
Version=12.8
|
|
@EndOfDesignText@
|
|
'#######################################################################################
|
|
' 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
|
|
Type TResultado(Tag As String, Success As Boolean, resultado As DBResult, ErrorMessage As String)
|
|
Type TJobInfo (Target As Object, EventName As String, IsQuery As Boolean)
|
|
|
|
Private activeJobs As Map
|
|
Private jobCounter As Int
|
|
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
|
|
|
|
Public Sub Initialize
|
|
If activeJobs.IsInitialized = False Then
|
|
activeJobs.Initialize
|
|
End If
|
|
jobCounter = 0
|
|
currentActiveLink = ""
|
|
End Sub
|
|
|
|
Public Sub ExecuteQuery(rdcLink As String, Command As DBCommand, Target As Object, Event As String)
|
|
jobCounter = jobCounter + 1
|
|
Dim currentJobTag As String = $"reqManagerWJob_${DateTime.Now}_${jobCounter}"$
|
|
|
|
If logger Then Log($"ExecuteQuery (Tag: ${currentJobTag}): Command=${Command.Name}, Event=${Event}"$)
|
|
|
|
' 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
|
|
|
|
activeJobs.Put(currentJobTag, jobInfo)
|
|
reqManager.ExecuteQuery(Command, 0, currentJobTag)
|
|
End Sub
|
|
|
|
Public Sub ExecuteCommand(rdcLink As String, Command As DBCommand, Target As Object, Event As String)
|
|
jobCounter = jobCounter + 1
|
|
Dim currentJobTag As String = $"reqManagerWJob_${DateTime.Now}_${jobCounter}"$
|
|
|
|
If logger Then Log($"ExecuteCommand (Tag: ${currentJobTag}): Command=${Command.Name}, Event=${Event}"$)
|
|
|
|
' 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
|
|
|
|
activeJobs.Put(currentJobTag, jobInfo)
|
|
reqManager.ExecuteCommand(Command, currentJobTag)
|
|
End Sub
|
|
|
|
Public Sub JobDone(job As HttpJob)
|
|
If logger Then Log("===== JDDBRW =====")
|
|
Dim currentJobTag As String = ""
|
|
|
|
Try
|
|
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
|
|
|
|
Catch
|
|
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 |