Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created August 3, 2020 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/e76fb4d32dc012bef75523f65e8727ed to your computer and use it in GitHub Desktop.
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
[ 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.
<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>
<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