Example usage for org.apache.commons.exec Executor getWatchdog

List of usage examples for org.apache.commons.exec Executor getWatchdog

Introduction

In this page you can find the example usage for org.apache.commons.exec Executor getWatchdog.

Prototype

ExecuteWatchdog getWatchdog();

Source Link

Document

Get the watchdog used to kill of processes running, typically, too long time.

Usage

From source file:ch.ivyteam.ivy.maven.TestStartEngine.java

private static void kill(Executor startedProcess) {
    if (startedProcess != null) {
        startedProcess.getWatchdog().destroyProcess();
    }/*from w w w  . j a v a2  s.  c  om*/
}

From source file:ch.ivyteam.ivy.maven.engine.EngineControl.java

private void waitForEngineStart(Executor executor) throws Exception {
    int i = 0;/*from   ww w .  j  ava  2 s  .c o  m*/
    while (!engineStarted.get()) {
        Thread.sleep(1_000);
        i++;
        if (!executor.getWatchdog().isWatching()) {
            throw new RuntimeException("Engine start failed unexpected.");
        }
        if (i > context.timeoutInSeconds) {
            throw new TimeoutException("Timeout while starting engine " + context.timeoutInSeconds + " [s].\n"
                    + "Check the engine log for details or increase the timeout property '"
                    + StartTestEngineMojo.IVY_ENGINE_START_TIMEOUT_SECONDS + "'");
        }
    }
    context.log.info("Engine started after " + i + " [s]");
}

From source file:com.netflix.spinnaker.clouddriver.jobs.local.JobExecutorLocal.java

private JobResult<String> execute(JobRequest jobRequest) throws IOException {
    ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
    ByteArrayOutputStream stdErr = new ByteArrayOutputStream();

    Executor executor = buildExecutor(new PumpStreamHandler(stdOut, stdErr, jobRequest.getInputStream()));
    int exitValue = executor.execute(jobRequest.getCommandLine(), jobRequest.getEnvironment());

    return JobResult.<String>builder()
            .result(exitValue == 0 ? JobResult.Result.SUCCESS : JobResult.Result.FAILURE)
            .killed(executor.getWatchdog().killedProcess()).output(stdOut.toString()).error(stdErr.toString())
            .build();//from   w  ww .j  a  v a 2s. c om
}

From source file:com.netflix.spinnaker.clouddriver.jobs.local.JobExecutorLocal.java

private <T> JobResult<T> executeStreaming(JobRequest jobRequest, ReaderConsumer<T> consumer)
        throws IOException {
    PipedOutputStream stdOut = new PipedOutputStream();
    ByteArrayOutputStream stdErr = new ByteArrayOutputStream();

    Executor executor = buildExecutor(new PumpStreamHandler(stdOut, stdErr, jobRequest.getInputStream()));
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(jobRequest.getCommandLine(), jobRequest.getEnvironment(), resultHandler);

    T result = consumer.consume(new BufferedReader(new InputStreamReader(new PipedInputStream(stdOut))));

    try {/*from  ww w  .j av a2 s . c o  m*/
        resultHandler.waitFor();
    } catch (InterruptedException e) {
        executor.getWatchdog().destroyProcess();
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }

    return JobResult.<T>builder()
            .result(resultHandler.getExitValue() == 0 ? JobResult.Result.SUCCESS : JobResult.Result.FAILURE)
            .killed(executor.getWatchdog().killedProcess()).output(result).error(stdErr.toString()).build();
}

From source file:org.apache.stratos.cartridge.agent.test.JavaCartridgeAgentTest.java

@After
public void tearDown() {
    for (Map.Entry<String, Executor> entry : executorList.entrySet()) {
        try {//from   w  w w .  j  a  v a  2s .c  o m
            String commandText = entry.getKey();
            Executor executor = entry.getValue();
            ExecuteWatchdog watchdog = executor.getWatchdog();
            if (watchdog != null) {
                log.info("Terminating process: " + commandText);
                watchdog.destroyProcess();
            }
            //                File workingDirectory = executor.getWorkingDirectory();
            //                if (workingDirectory != null) {
            //                    log.info("Cleaning working directory: " + workingDirectory.getAbsolutePath());
            //                    FileUtils.deleteDirectory(workingDirectory);
            //                }
        } catch (Exception ignore) {
        }
    }
    for (ServerSocket serverSocket : serverSocketList) {
        try {
            log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress());
            serverSocket.close();
        } catch (IOException e) {
            log.info("Couldn't stop socket server " + serverSocket.getLocalSocketAddress() + ", "
                    + e.getMessage());
        }
    }

    try {
        log.info("Deleting source checkout folder...");
        FileUtils.deleteDirectory(new File("/tmp/test-jca-source"));
    } catch (Exception ignore) {
    }

    //this.instanceStatusEventReceiver.terminate();
    //  this.topologyEventReceiver.terminate();

    this.instanceActivated = false;
    this.instanceStarted = false;
}

From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java

/**
 * TearDown method for test method testPythonCartridgeAgent
 *///from  w ww . ja va 2s  .co m
