This is in reply to a question from Bob Corsaro on Fri May 13, five months ago, 
but I'll post my solution in case it helps him (or anyone else), and also out 
of gratitude for the use of supervisord, and how quickly my last issue was 
fixed (45 minutes!)

About 90% of all my supervisord's child processes are JVMs (over 400 of them, 
spread out over 3 instances).  What I ran into was that the JVM already traps 
SIGINT, SIGTERM, etc for its own purposes, but leaves SIGUSR1 and SIGUSR2 
available.   I decided to use SIGUSR2 as a 'graceful termination' signal, and I 
used the sun.misc.Signal and sun.misc.Signal classes to trap this signal, set a 
flag in one of our main Application class, and have every other part of the 
system check this flags.  It has worked out well for us.  So all of our 
processing loops look like this:

            while(Application.terminationSignalReceived() == false) {
                do_stuff();
            }

Our supervisord.conf file has this line in each of the [program:] directives:
        stopsignal=USR2

Here are the relevant bits of our Application class:

        import sun.misc.Signal;
        import sun.misc.SignalHandler;
        
        public class Application {
            private static final HashMap<Signal, Long> caughtSignals = new 
HashMap<Signal, Long>();
            private static Signal SIGUSR2 = null; // I have other signals 
defined here  
            static public synchronized boolean terminationSignalReceived() {
                return caughtSignals.containsKey(SIGUSR2);
            }
            static public Map<Signal, Long> getCaughtSignals() {
                return caughtSignals;
            }
            static final private SignalHandler handler = new SignalHandler() {
                @Override
                public void handle(Signal signal) {
                    log.info("recorded signal: " + signal + ", waiting for 
application main loop to notice");
                    getCaughtSignals().put(signal, System.currentTimeMillis());
                }
            };
            static {
                // Windows doesn't have signals, so don't bother.
                if (System.getProperty("os.name").contains("Windows") == false) 
{
                    try {
                        SIGUSR2 = new Signal("USR2");   // I have other signals 
defined here too
                        Signal.handle(SIGUSR2, handler);
                    } catch (Exception e) {
                        log.error(e, e);
                        System.exit(1);
                    }
                }
            }
        }


Our apps do a lot of sleeping, so I even made an Application.sleep() that will 
abort with 500ms if SIGUSR2 is received:

    public static void sleep(long ms) {
        long timeToSleepUntil = System.currentTimeMillis() + ms;
        while (System.currentTimeMillis() < timeToSleepUntil && 
terminationSignalReceived() == false) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ignored) {
            }
        }
    }

Yes, I know about the warning about using com.sun and sun.misc, and the 
compiler gripes with every build, but I wasn't able to find a 'standard' Java 
way of handling Unix signals.  I'd love to hear about a better way, if anyone 
knows of it!

Hope this helps somebody.




tlj

----------------------------------------------------
david.birdsong at gmail.com wrote:
> You have to send it a signal that it will respond to, after
> 'stopwaitsecs' supervisord should send it a SIGKILL which should kill
> any process. So either you've set stopwaitsecs=0, which IIRC will wait
> forever or supervisord isn't able to get an exit status from java
> after sending a SIGKILL--which means that java is buried in a device
> driver or file system driver and cannot exit. Having hard drive
> problems?

> On Fri, May 13, 2011 at 11:33 AM, Bob Corsaro <rcorsaro at gmail.com> wrote:
> > I'm running a java process from supervisor and supervisorctl restart all
> > doesn't kill the old process.  Any tips for dealing with java?


....................................................................................................................
Timothy Jones | Advisory Systems Engineer, Development 
+1 (813) 637.5366 office | +1 (813) 454.8643 mobile
Syniverse Technologies | We make mobile work.
[email protected] | www.syniverse.com
.................................................................................................................... 
Find Syniverse on Facebook | Follow Syniverse on Twitter 

_______________________________________________
Supervisor-users mailing list
[email protected]
http://lists.supervisord.org/mailman/listinfo/supervisor-users

Reply via email to