runEvent asynchronous execution

I've got a loop in a handler and I want to dispatch another event to
go perform some tasks. The events I want to dispatch don't return any
data or even invoke a view, so they are ideal for asynchronous
execution. But reading through the documentation I can't seem to
figure out if runEvent is asynchronous or not. Does my loop have to
wait for the dispatched event to finish before continuing on its merry
way? If that is the default behavior, is there a way to run it
asynchronously?

Thanks,
Judah

While I do not have an answer to your question (is runEvent asynch or
not?) something smells fishy here. My opinion of CB handlers is that
they are a bridge to the HTTP paradigm into your code. But it sounds
like you want to invoke a handler outside of an HTTP request. Yes,
your "master" handler is running inside an HTTP request but your
children events are invoked manually-ish. Does this make sense? Since
this is the case, it makes more sense to just invoke your Service
layer directly, rather than run through your set of CB handlers which
are really being called outside of the HTTP cycle.

Maybe I am way off-base here.

/Cody

You can definitely execute asynchronous events or announcements via interceptors. However,

Just make sure that YOU thread it. ColdBox won’t thread it for you. So if you want to thread an event, then use cfthread and runEvent(), if you want an interception, which is what I would recommend you announce since it then does not depend on the event context, then announce.

<cfset announceInterception(“myEventToAnnounce”,attributes.myData)>

In this particular case, the dispatched event is grabbing data, using
a view to generate an html snippet that is then turned into a pdf,
then saved out to the file system along with some other things. The
dispatched event really does invoke the entire MVC stack, not just the
Service layer. I had originally though to do it down in my service
object but the templating for the PDF generation really screamed out
for using views. I had to sit down and talk with a couple coworkers to
do a sanity check and we all agreed that this is the way to do it, it
is just a slightly curious situation. Ordinarily I'd agree that your
instincts are right on as they are my instincts too.

I could do the same thing using cfthread and doing an http call to my
coldbox app. That wouldn't be a problem and it is the correct
paradigm, but if there is a good way to do it within the framework,
I'd prefer to use the framework.

Judah

Your use case is right on and I would just thread a runEven() call or dispatch an interceptor and still remain within the life cycle of the framework, but just in another thread.

I wouldn’t dispatch and http call though, that just seems DIRTY to me.

Luis

Also,
as a favor,can you post back your results and maybe some code. I would love to do a blog entry on this.

Thanks Luis. If I decide I need to optimize that section of code and
make it run asynchronously I'll take a look at the interceptors. I
think it might be worthwhile to make a note in the documentation on
runEvent to note that it does not behave asynchronously. I keep
thinking of "events" as occurring in a way that isn't bound to the
http request cycle, like in Javascript. Nothing wrong with how Coldbox
is doing it, the paradigm just isn't always obvious to me.

Thanks as always,
Judah

I wouldn't do a thread and http call either, which is why I inquired
about runEvent :slight_smile:

I'll need to read up on interceptors as I haven't written one yet but
when I do I'll try and get some code and a write up back to you. I may
just leave it as runEvent in a loop for now since this needs to be
beta for a client next week but I'm writing a note to revisit that
code for the 1.1 version right after beta.

Thanks,
Judah

Awesome!!

Keep us posted!