jfrazee commented on a change in pull request #5004:
URL: https://github.com/apache/nifi/pull/5004#discussion_r614972469



##########
File path: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java
##########
@@ -808,6 +815,63 @@ public void notifyStop() {
                 "Hello,\n\nApache NiFi has been told to initiate a shutdown on 
host " + hostname + " at " + now + " by user " + user);
     }
 
+    public Integer decommission() throws IOException {
+        final Logger logger = cmdLogger;
+        final Integer port = getCurrentPort(logger);
+        if (port == null) {
+            logger.info("Apache NiFi is not currently running");
+            return 15;
+        }
+
+        // indicate that a stop command is in progress
+        final File lockFile = getLockFile(logger);
+        if (!lockFile.exists()) {
+            lockFile.createNewFile();
+        }
+
+        final Properties nifiProps = loadProperties(logger);
+        final String secretKey = nifiProps.getProperty("secret.key");
+        final String pid = nifiProps.getProperty(PID_KEY);
+        final File statusFile = getStatusFile(logger);
+        final File pidFile = getPidFile(logger);
+
+        try (final Socket socket = new Socket()) {
+            logger.debug("Connecting to NiFi instance");
+            socket.setSoTimeout(10000);

Review comment:
       This isn't specific to just here but the connection timeout value is in 
multiple places in `RunNiFi` and sometimes 10000 and other times 60000 so this 
one should probably be a constant.
   ```suggestion
               socket.setSoTimeout(NIFI_SOCKET_TIMEOUT_MILLIS);
   ```

##########
File path: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java
##########
@@ -841,69 +905,17 @@ public void stop() throws IOException {
             out.flush();
             socket.shutdownOutput();
 
-            final InputStream in = socket.getInputStream();
-            int lastChar;
-            final StringBuilder sb = new StringBuilder();
-            while ((lastChar = in.read()) > -1) {
-                sb.append((char) lastChar);
-            }
-            final String response = sb.toString().trim();
-
+            final String response = readResponse(socket.getInputStream());

Review comment:
       Since there's already a dependency on commons here it might be simpler 
to use `IOUtils.toString()` for these.
   ```suggestion
               final String response = 
IOUtils.toString(socket.getInputStream(), "UTF-8");
   ```

##########
File path: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java
##########
@@ -924,6 +936,65 @@ public void stop() throws IOException {
         }
     }
 
+    private String readResponse(final InputStream in) throws IOException {

Review comment:
       If you think it makes sense to use `IOUtils.toString()` then this can be 
removed.

##########
File path: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java
##########
@@ -924,6 +936,65 @@ public void stop() throws IOException {
         }
     }
 
+    private String readResponse(final InputStream in) throws IOException {
+        int lastChar;
+        final StringBuilder sb = new StringBuilder();
+        while ((lastChar = in.read()) > -1) {
+            sb.append((char) lastChar);
+        }
+
+        return sb.toString().trim();
+    }
+
+    private void waitForShutdown(final String pid, final Logger logger, final 
File statusFile, final File pidFile) throws IOException {
+        final Properties bootstrapProperties = new Properties();
+        try (final FileInputStream fis = new 
FileInputStream(bootstrapConfigFile)) {
+            bootstrapProperties.load(fis);
+        }
+
+        String gracefulShutdown = 
bootstrapProperties.getProperty(GRACEFUL_SHUTDOWN_PROP, 
DEFAULT_GRACEFUL_SHUTDOWN_VALUE);
+        int gracefulShutdownSeconds;
+        try {
+            gracefulShutdownSeconds = Integer.parseInt(gracefulShutdown);
+        } catch (final NumberFormatException nfe) {
+            gracefulShutdownSeconds = 
Integer.parseInt(DEFAULT_GRACEFUL_SHUTDOWN_VALUE);
+        }
+
+        notifyStop();
+        final long startWait = System.nanoTime();
+        while (isProcessRunning(pid, logger)) {
+            logger.info("Waiting for Apache NiFi to finish shutting down...");
+            final long waitNanos = System.nanoTime() - startWait;
+            final long waitSeconds = TimeUnit.NANOSECONDS.toSeconds(waitNanos);
+            if (waitSeconds >= gracefulShutdownSeconds && 
gracefulShutdownSeconds > 0) {
+                if (isProcessRunning(pid, logger)) {
+                    logger.warn("NiFi has not finished shutting down after {} 
seconds. Killing process.", gracefulShutdownSeconds);
+                    try {
+                        killProcessTree(pid, logger);
+                    } catch (final IOException ioe) {
+                        logger.error("Failed to kill Process with PID {}", 
pid);
+                    }
+                }
+                break;
+            } else {
+                try {
+                    Thread.sleep(2000L);

Review comment:
       Maybe a constant here.
   ```suggestion
                       Thread.sleep(GRACEFUL_SHUTDOWN_RETRY_MILLIS);
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to