Starting a thread from Glassfish


#1

I’m working on a project that is looking in to using the glassfish gem
for
our Rails application. One thing we need to do with this application is
have a process/thread running which is periodically (e.g. once per
second)
communicate with an external device. What I was thinking we could do is
start up a normal ruby thread inside of a Rails initializer, but the
problem
I’m finding is that glassfish will start up multiple Rails runtimes
which
are each starting up their own background thread. Anyone have any ideas
on
a way to start up only one thread no matter how many rails runtimes spin
up?

Joe


#2

The obvious way, if you can, is to run Rails in thread-safe mode.
GlassFish Gem will only start up one instance of rails if Rails is in
thread-safe mode. If you only need one runtime, you can also run with
–runtimes-max 1, which will limit it to a single runtime. However,
limiting it to a single runtime without thread-safe mode will prevent
serving concurrent requests.

Failing that, you would want to put a check into the thread starting
code to check to see if the thread has already started. Though I’m not
sure how to do that in Ruby, I’d guess that either checking for a named
thread before creating your own or touching a file on disk that you can
check would work.

Joseph A. wrote:

Joe


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

That’s what’s weird, I set up Rails to run in thread-safe mode, and only
use
1 rails instance, but I swear that all of my Rails initializers are
being
run twice. I have a lot of constants set up in initializers and with
GlassFish I get a bunch of ruby warnings about constants already being
defined. This does not happen with Mongrel. If GF is starting more
than
one rails runtime would I get those already initialized constant
warnings?
I’m not totally sure if GF starts one JRuby runtime but 2 rails
runtimes, or
if it would be 2 JRuby runtimes and 2 rails instances. Anyone know?

Joe


#4

There was a bug for this, since we were initializing Rails incorrectly
(loading things twice). I can’t find the report now, but it has since
been fixed. It will run the initializers twice, but only one rails
instance will come up. Since starting multiple Rails instances always
happens in separate JRuby runtimes, you won’t (or shouldn’t) see those
warnings if you were starting multiple runtimes.

For now, you may be able to ignore those warnings, but I’ll pester Vivek
to release a new version of the gem with that fix in it.

Joseph A. wrote:

Joe

    glassfish gem for our Rails application.  One thing we need to



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#5

Thank you very much, this answers a lot of my questions. Should this
bug be
fixed in the currently released GF gem? I think I’m on the latest one
available.

Joe