This is an automated email from the ASF dual-hosted git repository.

bbende pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new a8fbac520d NIFI-13689 Added Watch Delay to Bootstrap Start Command 
(#9219)
a8fbac520d is described below

commit a8fbac520d285b5a8ec388c3efa6b82d92372833
Author: David Handermann <[email protected]>
AuthorDate: Fri Aug 30 08:55:50 2024 -0500

    NIFI-13689 Added Watch Delay to Bootstrap Start Command (#9219)
---
 .../command/StandardBootstrapCommandProvider.java  |  5 +++-
 .../bootstrap/command/StartBootstrapCommand.java   | 34 ++++++++++++++++------
 .../command/StartBootstrapCommandTest.java         | 23 +++++++++++----
 3 files changed, 46 insertions(+), 16 deletions(-)

diff --git 
a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StandardBootstrapCommandProvider.java
 
b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StandardBootstrapCommandProvider.java
index bfaada97b4..94bd59d852 100644
--- 
a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StandardBootstrapCommandProvider.java
+++ 
b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StandardBootstrapCommandProvider.java
@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
 
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
@@ -69,6 +70,8 @@ public class StandardBootstrapCommandProvider implements 
BootstrapCommandProvide
 
     private static final int DAYS_REQUESTED_DEFAULT = 1;
 
+    private static final Duration START_WATCH_DELAY = Duration.ofSeconds(60);
+
     private static final BootstrapArgumentParser bootstrapArgumentParser = new 
StandardBootstrapArgumentParser();
 
     private static final Logger commandLogger = 
LoggerFactory.getLogger(ApplicationClassName.BOOTSTRAP_COMMAND.getName());
@@ -114,7 +117,7 @@ public class StandardBootstrapCommandProvider implements 
BootstrapCommandProvide
             final BootstrapCommand runBootstrapCommand = new 
RunBootstrapCommand(configurationProvider, processHandleProvider);
             final ProcessHandle currentProcessHandle = ProcessHandle.current();
             final BootstrapCommand statusBootstrapCommand = new 
ApplicationProcessStatusBootstrapCommand(currentProcessHandle);
-            bootstrapCommand = new StartBootstrapCommand(runBootstrapCommand, 
statusBootstrapCommand);
+            bootstrapCommand = new StartBootstrapCommand(runBootstrapCommand, 
statusBootstrapCommand, START_WATCH_DELAY);
         } else if (BootstrapArgument.STATUS == bootstrapArgument) {
             bootstrapCommand = new 
ManagementServerBootstrapCommand(processHandleProvider, HEALTH, 
commandLoggerStreamHandler);
         } else if (BootstrapArgument.STATUS_HISTORY == bootstrapArgument) {
diff --git 
a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StartBootstrapCommand.java
 
b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StartBootstrapCommand.java
index 0c49e0800e..e86277a4bf 100644
--- 
a/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StartBootstrapCommand.java
+++ 
b/nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/command/StartBootstrapCommand.java
@@ -19,6 +19,7 @@ package org.apache.nifi.bootstrap.command;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.time.Duration;
 import java.util.Objects;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -37,13 +38,16 @@ class StartBootstrapCommand implements BootstrapCommand {
 
     private final BootstrapCommand statusCommand;
 
+    private final Duration startWatchDelay;
+
     private final ScheduledExecutorService scheduledExecutorService;
 
     private CommandStatus commandStatus = CommandStatus.ERROR;
 
-    StartBootstrapCommand(final BootstrapCommand runCommand, final 
BootstrapCommand statusCommand) {
+    StartBootstrapCommand(final BootstrapCommand runCommand, final 
BootstrapCommand statusCommand, final Duration startWatchDelay) {
         this.runCommand = Objects.requireNonNull(runCommand);
         this.statusCommand = Objects.requireNonNull(statusCommand);
+        this.startWatchDelay = Objects.requireNonNull(startWatchDelay);
 
         this.scheduledExecutorService = Executors.newScheduledThreadPool(1, 
command -> {
             final Thread thread = new Thread(command);
@@ -63,15 +67,32 @@ class StartBootstrapCommand implements BootstrapCommand {
         commandStatus = runCommand.getCommandStatus();
 
         if (CommandStatus.SUCCESS == commandStatus) {
-            logger.info("Application watch started");
-            final WatchCommand watchCommand = new WatchCommand();
-            scheduledExecutorService.scheduleAtFixedRate(watchCommand, 
MONITOR_INTERVAL, MONITOR_INTERVAL, TimeUnit.SECONDS);
+            final StartWatchCommand startWatchCommand = new 
StartWatchCommand();
+            scheduledExecutorService.schedule(startWatchCommand, 
startWatchDelay.toMillis(), TimeUnit.MILLISECONDS);
             commandStatus = CommandStatus.RUNNING;
         } else {
             scheduledExecutorService.shutdown();
         }
     }
 
+    private class StartWatchCommand implements Runnable {
+
+        @Override
+        public void run() {
+            statusCommand.run();
+            final CommandStatus status = statusCommand.getCommandStatus();
+            if (CommandStatus.SUCCESS == status) {
+                final WatchCommand watchCommand = new WatchCommand();
+                scheduledExecutorService.scheduleAtFixedRate(watchCommand, 
MONITOR_INTERVAL, MONITOR_INTERVAL, TimeUnit.SECONDS);
+                logger.info("Application monitoring started");
+            } else {
+                logger.warn("Application monitoring failed with status [{}]", 
status);
+                scheduledExecutorService.shutdown();
+                commandStatus = status;
+            }
+        }
+    }
+
     private class WatchCommand implements Runnable {
 
         @Override
@@ -80,11 +101,6 @@ class StartBootstrapCommand implements BootstrapCommand {
             final CommandStatus status = statusCommand.getCommandStatus();
             if (CommandStatus.SUCCESS == status) {
                 logger.debug("Application running");
-            } else if (CommandStatus.FAILED == status) {
-                logger.error("Application watch failed");
-                scheduledExecutorService.shutdown();
-                logger.info("Application watch stopped");
-                commandStatus = CommandStatus.FAILED;
             } else {
                 logger.warn("Application not running [{}]", status);
                 runCommand.run();
diff --git 
a/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/command/StartBootstrapCommandTest.java
 
b/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/command/StartBootstrapCommandTest.java
index 2d9b0a642f..53237ed8d9 100644
--- 
a/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/command/StartBootstrapCommandTest.java
+++ 
b/nifi-bootstrap/src/test/java/org/apache/nifi/bootstrap/command/StartBootstrapCommandTest.java
@@ -22,12 +22,18 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.lenient;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
 class StartBootstrapCommandTest {
+    private static final Duration START_STATUS_DELAY = Duration.ofMillis(25);
+
+    private static final Duration RUN_STATUS_DELAY = Duration.ofMillis(500);
+
     @Mock
     private BootstrapCommand runBootstrapCommand;
 
@@ -38,7 +44,7 @@ class StartBootstrapCommandTest {
 
     @BeforeEach
     void setCommand() {
-        command = new StartBootstrapCommand(runBootstrapCommand, 
statusBootstrapCommand);
+        command = new StartBootstrapCommand(runBootstrapCommand, 
statusBootstrapCommand, START_STATUS_DELAY);
     }
 
     @Test
@@ -53,16 +59,21 @@ class StartBootstrapCommandTest {
     }
 
     @Test
-    void testRunSuccessFailed() {
+    void testRunSuccessFailed() throws InterruptedException {
         final CommandStatus runCommandStatus = CommandStatus.SUCCESS;
         
when(runBootstrapCommand.getCommandStatus()).thenReturn(runCommandStatus);
 
         final CommandStatus statusCommandStatus = CommandStatus.FAILED;
-        
lenient().when(statusBootstrapCommand.getCommandStatus()).thenReturn(statusCommandStatus);
+        
when(statusBootstrapCommand.getCommandStatus()).thenReturn(statusCommandStatus);
 
         command.run();
 
-        final CommandStatus commandStatus = command.getCommandStatus();
-        assertEquals(CommandStatus.RUNNING, commandStatus);
+        final CommandStatus runningStatus = command.getCommandStatus();
+        assertEquals(CommandStatus.RUNNING, runningStatus);
+
+        TimeUnit.MILLISECONDS.sleep(RUN_STATUS_DELAY.toMillis());
+
+        final CommandStatus failedStatus = command.getCommandStatus();
+        assertEquals(CommandStatus.FAILED, failedStatus);
     }
 }

Reply via email to