From d39848f06eff3fa6c53a6632ccb8a9b67c60582f Mon Sep 17 00:00:00 2001 From: Bryan Bende Date: Tue, 25 Aug 2015 15:43:52 -0400 Subject: [PATCH] NIFI-894 Introducing a lock file to communicate from boostrap to NiFi that a shutdown is in progress. --- .../org/apache/nifi/bootstrap/RunNiFi.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java index 636fd4a9b9..1299c6dcb2 100644 --- a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java +++ b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java @@ -235,6 +235,16 @@ public class RunNiFi { return statusFile; } + public File getLockFile(final Logger logger) { + final File confDir = bootstrapConfigFile.getParentFile(); + final File nifiHome = confDir.getParentFile(); + final File bin = new File(nifiHome, "bin"); + final File lockFile = new File(bin, "nifi.lock"); + + logger.debug("Lock File: {}", lockFile); + return lockFile; + } + private Properties loadProperties(final Logger logger) throws IOException { final Properties props = new Properties(); final File statusFile = getStatusFile(logger); @@ -498,6 +508,12 @@ public class RunNiFi { return; } + // 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"); @@ -576,6 +592,10 @@ public class RunNiFi { } } catch (final IOException ioe) { logger.error("Failed to send shutdown command to port {} due to {}", new Object[]{port, ioe.toString(), ioe}); + } finally { + if (lockFile.exists() && !lockFile.delete()) { + logger.error("Failed to delete lock file {}; this file should be cleaned up manually", lockFile); + } } } @@ -624,6 +644,11 @@ public class RunNiFi { return; } + final File prevLockFile = getLockFile(cmdLogger); + if (prevLockFile.exists() && !prevLockFile.delete()){ + cmdLogger.warn("Failed to delete previous lock file {}; this file should be cleaned up manually", prevLockFile); + } + final ProcessBuilder builder = new ProcessBuilder(); if (!bootstrapConfigFile.exists()) { @@ -805,6 +830,12 @@ public class RunNiFi { return; } + final File lockFile = getLockFile(defaultLogger); + if (lockFile.exists()) { + defaultLogger.info("A shutdown was initiated. Will not restart NiFi"); + return; + } + final boolean previouslyStarted = getNifiStarted(); if (!previouslyStarted) { defaultLogger.info("NiFi never started. Will not restart NiFi");