Fix for issue: 304
Running 'managix stop' should wait until the asterix instance actually
terminates rather than saying the system is stopped prematurely.

What was happening?
A kill -15 is not a force kill and triggers a JVM shutdown hook. 
Eventually the hook completes and JVM ends itself. But it may take significant time
if the hoo triggers a potentially expensive operation.
OS would return control to managix tricking it to believe that the script/command finished. 

What the fix was?
The script that invoked a kill -15 does not immediately return but waits until it has verified 
that the process (JVM) has indeed died. Managix doesnt hear back from the script until
then and so it waits.



git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_lsm_stabilization@1502 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-events/src/main/resources/events/node_failure/nc_failure.sh b/asterix-events/src/main/resources/events/node_failure/nc_failure.sh
index 3ca083c..c562bf2 100755
--- a/asterix-events/src/main/resources/events/node_failure/nc_failure.sh
+++ b/asterix-events/src/main/resources/events/node_failure/nc_failure.sh
@@ -5,3 +5,11 @@
 PID_INFO=`ps -ef |  grep asterix | grep -v grep | grep -v nc_join |  grep $PARENT_ID`
 PID=`echo $PID_INFO | cut -d " " -f2`
 kill -15 $PID
+
+cmd_output=$(jps|grep $PID)
+while [ ${#cmd_output} -ne 0 ]
+do
+  sleep 1
+  kill -15 $PID
+  cmd_output=$(jps|grep $PID)
+done
diff --git a/asterix-installer/src/main/java/edu/uci/ics/asterix/installer/command/StopCommand.java b/asterix-installer/src/main/java/edu/uci/ics/asterix/installer/command/StopCommand.java
index 0e10414..dfd8c5e 100644
--- a/asterix-installer/src/main/java/edu/uci/ics/asterix/installer/command/StopCommand.java
+++ b/asterix-installer/src/main/java/edu/uci/ics/asterix/installer/command/StopCommand.java
@@ -40,19 +40,24 @@
         AsterixInstance asterixInstance = InstallerUtil.validateAsterixInstanceExists(asterixInstanceName,
                 State.ACTIVE, State.UNUSABLE);
         PatternCreator pc = new PatternCreator();
-        List<Pattern> patternsToExecute = new ArrayList<Pattern>();
-        patternsToExecute.add(pc.createCCStopPattern(asterixInstance.getCluster().getMasterNode().getId()));
-
-        for (Node node : asterixInstance.getCluster().getNode()) {
-            patternsToExecute.add(pc.createNCStopPattern(node.getId(), asterixInstanceName + "_" + node.getId()));
-        }
         EventrixClient client = InstallerUtil.getEventrixClient(asterixInstance.getCluster());
+
+        List<Pattern> ncKillPatterns = new ArrayList<Pattern>();
+        for (Node node : asterixInstance.getCluster().getNode()) {
+            ncKillPatterns.add(pc.createNCStopPattern(node.getId(), asterixInstanceName + "_" + node.getId()));
+        }
+
+        List<Pattern> ccKillPatterns = new ArrayList<Pattern>();
+        ccKillPatterns.add(pc.createCCStopPattern(asterixInstance.getCluster().getMasterNode().getId()));
+
         try {
-            client.submit(new Patterns(patternsToExecute));
+            client.submit(new Patterns(ncKillPatterns));
+            client.submit(new Patterns(ccKillPatterns));
         } catch (Exception e) {
             // processes are already dead
             LOGGER.debug("Attempt to kill non-existing processess");
         }
+
         asterixInstance.setState(State.INACTIVE);
         asterixInstance.setStateChangeTimestamp(new Date());
         ServiceProvider.INSTANCE.getLookupService().updateAsterixInstance(asterixInstance);