Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created October 20, 2013 15:27
Show Gist options
  • Save adamcameron/7071075 to your computer and use it in GitHub Desktop.
Save adamcameron/7071075 to your computer and use it in GitHub Desktop.
Very basic treatment of deferring the processing of a job, with callback handlers and monitoring functions.
<cfscript>
public struct function defer(required function job, function onSuccess, function onFailure, function onError, function onTerminate){
var deferThread = "";
try {
cfthread.status = "Running";
thread name="deferThread" action="run" attributecollection=arguments {
try {
successData.result = job();
cfthread.status = "Completed";
if (structKeyExists(attributes, "onSuccess")){
onSuccess(successData);
}
} catch (any e){
cfthread.status = "Failed";
if (structKeyExists(attributes, "onFailure")){
onFailure(e);
}else{
rethrow;
}
}
}
} catch (any e){
cfthread.status = "Errored";
if (structKeyExists(attributes, "onError")){
onError(e);
}else{
rethrow;
}
}
return {
getStatus = function(){
return cfthread.status;
},
terminate = function(){
if (cfthread.status == "Running"){
thread name="deferThread" action="terminate";
cfthread.status = "Terminated";
if (isDefined("onTerminate")){
onTerminate();
}
}
}
};
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment