From cb9c917ec1b55792425fd89958d9fe7efbf5ffef Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Mon, 30 Nov 2026 08:43:04 -0700 Subject: [PATCH] fix(db): resolve queue persistence regressions --- interceptors/LogFailedJobsInterceptor.cfc | 6 +- models/Jobs/DBBatchRepository.cfc | 10 ++- models/Providers/AbstractQueueProvider.cfc | 10 +++ models/Providers/BoxLangQueuePoll.bx | 12 ++- models/Providers/DBProvider.cfc | 12 +++ ..._01_000012_widen_failed_job_exceptions.cfc | 20 +++++ .../integration/OpenIssueRegressionSpec.cfc | 84 +++++++++++++++++++ 7 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 resources/database/migrations/2000_01_01_000012_widen_failed_job_exceptions.cfc diff --git a/interceptors/LogFailedJobsInterceptor.cfc b/interceptors/LogFailedJobsInterceptor.cfc index 02470ef..e7435b2 100644 --- a/interceptors/LogFailedJobsInterceptor.cfc +++ b/interceptors/LogFailedJobsInterceptor.cfc @@ -46,19 +46,19 @@ component { }, "exceptionType" : { "value" : arguments.data.exception.type ?: "", - "cfsqltype" : "CF_SQL_VARCHAR", + "cfsqltype" : "CF_SQL_LONGVARCHAR", "null" : ( arguments.data.exception.type ?: "" ) == "", "nulls" : ( arguments.data.exception.type ?: "" ) == "" }, "exceptionMessage" : { "value" : arguments.data.exception.message ?: "", - "cfsqltype" : "CF_SQL_VARCHAR", + "cfsqltype" : "CF_SQL_LONGVARCHAR", "null" : ( arguments.data.exception.message ?: "" ) == "", "nulls" : ( arguments.data.exception.message ?: "" ) == "" }, "exceptionDetail" : { "value" : arguments.data.exception.detail ?: "", - "cfsqltype" : "CF_SQL_VARCHAR", + "cfsqltype" : "CF_SQL_LONGVARCHAR", "null" : ( arguments.data.exception.detail ?: "" ) == "", "nulls" : ( arguments.data.exception.detail ?: "" ) == "" }, diff --git a/models/Jobs/DBBatchRepository.cfc b/models/Jobs/DBBatchRepository.cfc index 9ef40a6..63ae721 100644 --- a/models/Jobs/DBBatchRepository.cfc +++ b/models/Jobs/DBBatchRepository.cfc @@ -84,12 +84,16 @@ component singleton accessors="true" { } public void function incrementTotalJobs( required string id, required numeric count ) { - qb.table( variables.batchTableName ) + var builder = qb.table( variables.batchTableName ); + var grammar = builder.getGrammar(); + var totalJobsColumn = grammar.wrapValue( "totalJobs" ); + var pendingJobsColumn = grammar.wrapValue( "pendingJobs" ); + builder .where( "id", arguments.id ) .update( values = { - "totalJobs" : qb.raw( "totalJobs + #count#" ), - "pendingJobs" : qb.raw( "pendingJobs + #count#" ), + "totalJobs" : qb.raw( "#totalJobsColumn# + #arguments.count#" ), + "pendingJobs" : qb.raw( "#pendingJobsColumn# + #arguments.count#" ), "completedDate" : { "value" : 0, "null" : true } }, options = variables.defaultQueryOptions diff --git a/models/Providers/AbstractQueueProvider.cfc b/models/Providers/AbstractQueueProvider.cfc index 61b3eae..93b076c 100644 --- a/models/Providers/AbstractQueueProvider.cfc +++ b/models/Providers/AbstractQueueProvider.cfc @@ -66,6 +66,16 @@ component accessors="true" { numeric currentAttempt ) { var config = deserializeJSON( arguments.payload ); + if ( + !config.keyExists( "mapping" ) || isNull( config.mapping ) || !isSimpleValue( config.mapping ) || trim( + config.mapping + ) == "" + ) { + throw( + type = "cbq.InvalidJobMapping", + message = "Job ###arguments.jobId# has no usable mapping and cannot be executed." + ); + } if ( !variables.wirebox.containsInstance( config.mapping ) ) { throw( diff --git a/models/Providers/BoxLangQueuePoll.bx b/models/Providers/BoxLangQueuePoll.bx index 8672b70..be65a39 100644 --- a/models/Providers/BoxLangQueuePoll.bx +++ b/models/Providers/BoxLangQueuePoll.bx @@ -5,7 +5,17 @@ class { var settings = getApplicationMetadata(); var applicationName = settings.name; var pollSettings = {}; - for ( var key in [ "mappings", "datasource", "defaultDatasource", "datasources", "javaSettings" ] ) { + for ( + var key in [ + "mappings", + "datasource", + "defaultDatasource", + "datasources", + "javaSettings", + "ormEnabled", + "ormSettings" + ] + ) { if ( settings.keyExists( key ) ) { pollSettings[ key ] = duplicate( settings[ key ] ); } diff --git a/models/Providers/DBProvider.cfc b/models/Providers/DBProvider.cfc index a3dfec6..33b6b7d 100644 --- a/models/Providers/DBProvider.cfc +++ b/models/Providers/DBProvider.cfc @@ -573,6 +573,18 @@ component accessors="true" extends="AbstractQueueProvider" { arguments.record.id, arguments.record.attempts ); + } catch ( cbq.InvalidJobMapping e ) { + var invalidJob = variables.wirebox.getInstance( "NonExecutableJob@cbq" ); + invalidJob.applyMemento( deserializeJSON( arguments.record.payload ) ); + invalidJob.setId( arguments.record.id ); + invalidJob.setMapping( "[missing mapping]" ); + invalidJob.setCurrentAttempt( arguments.record.attempts ); + invalidJob.setProviderContext( { + "attempt" : arguments.record.attempts, + "awaitingExecution" : true + } ); + markJobFailed( invalidJob, arguments.pool, e ); + return; } catch ( cbq.UnknownJobMapping e ) { // No execution has started. Release only this fetched reservation so // another worker can handle it, without consuming an attempt. diff --git a/resources/database/migrations/2000_01_01_000012_widen_failed_job_exceptions.cfc b/resources/database/migrations/2000_01_01_000012_widen_failed_job_exceptions.cfc new file mode 100644 index 0000000..5526c28 --- /dev/null +++ b/resources/database/migrations/2000_01_01_000012_widen_failed_job_exceptions.cfc @@ -0,0 +1,20 @@ +/** Preserve complete exception diagnostics in failed-job records. */ +component { + + function up( schema ) { + schema.alter( "cbq_failed_jobs", function( t ) { + t.modifyColumn( "exceptionType", t.longText( "exceptionType" ).nullable() ); + t.modifyColumn( "exceptionMessage", t.longText( "exceptionMessage" ) ); + t.modifyColumn( "exceptionDetail", t.longText( "exceptionDetail" ).nullable() ); + } ); + } + + function down( schema ) { + schema.alter( "cbq_failed_jobs", function( t ) { + t.modifyColumn( "exceptionType", t.string( "exceptionType" ).nullable() ); + t.modifyColumn( "exceptionMessage", t.string( "exceptionMessage" ) ); + t.modifyColumn( "exceptionDetail", t.string( "exceptionDetail" ).nullable() ); + } ); + } + +} diff --git a/tests/specs/integration/OpenIssueRegressionSpec.cfc b/tests/specs/integration/OpenIssueRegressionSpec.cfc index bbd47f4..19cdba5 100644 --- a/tests/specs/integration/OpenIssueRegressionSpec.cfc +++ b/tests/specs/integration/OpenIssueRegressionSpec.cfc @@ -2,6 +2,90 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" { function run() { describe( "reported configuration regressions", function() { + it( "fails a poison database job with no mapping instead of blocking its queue", function() { + var provider = getInstance( "DBProvider@cbq" ).setProperties( {} ); + var cbqSettings = getInstance( "coldbox:moduleSettings:cbq" ); + var originalLogFailedJobs = cbqSettings.logFailedJobs; + makePublic( provider, "processLockedRecord" ); + var pool = getInstance( "WorkerPool@cbq" ).setName( "InvalidMapping" ); + var queue = "invalid-mapping-#createUUID()#"; + provider.push( queue, getInstance( "@cbq" ).job( "MissingOnThisWorker" ) ); + var row = provider + .newQuery() + .from( "cbq_jobs" ) + .where( "queue", queue ) + .first(); + var payload = deserializeJSON( row.payload ); + payload.mapping = javacast( "null", "" ); + provider + .newQuery() + .from( "cbq_jobs" ) + .where( "id", row.id ) + .update( { + "payload" : serializeJSON( payload ), + "reservedBy" : pool.getUniqueId() + } ); + row.payload = serializeJSON( payload ); + try { + cbqSettings.logFailedJobs = true; + provider.processLockedRecord( row, pool ); + } finally { + cbqSettings.logFailedJobs = originalLogFailedJobs; + } + var after = provider + .newQuery() + .from( "cbq_jobs" ) + .where( "id", row.id ) + .first(); + expect( after.failedDate ?: "" ).notToBe( "" ); + expect( after.attempts ).toBe( 0 ); + expect( + getInstance( "QueryBuilder@qb" ) + .from( "cbq_failed_jobs" ) + .where( "originalId", toString( row.id ) ) + .where( "exceptionType", "cbq.InvalidJobMapping" ) + .count() + ).toBe( 1 ); + } ); + + it( "logs exception diagnostics longer than 255 characters", function() { + var cbqSettings = getInstance( "coldbox:moduleSettings:cbq" ); + var originalLogFailedJobs = cbqSettings.logFailedJobs; + var originalId = createUUID(); + var longValue = repeatString( "diagnostic-", 40 ); + var job = getInstance( "@cbq" ) + .job( "MissingOnThisWorker" ) + .setId( originalId ) + .onConnection( "db" ) + .onQueue( "long-diagnostics" ); + try { + cbqSettings.logFailedJobs = true; + controller + .getInterceptorService() + .announce( + "onCBQJobFailed", + { + "job" : job, + "exception" : { + "type" : longValue, + "message" : longValue, + "detail" : longValue, + "stackTrace" : longValue + } + } + ); + } finally { + cbqSettings.logFailedJobs = originalLogFailedJobs; + } + var failedLog = getInstance( "QueryBuilder@qb" ) + .from( "cbq_failed_jobs" ) + .where( "originalId", originalId ) + .first(); + expect( failedLog.exceptionType ).toBe( longValue ); + expect( failedLog.exceptionMessage ).toBe( longValue ); + expect( failedLog.exceptionDetail ).toBe( longValue ); + } ); + it( "stores batches with no optional lifecycle jobs", function() { var batch = getInstance( "@cbq" ).batch( [] ); var options = batch.getOptions();