changeset 1173:f34f23da58f8

Refactor MongoProcessRunner so that internal checks for running are same as public isStorageRunning() reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007304.html
author Jon VanAlten <vanaltj@gmail.com>
date Fri, 12 Jul 2013 16:03:07 -0600
parents c792f093228c
children 02bf4c95c96d
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/StorageNotRunningException.java
diffstat 2 files changed, 112 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Tue Jul 16 15:42:18 2013 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/MongoProcessRunner.java	Fri Jul 12 16:03:07 2013 -0600
@@ -78,94 +78,66 @@
 
     private DBStartupConfiguration configuration;
     private boolean isQuiet;
+    private String pid;
     
     public MongoProcessRunner(DBStartupConfiguration configuration, boolean quiet) {
         this.configuration = configuration;
         this.isQuiet = quiet;
     }
 
-    private String getPid() {
-        
-        String pid = null;
-        
+    private boolean checkPid() {
         File pidfile = configuration.getPidFile();
         Charset charset = Charset.defaultCharset();
         if (pidfile.exists()) {
             try (BufferedReader reader = Files.newBufferedReader(pidfile.toPath(), charset)) {
                 pid = reader.readLine();
-                if (pid == null || pid.isEmpty()) {
+                if (pid.isEmpty()) {
                     pid = null;
                 }
             } catch (IOException ex) {
                 logger.log(Level.WARNING, "Exception while reading pid file", ex);
                 pid = null;
             }
+        } else {
+            pid = null;
         }
-        
-        return pid;
+        return (pid != null);
     }
-    
-    public void stopService() throws IOException, InterruptedException, InvalidConfigurationException, ApplicationException {
- 
-        List<String> commands = new ArrayList<>(Arrays.asList(MONGO_SHUTDOWN_ARGS));
-        commands.add(getPid());
 
-        LoggedExternalProcess process = new LoggedExternalProcess(commands);
-        int status = process.runAndReturnResult();
-        if (status == 0) {
-            display(translator.localize(LocaleResources.SERVER_SHUTDOWN_COMPLETE, configuration.getDBPath().toString()));
-            display(translator.localize(LocaleResources.LOG_FILE_AT, configuration.getLogFile().toString()));
-            // all went well, make sure to remove pid file.
-            try {
-                Files.delete(configuration.getPidFile().toPath());
-            } catch (IOException e) {
-                // ignore
-            }
-        } else {
-            
-            LocalizedString message = translator.localize(LocaleResources.CANNOT_SHUTDOWN_SERVER,
-                    configuration.getDBPath().toString(),
-                    String.valueOf(status));
-            display(message);
-            throw new StorageStopException(configuration.getDBPath(), status, message.getContents());
+    private void deleteStalePidFile() {
+        pid = null;
+        LocalizedString message = translator.localize(LocaleResources.STALE_PID_FILE_NO_MATCHING_PROCESS, configuration.getPidFile().toString(), MONGO_PROCESS);
+        // Mongo didn't remove its PID file? Work around the issue. Log
+        // the event, remove the stale pid file and continue.
+        logger.log(Level.WARNING, message.getContents());
+        try {
+            Files.delete(configuration.getPidFile().toPath());
+        } catch (IOException benign) {
+            // ignore this benign error
         }
     }
     
-    private boolean checkExistingProcess() {
-        String pid = getPid();
-        if (pid == null)
+    public boolean isStorageRunning() {
+        if (!checkPid()) {
             return false;
+        }
         
-        String processName = UnixProcessUtilities.getInstance().getProcessName(getPid());
+        String processName = UnixProcessUtilities.getInstance().getProcessName(pid);
         // TODO: check if we want mongos or mongod from the configs
-        return processName != null && processName.equalsIgnoreCase(MONGO_PROCESS);
-    }
-    
-    public boolean isStorageRunning() {
-        return getPid() != null;
+        boolean processIsRunning = processName != null && processName.equalsIgnoreCase(MONGO_PROCESS);
+        if (!processIsRunning) {
+            deleteStalePidFile();
+        }
+        return processIsRunning;
     }
     
     public void startService() throws IOException, InterruptedException,
             ApplicationException, InvalidConfigurationException {
 
-        String pid = getPid();
-        if (pid != null) {
-            LocalizedString message = null;
-            if (!checkExistingProcess()) {
-                message = translator.localize(LocaleResources.STALE_PID_FILE_NO_MATCHING_PROCESS, configuration.getPidFile().toString(), MONGO_PROCESS);
-                // Mongo didn't remove its PID file? Work around the issue. Log
-                // the event, remove the stale pid file and continue.
-                logger.log(Level.WARNING, message.getContents());
-                try {
-                    Files.delete(configuration.getPidFile().toPath());
-                } catch (IOException benign) {
-                    // ignore this benign error
-                }
-            } else {
-                message = translator.localize(LocaleResources.STORAGE_ALREADY_RUNNING_WITH_PID, String.valueOf(pid));
-                display(message);
-                throw new StorageAlreadyRunningException(Integer.valueOf(pid), message.getContents());
-            }
+        if (isStorageRunning()) {
+            LocalizedString message = translator.localize(LocaleResources.STORAGE_ALREADY_RUNNING_WITH_PID, pid);
+            display(message);
+            throw new StorageAlreadyRunningException(Integer.valueOf(pid), message.getContents());
         }
         
         String dbVersion = getDBVersion();
@@ -187,14 +159,15 @@
         Thread.sleep(500);
 
         if (status == 0) {
-            pid = getPid();
-            if (pid == null) status = -1;
+            if (!isStorageRunning()) {
+                status = -1;
+            }
         }
-        
+
         if (status == 0) {
             display(translator.localize(LocaleResources.SERVER_LISTENING_ON, configuration.getDBConnectionString()));
             display(translator.localize(LocaleResources.LOG_FILE_AT, configuration.getLogFile().toString()));
-            display(translator.localize(LocaleResources.PID_IS,  String.valueOf(pid)));
+            display(translator.localize(LocaleResources.PID_IS, pid));
             
         } else {
             
@@ -206,6 +179,37 @@
         }
     }
     
+    public void stopService() throws IOException, InterruptedException, InvalidConfigurationException, ApplicationException {
+ 
+        if (!isStorageRunning()) {
+            LocalizedString message = translator.localize(LocaleResources.STORAGE_NOT_RUNNING);
+            display(message);
+            throw new StorageNotRunningException(message.getContents());
+        }
+        List<String> commands = new ArrayList<>(Arrays.asList(MONGO_SHUTDOWN_ARGS));
+        commands.add(pid);
+
+        LoggedExternalProcess process = new LoggedExternalProcess(commands);
+        int status = process.runAndReturnResult();
+        if (status == 0) {
+            display(translator.localize(LocaleResources.SERVER_SHUTDOWN_COMPLETE, configuration.getDBPath().toString()));
+            display(translator.localize(LocaleResources.LOG_FILE_AT, configuration.getLogFile().toString()));
+            // all went well, make sure to remove pid file.
+            try {
+                Files.delete(configuration.getPidFile().toPath());
+            } catch (IOException e) {
+                // ignore
+            }
+        } else {
+            
+            LocalizedString message = translator.localize(LocaleResources.CANNOT_SHUTDOWN_SERVER,
+                    configuration.getDBPath().toString(),
+                    String.valueOf(status));
+            display(message);
+            throw new StorageStopException(configuration.getDBPath(), status, message.getContents());
+        }
+    }
+    
     List<String> getStartupCommand(String dbVersion) throws IOException, InvalidConfigurationException {
         List<String> commands = new ArrayList<>(Arrays.asList(MONGO_BASIC_ARGS));
         
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/db/StorageNotRunningException.java	Fri Jul 12 16:03:07 2013 -0600
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2012, 2013 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.agent.cli.impl.db;
+
+import com.redhat.thermostat.common.tools.ApplicationException;
+
+@SuppressWarnings("serial")
+public class StorageNotRunningException extends ApplicationException {
+
+    public StorageNotRunningException(String message) {
+        super(message);
+    }
+}