[coldbox 3.8.1] Accessing (adding to) the RequestContext inside a Thread

I have a method (see below) which will send out a bunch of emails. This all works fine, but I have been trying to get it working in a seperate thread (there could be a lot of emails), but have been having a lot of problems. I have narrowed it down to no being able to add to access the RequestContext inside the thread.

The email layout and view of the email generated inside the thread can access the items that were added to RC outside (above) the thread, but cannot access anything that I try to add to RC inside the thread.

More specifically. inside my layout (email/signatureEmail_thread), I can access rc,renewals, rc.emailView and rc.signature, which are created before the thead starts, but I cannot access rc.renewal, which is created inside the thread.

I have tried just about everything I can think of and too great to list. But the latest being to manually pass ‘event’ into the thread, but still not working.

Any pointers on how I can add the ‘renewal’ to the rc inside the threat greatly appreciated.

Jason

`

//send renewal notifications
public function emailpending(){
rc.renewals = memberService.getRenewalsPending(rc.periodID);
rc.emailView=‘membership/renewals/emails/pending_renewals’;
rc.signature = orm.get(“EmailSignature”,rc.signature);

thread name=“sendRenewals” event="#event#" renewals="#rc.renewals#" signature="#rc.signature#"{
//createObject( “java”, “coldfusion.tagext.lang.SettingTag” ).setRequestTimeout(javaCast( “double”, 1800 ));
var mailService = getPlugin(“MailService”);
rc = event.getCollection();
for (i=1; i<=arrayLen(renewals); i++){
rc.renewal = renewals[i];
try {

var email = mailService.newMail(
to = Application.settings.testemail,
from = signature.getEmailAddress(),
subject=“Membership Renewal”,
type = “html”
);
email.addMailPart(charset=‘utf-8’,type=‘text/plain’,body=renderLayout(“email/signatureEmail_thread”));
email.addMailPart(charset=‘utf-8’,type=‘text/html’,body=renderLayout(“email/signatureEmail_thread”));
sentEmail = mailService.send(email);

catch(any e) {
//log error
}
}

}

flash.put(‘success’,‘Emails have been sent to Members Pending Renewal’);
setNextEvent(url=’/index.cfm/admin/membership.renewals/listpending/#rc.periodID#’);
}

`

I do a lot of thread based emails with layouts.

Generally, anything I need to access inside the thread I pass via arguments (which it looks like you are doing). However, you need to access them via the attributes scope, which it doesn’t look like you are doing: i.e attributes.renewals

Anything I need to have in the layout, again, I pass via arguments.

Now, you could just do:

thread rc="#rc#" {
myVar = attributes.rc.myVar; // for example
attributes.Renderer.renderLayout(layout=“myLayout”, args={rc=attributes.rc}); //you should really be passing the Renderer component to the thread. I inject it using wirebox…
}

However, whilst the above would work, I find it better to just pass in the variables you need into the thread and the layout - not the entire rc (depending of course of what it already in it), otherwise your thread can get bloated and potentially takes a while to fire up (especially if you pass in components with dependencies etc).

HTH.

Tom.

Tom, you are a champion. That was it… And yes, I did need to pass in the Renderer to get it all to work. You have ended days of frustration. Thank you!!

You have to remember that threads should not rely on anything outside of what they know, think of it as a 100% black box scenario, where the only things that must be known are inputs and outputs and not rely on anything that may or may not be available.

For example, even if you pass it into the thread (I haven’t tried this so someone might correct me) it is by reference. That means that if you access say the cgi scope, there are times when the thread lives longer than the request. When that happens you can and do get unexpected results. For example if you are using the emails to say use the IP address of the user from the cgi scope, if you don’t copy the variables and pass them in, you experience a lot of issues like what your having. From null variables when they should contain something or null pointer errors.

If you remember that you will be fine.