changeset 205:38c8665824e5

Check if db is already running when an existing pid is found Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000692.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Wed, 04 Apr 2012 22:12:03 +0200
parents 832c61ca1f96
children 1584e5cbe144
files tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java
diffstat 2 files changed, 34 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Wed Apr 04 22:11:03 2012 +0200
+++ b/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Wed Apr 04 22:12:03 2012 +0200
@@ -113,7 +113,7 @@
         if (actionEvent.getSource().equals(database)) {
             switch (actionEvent.getActionId()) {
             // we are only interested in starting the agent if
-            // we started the service
+            // we started the database ourselves
             case START:
                 String dbUrl = database.getConfiguration().getDBConnectionString();
                 List<String> args = new ArrayList<>();
--- a/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java	Wed Apr 04 22:11:03 2012 +0200
+++ b/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java	Wed Apr 04 22:12:03 2012 +0200
@@ -44,14 +44,20 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import com.redhat.thermostat.common.config.InvalidConfigurationException;
 import com.redhat.thermostat.common.utils.LoggedExternalProcess;
+import com.redhat.thermostat.common.utils.LoggingUtils;
 
 import com.redhat.thermostat.tools.ApplicationException;
+import com.redhat.thermostat.tools.unix.UnixProcess;
 
 class MongoProcessRunner {
     
+    private static final Logger logger = LoggingUtils.getLogger(MongoProcessRunner.class);
+    
     private static final String [] MONGO_BASIC_ARGS = {
         "mongod", "--quiet", "--fork", "--nojournal", "--noauth", "--bind_ip"
     };
@@ -67,10 +73,11 @@
         this.configuration = configuration;
         this.isQuiet = quiet;
     }
-    
-    private String checkPid() {
+   
+    private String getPid() {
+        
         String pid = null;
-        // check the pid to be sure
+        
         File pidfile = configuration.getPidFile();
         Charset charset = Charset.defaultCharset();
         if (pidfile.exists()) {
@@ -79,8 +86,8 @@
                 if (pid == null || pid.isEmpty()) {
                     pid = null;
                 }
-            } catch (IOException ignore) {
-                ignore.printStackTrace();
+            } catch (IOException ex) {
+                logger.log(Level.WARNING, "Exception while reading pid file", ex);
                 pid = null;
             }
         }
@@ -100,8 +107,7 @@
             display("log file is here: " + configuration.getLogFile());
             
         } else {
-            // TODO: check the pid and see if it's running or not
-            // perhaps was already down
+            
             String message = "cannot shutdown server " + configuration.getDBPath() +
                     ", exit status: " + status +
                     ". Please check that your configuration is valid";
@@ -110,12 +116,29 @@
         }
     }
     
+    private boolean checkExistingProcess() {
+        String pid = getPid();
+        if (pid == null)
+            return false;
+        
+        String processName = UnixProcess.getInstance().getProcessName(getPid());
+        // TODO: check if we want mongos or mongod from the configs
+        return processName != null && processName.equalsIgnoreCase("mongod");
+    }
+    
     void startService() throws IOException, InterruptedException, ApplicationException {
         
-        String pid = checkPid();
+        String pid = getPid();
         if (pid != null) {
             String message = "cannot start server " + configuration.getDBPath() +
-                    ", found pid file rom previous run, please, cleanup";
+                             ", found pid file from previous run";
+            
+            if (!checkExistingProcess()) {
+                message += ", but no matching process running, please, cleanup";
+            } else {
+                message += ", an instance is running with pid: " + pid;
+            }
+            
             display(message);
             throw new ApplicationException(message);
         }
@@ -146,7 +169,7 @@
         LoggedExternalProcess process = new LoggedExternalProcess(commands);
         int status = process.runAndReturnResult();
         if (status == 0) {
-            pid = checkPid();
+            pid = getPid();
             if (pid == null) status = -1;
         }