changeset 642:74cc8134e87f

Handle SIGTERM in addition to SIGINT in agent. This patch is a fix for PR1179 where the agent did not cleanly shut down on a regular termination request (SIGTERM). It now shuts down in the same way as on CTRL+C. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-September/003466.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Fri, 28 Sep 2012 15:23:01 +0200
parents b515d481c39e
children b1f98c2a17d5
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java
diffstat 1 files changed, 29 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java	Thu Sep 27 12:09:57 2012 -0400
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java	Fri Sep 28 15:23:01 2012 +0200
@@ -173,15 +173,11 @@
         logger.fine("Agent id: " + agent.getId());
 
         final CountDownLatch shutdownLatch = new CountDownLatch(1);
-        Signal.handle(new Signal("INT"), new SignalHandler() {
-            public void handle(sun.misc.Signal sig) {
-                configServer.stopListening();
-                agent.stop();
-                logger.fine("Agent stopped.");       
-                shutdownLatch.countDown();
-            }
-        });
+        SignalHandler handler = new CustomSignalHandler(agent, configServer, logger, shutdownLatch);
+        Signal.handle(new Signal("INT"), handler);
+        Signal.handle(new Signal("TERM"), handler);
         try {
+            // Wait for either SIGINT or SIGTERM
             shutdownLatch.await();
             logger.fine("terminating agent cmd");
         } catch (InterruptedException e) {
@@ -237,5 +233,30 @@
     public Collection<ArgumentSpec> getAcceptedArguments() {
         return AgentOptionParser.getAcceptedArguments();
     }
+    
+    // Does not need a reference of the enclosing type so lets declare this class static
+    private static class CustomSignalHandler implements SignalHandler {
+
+        private Agent agent;
+        private ConfigurationServer configServer;
+        private Logger logger;
+        private CountDownLatch shutdownLatch;
+        
+        CustomSignalHandler(Agent agent, ConfigurationServer configServer, Logger logger, CountDownLatch latch) {
+            this.agent = agent;
+            this.configServer = configServer;
+            this.logger = logger;
+            this.shutdownLatch = latch;
+        }
+        
+        @Override
+        public void handle(Signal arg0) {
+            configServer.stopListening();
+            agent.stop();
+            logger.fine("Agent stopped.");       
+            shutdownLatch.countDown();
+        }
+        
+    }
 
 }