Created
August 3, 2020 11:36
-
-
Save bennadel/e76fb4d32dc012bef75523f65e8727ed to your computer and use it in GitHub Desktop.
Using A Closure To "Terminate" CFThread Tags Across Page Requests In Lucee CFML 5.3.6.61
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ Test Thread 4b ]: Running iteration 1. | |
[ Test Thread 4b ]: Running iteration 2. | |
[ Test Thread 4b ]: Running iteration 3. | |
[ Test Thread 4b ]: Running iteration 4. | |
[ Test Thread 4b ]: About to force quit thread. | |
[ Test Thread 4b ]: CFThread exiting. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Keep track of the spawned threads so that we can "force quit" them from another | |
// page reference. | |
if ( isNull( application.threadRefs ) ) { | |
application.threadRefs = []; | |
} | |
// The execution of the CFThread tag is limited by the request-timeout of the server | |
// and application. As such, we have to bump up the request-timeout to make sure that | |
// our threads aren't terminated prematurely. | |
setting | |
requestTimeout = 120 | |
; | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
uniqueName = "Test Thread #createUniqueId()#"; | |
thread | |
name = "testThread" | |
type = "daemon" | |
uniqueName = uniqueName | |
{ | |
// I determine if the internal workings of the thread should continue running. | |
var isRunning = true; | |
// The THREAD scope acts as a "tunnel" by which the CFThread tag can expose data | |
// to the outside world. In this case, we're going to expose a CLOSURE that sets | |
// and THREAD-LOCAL VARIABLE to FALSE indicating that the CFThread should stop | |
// executing its internal loop. | |
thread.forceQuit = () => { | |
systemOutput( "[ #uniqueName# ]: About to force quit thread." ); | |
isRunning = false; | |
}; | |
// Keep "doing stuff" until we are told to stop. | |
for ( var i = 1 ; isRunning && i <= 120 ; i++ ) { | |
systemOutput( "[ #uniqueName# ]: Running iteration #i#.", true, false ); | |
sleep( 1000 ); | |
} | |
systemOutput( "[ #uniqueName# ]: CFThread exiting.", true, false ); | |
} // END: Thread. | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
// Keep a reference to the thread so we can terminate it from another page. | |
application.threadRefs.append( cfthread.testThread ); | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
if ( isNull( application.threadRefs ) || ! application.threadRefs.len() ) { | |
echo( "No threads to kill." ); | |
} | |
// Let's Loop over all of the cache CFThread instances and try to force-quit them. | |
for ( threadRef in application.threadRefs ) { | |
// NOTE: This IS NOT terminating the thread - it is calling a Thread-local | |
// function that has logic that will, in turn, allow the thread to complete. | |
threadRef.forceQuit(); | |
} | |
application.threadRefs = []; | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment