In the absence of both JMS (or SQS if you're really on EC2) I'd use a
DB as a queue. You need some sort of centralized repository, and DBs
are good at that.
component Queue {
function put(message) {
query("insert into queue (timestamp, status, message) values
(now(), 'new', '#serializeJson(message)#');
}
function has() {
return query("select id from queue limit 1").recordCount;
}
function take(workerId) {
q = query("select id, message from queue where status =
'in-progress-#workerId#' limit 1");
if (q.recordCount == 0) {
query("update queue set status = 'in-progress-#workerId#' where
status = 'new' limit 1");
return take(serverId);
}
return {
id: q.id,
message: deserializeJson(q.message)
};
}
function complete(messageId) {
query("update queue set status = 'complete' where id = #messageId#");
}
}
No, it's not amazingly feature rich, snazzy, cutting edge, etc. But
if you need CF and you need it quick'n'dirty it'll get the job done.
And no, I doubt that'll compile. But you get the drift. Just drop
that CFC on every server that needs it and let 'em at it.
As for scheduling the 'take's, you're kind of stuck without some kind
of scheduling framework, and you ruled all those out. ;) I the
absence of one of those you could use a method like this:
function workerThread() {
thread.run {
workerThreadInternal();
}
}
function workerThreadInternal() {
id = createUUID();
while (true) {
if (application.queue.has()) {
msg = application.queue.take();
application.worker.doIt(msg.message);
application.queue.complete(msg.id);
Thread.sleep(100);
} else {
Thread.sleep(10000);
}
}
}
Then invoke workerThread in OnApplicationStart to kick 'er off.
This has a pile of downsides, which is why stuff like JMS and SQS
exist. If you have alternatives, I wouldn't vote for something like
this, though I've certainly used this sort of thing more than once in
real apps.
cheers,
barneyb
On Mon, Aug 16, 2010 at 6:41 PM, Marc Esher <[email protected]> wrote:
> Hi all,
> Since it's been so long that cfcdev has had a message, I know you'll
> all put down the beers, put the spouses and kids to bed, and jump at
> the chance to pitch in here.
>
> I'm whipping up a prototype app -- not for work, not for homework --
> that I'm thought-experimenting about "how would I run this in a
> multi-server environment?". I'm not looking for code samples or
> anything really specific, just options.
>
> Imagine you have a *lot* of background work to do. You have an unknown
> -- and potentially elastic -- set of servers with which to do the
> work. It's probably easiest to think of it as if you were running this
> thing on Amazon EC2 or another service. You have a "queue" of work to
> be done. You have worker servers to do the work.
>
> What are your options for scheduling that work with ColdFusion? In an
> ideal world, there'd be a shared queue, and workers would "take()" off
> of that queue, with zero contention... i.e. as soon as one server
> takes off the queue, that task is immediately unavailable to another
> server that also attempted to take it.
>
> I'd normally use java for this, but I want to use CF to see how badass
> it can get in a situation such as this one. And this means no java
> Timers, TimerTasks, or my dear friends the ExecutorService and its
> wonderful relations... straight CF. I'm not opposed to event gateways,
> though I'd really like to stay away from a JMS server if I can help
> it. Importantly, it needs to be fairly easy to debug, which is
> always a problem in cases such as these.
>
> Thoughts?
>
> Oh, I know: this is like when the boss comes in and says "we need it
> fast, we need it simple, and we need it now". I have a good idea of
> how I'd do this with java, but I'm mostly interested in what CF could
> provide.
>
> Thanks!
>
> Marc
>
> --
> You received this message because you are subscribed to the Google Groups
> "CFCDev" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/cfcdev?hl=en.
>
>
--
Barney Boisvert
[email protected]
http://www.barneyb.com/
--
You received this message because you are subscribed to the Google Groups
"CFCDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cfcdev?hl=en.