protected void tearDown(String sourcePath) {
    for (Map.Entry<String, Executor> entry : executorList.entrySet()) {
        try {
            String commandText = entry.getKey();
            Executor executor = entry.getValue();
            log.info("Terminating process: " + commandText);
            executor.setExitValue(0);
            executor.getWatchdog().destroyProcess();
        } catch (Exception ignore) {
        }
    }
    // wait until everything cleans up to avoid connection errors
    sleep(1000);
    for (ServerSocket serverSocket : serverSocketMap.values()) {
        try {
            log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress());
            serverSocket.close();
        } catch (IOException ignore) {
        }
    }
    try {
        if (thriftTestServer != null) {
            thriftTestServer.stop();
        }
    } catch (Exception ignore) {
    }

    if (sourcePath != null) {
        try {
            log.info("Deleting source checkout folder...");
            FileUtils.deleteDirectory(new File(sourcePath));
        } catch (Exception ignore) {
        }
    }
    log.info("Terminating event receivers...");
    this.instanceStatusEventReceiver.terminate();
    this.topologyEventReceiver.terminate();
    this.initializerEventReceiver.terminate();

    this.instanceStatusEventReceiver = null;
    this.topologyEventReceiver = null;
    this.initializerEventReceiver = null;

    this.instanceActivated = false;
    this.instanceStarted = false;

    // stop the broker services
    for (Map.Entry<String, BrokerService> entry : this.messageBrokers.entrySet()) {
        try {
            log.debug("Stopping broker service [" + entry.getKey() + "]");
            entry.getValue().stop();
        } catch (Exception ignore) {
        }
    }

    this.messageBrokers = null;

    // TODO: use thread synchronization and assert all connections are properly closed
    // leave some room to clear up active connections
    sleep(1000);
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java

/**
 * TearDown method for test method testPythonCartridgeAgent
 *//*ww w .ja  v  a2 s .c o  m*/
protected void tearDown(String sourcePath) {
    for (Map.Entry<String, Executor> entry : executorList.entrySet()) {
        try {
            String commandText = entry.getKey();
            Executor executor = entry.getValue();
            log.info("Terminating process: " + commandText);
            executor.setExitValue(0);
            executor.getWatchdog().destroyProcess();
        } catch (Exception ignore) {
        }
    }
    // wait until everything cleans up to avoid connection errors
    sleep(1000);
    for (ServerSocket serverSocket : serverSocketMap.values()) {
        try {
            log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress());
            serverSocket.close();
        } catch (IOException ignore) {
        }
    }
    try {
        if (thriftTestServer != null) {
            thriftTestServer.stop();
        }
    } catch (Exception e) {
        log.error("Could not stop Thrift test server", e);
    }

    try {
        log.info("Deleting source checkout folder...");
        FileUtils.deleteDirectory(new File(sourcePath));
    } catch (Exception ignore) {
    }
    this.instanceStatusEventReceiver.terminate();
    this.topologyEventReceiver.terminate();

    this.instanceActivated = false;
    this.instanceStarted = false;
    try {
        broker.stop();
    } catch (Exception e) {
        log.error("Error while stopping the broker service", e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonCartridgeAgentTest.java

/**
 * TearDown method for test method testPythonCartridgeAgent
 *//*from w  ww .  j a va 2s  .co  m*/
@After
public void tearDown() {
    for (Map.Entry<String, Executor> entry : executorList.entrySet()) {
        try {
            String commandText = entry.getKey();
            Executor executor = entry.getValue();
            ExecuteWatchdog watchdog = executor.getWatchdog();
            if (watchdog != null) {
                log.info("Terminating process: " + commandText);
                watchdog.destroyProcess();
            }
            File workingDirectory = executor.getWorkingDirectory();
            if (workingDirectory != null) {
                log.info("Cleaning working directory: " + workingDirectory.getAbsolutePath());
                FileUtils.deleteDirectory(workingDirectory);
            }
        } catch (Exception ignore) {
        }
    }
    for (ServerSocket serverSocket : serverSocketList) {
        try {
            log.info("Stopping socket server: " + serverSocket.getLocalSocketAddress());
            serverSocket.close();
        } catch (IOException ignore) {
        }
    }

    try {
        log.info("Deleting source checkout folder...");
        FileUtils.deleteDirectory(new File(SOURCE_PATH));
    } catch (Exception ignore) {

    }

    this.instanceStatusEventReceiver.terminate();
    this.topologyEventReceiver.terminate();

    this.instanceActivated = false;
    this.instanceStarted = false;
}

From source file:org.obm.push.mail.greenmail.ExternalProcess.java

protected ClosableProcess execute(Map<String, String> cliArgs) throws ExternalProcessException {
    setCommandLineArgs(cliArgs);//  w w  w . jav  a 2 s  . co m
    final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    final Executor executor = buildExecutor();
    executeProcess(resultHandler, executor);
    waitForProcessStartTime(resultHandler);

    return new ClosableProcess() {
        @Override
        public void closeProcess() throws InterruptedException {
            executor.getWatchdog().destroyProcess();
            resultHandler.waitFor();
        }
    };
}