Gracefully shutting down JRuby app on Tomcat

Currently I have a large JRuby application running on Tomcat. I have
generic scripts for managing my tomcat applications. However, my JRuby
app shuts down before all the currently running requests are done. So
I’m trying to get my app to wait them to finish before terminating.

My thoughts so far:

  1. Trap the “TERM” signal with “Signal.trap”
  2. Respond to load balancer to give the server no more traffic
  3. ??? Wait for remaining request threads to finish responding
  4. Normal cleanup in “at_exit”

The problem I have is with #3. I’m having difficulty trying to figure
out if there are any web requests still processing. I can see here
(http://wiki.apache.org/tomcat/FAQ/Monitoring) that there are some
useful JMX beans I could use but I don’t know how to import the classes
(How does “Catalina: type=ThreadPool,name=”[depends]“” translate to a
java_import?). Hell, even after figuring out how to get in the bean I’m
going to have to play around to see if I can get the information I need
(Get the active thread count, but does it include everything not just
request threads? Am I going to have to iterate and check the information
for each thread? etc.).

Any suggestions? Perhaps I’m going about this the completely wrong way.

Version info: Java 6; JRuby 1.67; Ruby 1.8.7

Thanks!
Jose

Why does your app shut down in the first place? Do you have a logger or
a
backtrace in place?

Many reasons. :slight_smile: One reason is maintenance on the server. Yep I have a
logger and a backtrace.

(sorry for the late reply: long Easter weekend)

I usually do not trust the container to stop properly a jruby/war
applet.
Catching a TERM signal is not very clean to me.

Instead I usually have a special route, with either a DELETE or HEAD
verb,
coupled with encrypted token in the header, allowing me through a curl
request to properly stop the webapp.

I had to do this as too many times the webapp closed without releasing
the
persisted database files (I’m on Windows using embedded databases). You
might want to explore this path.

Good luck!