Skip to content

Commit

Permalink
Fix for errors being thrown in shutdown when LogBox doesn't start up …
Browse files Browse the repository at this point in the history
…correctly
  • Loading branch information
elpete committed Dec 4, 2024
1 parent 568f663 commit 238f669
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 58 deletions.
66 changes: 49 additions & 17 deletions system/cache/CacheFactory.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,27 @@ component accessors=true serializable=false {
*/
CacheFactory function shutdown(){
// Log startup
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutdown of cache factory: #getFactoryID()# requested and started." );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutdown of cache factory: #getFactoryID()# requested and started." );
}
}

// Notify Listeners
variables.eventManager.announce( "beforeCacheFactoryShutdown", { cacheFactory : this } );
if ( !isNull( variables.eventManager ) ) {
variables.eventManager.announce( "beforeCacheFactoryShutdown", { cacheFactory : this } );
}

// safely iterate and shutdown caches
getCacheNames().each( function( item ){
// Get cache to shutdown
var cache = getCache( item );

// Log it
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutting down cache: #item# on factoryID: #getFactoryID()#." );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutting down cache: #item# on factoryID: #getFactoryID()#." );
}
}

// process listeners
Expand All @@ -377,8 +383,10 @@ component accessors=true serializable=false {
variables.eventManager.announce( "afterCacheShutdown", { cache : cache } );

// log
if ( variables.log.canDebug() ) {
variables.log.debug( "Cache: #item# was shut down on factoryID: #getFactoryID()#." );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Cache: #item# was shut down on factoryID: #getFactoryID()#." );
}
}
} );

Expand All @@ -389,19 +397,27 @@ component accessors=true serializable=false {
removeFromScope();

// Shutdown LogBox and Executors if not in ColdBox Mode or WireBox mode
if ( !isObject( variables.coldbox ) && !isObject( variables.wirebox ) ) {
if (
!isNull( variables.coldbox ) && !isObject( variables.coldbox ) && !isNull( variables.wirebox ) && !isObject(
variables.wirebox
)
) {
if ( isObject( variables.logBox ) ) {
variables.logBox.shutdown();
}
variables.asyncManager.shutdownAllExecutors( force = true );
}

// Notify Listeners
variables.eventManager.announce( "afterCacheFactoryShutdown", { cacheFactory : this } );
if ( !isNull( variables.eventManager ) ) {
variables.eventManager.announce( "afterCacheFactoryShutdown", { cacheFactory : this } );
}

// Log shutdown complete
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutdown of cache factory: #getFactoryID()# completed." );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Shutdown of cache factory: #getFactoryID()# completed." );
}
}

return this;
Expand Down Expand Up @@ -438,7 +454,9 @@ component accessors=true serializable=false {
}

// Notify Listeners
variables.eventManager.announce( "beforeCacheShutdown", { cache : cache } );
if ( !isNull( variables.eventManager ) ) {
variables.eventManager.announce( "beforeCacheShutdown", { cache : cache } );
}

