Modified copy and delete record to sql routines to lock the queue record before attempting to prevent long running copy or delete calls form being processed more than once and wasting resources.

This commit is contained in:
Infineon\StieberD
2025-07-03 14:03:39 -07:00
parent 5ca3778719
commit 98ac420462
5 changed files with 354 additions and 320 deletions

View File

@ -22,98 +22,107 @@ Compile function Copy_Record_To_SQL(Table, Key, LogError, pKey)
09/23/2010 KRF Added logic to set @DICT, @ID, and @RECORD so handlers
can use symbolics
\*****************************************************************************/
$insert Microsoft_Ado_Equates
If Assigned(LogError) else LogError = 0
$Insert APP_INSERTS
$Insert MICROSOFT_ADO_EQUATES
Declare subroutine SRP_Com, Sql_Services
Declare function SRP_Com, Environment_Services, Datetime, Sql_Services, Error_Services, Database_Services
Ans = ""
Declare function SRP_Com, Environment_Services, Datetime, Sql_Services, Error_Services, Database_Services, Unassigned
// Make sure table is uppercase
Convert @LOWER_CASE to @UPPER_CASE in Table
Main:
If Unassigned(LogError) then LogError = False$
TablesToSkip = Database_Services('ReadDataRow', 'APP_INFO', 'SQL_TABLES_TO_SKIP')
Locate Table in TablesToSkip by "AL" using "," setting ListPos else
// The expected name of the handler
Handler = "COPY_":Table:"_RECORD_TO_SQL"
// Find the stored procedure that handles this table
Open "SYSOBJ" to hSysObj then
Read ObjCode from hSysObj, "$":Handler:"*LSL2" then
// Connect to the ODBC
If SRP_Com(Connection, "CREATE", "ADODB.Connection") then
ConnectionString = Environment_Services('GetSQLScrapeConnectionString')
If SRP_Com(Connection, "CALL", "Open", ConnectionString) EQ "" then
// Read the record and call the handler
Open Table to hTable then
Read @RECORD from hTable, Key then
If Key NE "" then
Open "DICT.":Table to @DICT then null
@ID = Key
Ans = Function(@Handler(Connection, Key, @RECORD))
If Ans EQ '' then
If Assigned(pKey) then
If pKey NE '' then
Open 'SQL_PENDING' to pTable then
Delete pTable, pKey else null
Ans = ""
Done = False$
// Make sure table is uppercase
Convert @LOWER_CASE to @UPPER_CASE in Table
TablesToSkip = Database_Services('ReadDataRow', 'APP_INFO', 'SQL_TABLES_TO_SKIP')
Locate Table in TablesToSkip by "AL" using "," setting ListPos else
Open 'SQL_PENDING' to pTable then
If pKey NE '' then Lock pTable, pKey else Done = True$
If Not(Done) then
// The expected name of the handler
Handler = "COPY_":Table:"_RECORD_TO_SQL"
// Find the stored procedure that handles this table
Open "SYSOBJ" to hSysObj then
Read ObjCode from hSysObj, "$":Handler:"*LSL2" then
// Connect to the ODBC
If SRP_Com(Connection, "CREATE", "ADODB.Connection") then
ConnectionString = Environment_Services('GetSQLScrapeConnectionString')
If SRP_Com(Connection, "CALL", "Open", ConnectionString) EQ "" then
// Read the record and call the handler
Open Table to hTable then
Read @RECORD from hTable, Key then
If Key NE "" then
Open "DICT.":Table to @DICT then null
@ID = Key
Ans = Function(@Handler(Connection, Key, @RECORD))
If Ans EQ '' then
If Assigned(pKey) then
If pKey NE '' then
Delete pTable, pKey else Null
Unlock pTable, pKey else Null
end
end
end
end
end
end
end
end else
Ans = 'Unable to read ':Key:' from ':Table:' table.'
end
end
end else
Ans = 'Unable to open connection to SQL server. SQL connection error: ':SRP_Com(Connection, "ERROR")
end
SRP_Com(Connection, "CALL", "Close")
SRP_Com(Connection, "RELEASE")
end else
Ans = "Unable to create ADO connection."
end
end
If LogError and Len(Ans) then
Open "SQL_ERROR" to hSqlError then
ErrorKey = OConv(Date(), "D4-J")
Timestamp = OConv(Datetime(), 'DT')
Read Errors from hSqlError, ErrorKey then
Errors := @FM:Table:@VM:Key:@VM:Timestamp:@VM:Ans
Write Errors to hSqlError, ErrorKey
end else
Errors = Table:@VM:Key:@VM:Timestamp:@VM:Ans
Write Errors to hSqlError, ErrorKey
end
end
end
If Assigned(pKey) then
If (pKey NE '') then
// Always log the result
Open 'SQL_LOG' to hLog then
If Ans EQ '' then
Result = 'PROCESSED'
end else
Result = Ans
end else
Ans = 'Unable to read ':Key:' from ':Table:' table.'
end
end
end else
Ans = 'Unable to open connection to SQL server. SQL connection error: '
Ans := SRP_Com(Connection, "ERROR")
end
SRP_Com(Connection, "CALL", "Close")
SRP_Com(Connection, "RELEASE")
end else
Ans = "Unable to create ADO connection."
end
end
CurrDate = Date()
Read Log from hLog, CurrDate then
Log := @FM:pKey:@VM:Result
end else
Log = pKey:@VM:Result
If LogError and Len(Ans) then
Open "SQL_ERROR" to hSqlError then
ErrorKey = OConv(Date(), "D4-J")
Timestamp = OConv(Datetime(), 'DT')
Read Errors from hSqlError, ErrorKey then
Errors := @FM:Table:@VM:Key:@VM:Timestamp:@VM:Ans
Write Errors to hSqlError, ErrorKey
end else
Errors = Table:@VM:Key:@VM:Timestamp:@VM:Ans
Write Errors to hSqlError, ErrorKey
end
end
end
If Assigned(pKey) then
If (pKey NE '') then
// Always log the result
Open 'SQL_LOG' to hLog then
If Ans EQ '' then
Result = 'PROCESSED'
end else
Result = Ans
end
CurrDate = Date()
Read Log from hLog, CurrDate then
Log := @FM:pKey:@VM:Result
end else
Log = pKey:@VM:Result
end
Write Log to hLog, CurrDate
end
end
end
Write Log to hLog, CurrDate
end
end
end
end
end
end
end
Return Ans