// Shutdown the cache
cache.shutdown();
Expand All @@ -463,6 +481,10 @@ component accessors=true serializable=false {
* Remove the cache factory from scope registration if enabled, else does nothing
*/
CacheFactory function removeFromScope(){
if ( isNull( variables.config ) ) {
return this;
}

var scopeInfo = variables.config.getScopeRegistration();
if ( scopeInfo.enabled ) {
new coldbox.system.core.collections.ScopeStorage().delete( scopeInfo.key, scopeInfo.scope );
Expand Down Expand Up @@ -524,17 +546,21 @@ component accessors=true serializable=false {
* Remove all the registered caches in this factory, this triggers individual cache shutdowns
*/
CacheFactory function removeAll(){
if ( variables.log.canDebug() ) {
variables.log.debug( "Removal of all caches requested on factoryID: #getFactoryID()#" );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "Removal of all caches requested on factoryID: #getFactoryID()#" );
}
}

getCacheNames().each( function( item ){
removeCache( item );
} );


if ( variables.log.canDebug() ) {
variables.log.debug( "All caches removed." );
if ( !isNull( variables.log ) ) {
if ( variables.log.canDebug() ) {
variables.log.debug( "All caches removed." );
}
}

return this;
Expand Down Expand Up @@ -640,8 +666,14 @@ component accessors=true serializable=false {
* Get the array of caches registered with this factory
*/
array function getCacheNames(){
lock name="#variables.lockName#" type="readonly" timeout="20" throwontimeout="true" {
if ( !isNull( variables.lockName ) ) {
lock name="#variables.lockName#" type="readonly" timeout="20" throwontimeout="true" {
return structKeyArray( variables.caches );
}
} else if ( !isNull( variables.caches ) ) {
return structKeyArray( variables.caches );
} else {
return [];
}
}

Expand Down
80 changes: 48 additions & 32 deletions system/ioc/Injector.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,22 @@ component serializable="false" accessors="true" {
var iData = { injector : this };

// Log
if ( variables.log.canInfo() ) {
if ( !isNull( variables.log ) && variables.log.canInfo() ) {
variables.log.info( "Shutdown of Injector: #getInjectorID()# requested and started." );
}

// Notify Listeners
variables.eventManager.announce( "beforeInjectorShutdown", iData );
if ( !isNull( variables.eventManager ) ) {
variables.eventManager.announce( "beforeInjectorShutdown", iData );
}

// Check if binder has onShutdown convention
if ( structKeyExists( variables.binder, "onShutdown" ) ) {
if ( !isNull( variables.binder ) && structKeyExists( variables.binder, "onShutdown" ) ) {
variables.binder.onShutdown( this );
}

// Do we have children?
if ( structCount( variables.childInjectors ) ) {
if ( !isNull( variables.childInjectors ) && structCount( variables.childInjectors ) ) {
variables.childInjectors.each( function( childName, childInstance ){
arguments.childInstance.shutdown( this );
} );
Expand All @@ -440,24 +442,28 @@ component serializable="false" accessors="true" {
}

// Remove from scope
removeFromScope();
if ( !isNull( variables.binder ) ) {
removeFromScope();
}

// Notify Listeners
variables.eventManager.announce( "afterInjectorShutdown", iData );
if ( !isNull( variables.eventManager ) ) {
variables.eventManager.announce( "afterInjectorShutdown", iData );
}

// Log shutdown complete
if ( variables.log.canInfo() ) {
if ( !isNull( variables.log ) && variables.log.canInfo() ) {
variables.log.info( "Shutdown of injector: #getInjectorID()# completed." );
}

// Shutdown LogBox last if not in ColdBox Mode
if ( !isColdBoxLinked() ) {
if ( !isColdBoxLinked() && !isNull( variables.logBox ) ) {
variables.logBox.shutdown();
}

// Shutdown Executors if not in ColdBox Mode
// This needs to happen AFTER logbox is shutdown since they share the taskScheduler
if ( !isColdBoxLinked() ) {
if ( !isColdBoxLinked() && !isNull( variables.asyncManager ) ) {
variables.asyncManager.shutdownAllExecutors( force = true );
}

Expand Down Expand Up @@ -547,9 +553,11 @@ component serializable="false" accessors="true" {
}

// We could not find it
variables.log.error(
"Requested instance:#arguments.name# was not located in any declared scan location(s): #structKeyList( variables.binder.getScanLocations() )#, or by path or by hierarchy."
);
if ( !isNull( variables.log ) && variables.log.canError() ) {
variables.log.error(
"Requested instance:#arguments.name# was not located in any declared scan location(s): #structKeyList( variables.binder.getScanLocations() )#, or by path or by hierarchy."
);
}
throw(
message : "Instance not found: '#arguments.name#'",
detail : "The instance could not be located in any declared scan location(s) (#structKeyList( variables.binder.getScanLocations() )#) or full path location or parent or children",
Expand Down Expand Up @@ -670,7 +678,7 @@ component serializable="false" accessors="true" {
}

// log data
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Instance object built: #arguments.mapping.getName()#:#arguments.mapping.getPath().toString()# by (#getName()#) injector"
);
Expand Down Expand Up @@ -801,7 +809,7 @@ component serializable="false" accessors="true" {
}

// debug info, NADA found!
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug( "Instance: #arguments.name# was not located anywhere by (#getName()#) injector" );
}

Expand Down Expand Up @@ -951,7 +959,7 @@ component serializable="false" accessors="true" {
variables.eventManager.announce( "afterInstanceAutowire", iData );

// Debug Data
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Finalized Autowire for: #arguments.targetID#:#arguments.mapping.getName()#:#arguments.mapping.getPath().toString()#"
);
Expand Down Expand Up @@ -1010,7 +1018,7 @@ component serializable="false" accessors="true" {
* @doc_generic boolean
*/
boolean function isColdBoxLinked(){
return isObject( variables.coldbox );
return !isNull( variables.coldbox ) && isObject( variables.coldbox );
}

/**
Expand All @@ -1019,7 +1027,7 @@ component serializable="false" accessors="true" {
* @doc_generic boolean
*/
boolean function isCacheBoxLinked(){
return isObject( variables.cacheBox );
return !isNull( variables.cacheBox ) && isObject( variables.cacheBox );
}

/**
Expand All @@ -1032,7 +1040,7 @@ component serializable="false" accessors="true" {
variables.scopeStorage.delete( scopeInfo.key, scopeInfo.scope );

// Log info
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug( "Injector (#getName()#) removed from scope: #scopeInfo.toString()#" );
}
}
Expand Down Expand Up @@ -1323,12 +1331,12 @@ component serializable="false" accessors="true" {
}

// some debugging goodness
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Dependency: #thisDIData.toString()# --> injected into #arguments.targetID# by (#getName()#) injector"
);
}
} else if ( variables.log.canDebug() ) {
} else if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Dependency: #thisDIData.toString()# Not Found when wiring #arguments.targetID#. Registered mappings are: #structKeyList( variables.binder.getMappings() )# by (#getName()#) injector"
);
Expand All @@ -1349,7 +1357,9 @@ component serializable="false" accessors="true" {
lock name="wirebox:transientcache" type="exclusive" throwontimeout="true" timeout="15" {
if ( !request.keyExists( "cbTransientDICache" ) ) {
request.cbTransientDICache = createObject( "java", "java.util.concurrent.ConcurrentHashMap" ).init();
variables.log.debug( () => "WireBox Transient Cache Created" );
if ( !isNull( variables.log ) ) {
variables.log.debug( () => "WireBox Transient Cache Created" );
}
}
}
}
Expand All @@ -1371,7 +1381,9 @@ component serializable="false" accessors="true" {
arguments.targetID.lcase(),
{ "injections" : {}, "delegations" : {} }
);
variables.log.debug( () => "WireBox Transient Cache Storage for #targetId# Created" );
if ( !isNull( variables.log ) ) {
variables.log.debug( () => "WireBox Transient Cache Storage for #targetId# Created" );
}
}
}
}
Expand Down Expand Up @@ -1501,7 +1513,7 @@ component serializable="false" accessors="true" {
for ( var key in customScopes ) {
variables.scopes[ key ] = createObject( "component", customScopes[ key ] ).init( this );
// Debugging
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug( "Registered custom scope: #key# (#customScopes[ key ]#)" );
}
}
Expand All @@ -1521,9 +1533,11 @@ component serializable="false" accessors="true" {
}
// If we have any aspects defined but no mixer, auto-add it
if ( !aopMixerAdded && variables.binder.hasAspects() ) {
variables.log.info(
"AOP aspects detected but no Mixer listener found, auto-adding it with defaults..."
);
if ( !isNull( variables.log ) && variables.log.canInfo() ) {
variables.log.info(
"AOP aspects detected but no Mixer listener found, auto-adding it with defaults..."
);
}
registerListener( { class : "coldbox.system.aop.Mixer", name : "aopMixer" } );
}
return this;
Expand All @@ -1542,7 +1556,9 @@ component serializable="false" accessors="true" {
// configure it
thisListener.configure( this, listener.properties );
} catch ( Any e ) {
variables.log.error( "Error creating listener: #listener.toString()#", e );
if ( !isNull( variables.log ) && variables.log.canError() ) {
variables.log.error( "Error creating listener: #listener.toString()#", e );
}
throw(
message : "Error creating listener: #listener.toString()#",
detail : "#e.message# #e.detail# #e.stackTrace#",
Expand All @@ -1562,7 +1578,7 @@ component serializable="false" accessors="true" {
}

// debugging
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Injector (#getName()#) has just registered a new listener: #listener.toString()#"
);
Expand All @@ -1585,7 +1601,7 @@ component serializable="false" accessors="true" {
);

// Log info
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Scope Registration enabled and Injector (#getName()#) scoped to: #arguments.scopeInfo.toString()#"
);
Expand All @@ -1609,7 +1625,7 @@ component serializable="false" accessors="true" {
if ( isObject( arguments.config.cacheFactory ) ) {
variables.cacheBox = arguments.config.cacheFactory;
// debugging
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Configured Injector #getName()# with direct CacheBox instance: #variables.cacheBox.getFactoryID()#"
);
Expand All @@ -1629,7 +1645,7 @@ component serializable="false" accessors="true" {
);

// debugging
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Configured Injector #getName()# with CacheBox instance: #variables.cacheBox.getFactoryID()# and configuration file: #arguments.config.configFile#"
);
Expand All @@ -1640,7 +1656,7 @@ component serializable="false" accessors="true" {
// No config file, plain vanilla cachebox
variables.cacheBox = createObject( "component", "#arguments.config.classNamespace#.CacheFactory" ).init();
// debugging
if ( variables.log.canDebug() ) {
if ( !isNull( variables.log ) && variables.log.canDebug() ) {
variables.log.debug(
"Configured Injector #getName()# with vanilla CacheBox instance: #variables.cacheBox.getFactoryID()#"
);
Expand Down
Loading

0 comments on commit 238f669

Please sign in to comment.