Search in sources :

Example 21 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project alliance by codice.

the class MpegTsUdpClient method executeFFmpeg.

private static DefaultExecuteResultHandler executeFFmpeg(final CommandLine command, final int timeoutSeconds, final PumpStreamHandler streamHandler) throws IOException {
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutSeconds * 1000);
    final Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }
    final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(command, resultHandler);
    return resultHandler;
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 22 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project oxCore by GluuFederation.

the class ProcessHelper method executeProgram.

/**
 * @param printJobTimeout
 *            the printJobTimeout (ms) before the watchdog terminates the print
 *            process
 * @param printInBackground
 *            printing done in the background or blocking
 * @param streamHandler
 * @return a print result handler (implementing a future)
 * @throws IOException
 *             the test failed
 */
public static PrintResultHandler executeProgram(CommandLine commandLine, String workingDirectory, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException {
    ExecuteWatchdog watchdog = null;
    PrintResultHandler resultHandler;
    // Create the executor and consider the successExitValue as success
    Executor executor = new DefaultExecutor();
    executor.setExitValue(successExitValue);
    if (StringHelper.isNotEmpty(workingDirectory)) {
        executor.setWorkingDirectory(new File(workingDirectory));
    }
    // Redirect streams if needed
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }
    // Create a watchdog if requested
    if (printJobTimeout > 0) {
        watchdog = new ExecuteWatchdog(printJobTimeout);
        executor.setWatchdog(watchdog);
    }
    // Pass a "ExecuteResultHandler" when doing background printing
    if (printInBackground) {
        LOG.debug(String.format("Executing non-blocking process %s", commandLine.toString()));
        resultHandler = new PrintResultHandler(watchdog);
        executor.execute(commandLine, resultHandler);
    } else {
        LOG.debug(String.format("Executing blocking process %s", commandLine.toString()));
        successExitValue = executor.execute(commandLine);
        resultHandler = new PrintResultHandler(successExitValue);
    }
    return resultHandler;
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) File(java.io.File)

Example 23 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project Saturn by vipshop.

the class ScriptPidUtils method exeCmdWithoutPipe.

public static String exeCmdWithoutPipe(CommandLine cmdLine, ByteArrayInputStream input, Map<String, String> env) {
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog dog = new ExecuteWatchdog(3 * 1000);
    executor.setWatchdog(dog);
    executor.setExitValue(0);
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        SaturnLogOutputStream errorOS = new SaturnLogOutputStream(log, SaturnLogOutputStream.LEVEL_ERROR);
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorOS, input);
        executor.setStreamHandler(streamHandler);
        LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "exec command: {}", cmdLine);
        int value = executor.execute(cmdLine, env);
        if (value == 0) {
            String out = outputStream.toString();
            return out;
        } else {
            return null;
        }
    } catch (Exception e) {
        LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, e.getMessage(), e);
        return null;
    }
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 24 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project project-build-plugin by axonivy.

the class EngineControl method start.

public Executor start() throws Exception {
    CommandLine startCmd = toEngineCommand(Command.start);
    context.log.info("Start Axon Ivy Engine in folder: " + context.engineDirectory);
    Executor executor = createEngineExecutor();
    executor.setStreamHandler(createEngineLogStreamForwarder(logLine -> findStartEngineUrl(logLine)));
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    executor.execute(startCmd, asynchExecutionHandler());
    waitForEngineStart(executor);
    return executor;
}
Also used : Executor(org.apache.commons.exec.Executor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BodyHandlers(java.net.http.HttpResponse.BodyHandlers) LineOrientedOutputStreamRedirector(ch.ivyteam.ivy.maven.util.stream.LineOrientedOutputStreamRedirector) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandLine(org.apache.commons.exec.CommandLine) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) HttpRequest(java.net.http.HttpRequest) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer) HttpClient(java.net.http.HttpClient) URI(java.net.URI) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) HttpResponse(java.net.http.HttpResponse) OutputStream(java.io.OutputStream) StartTestEngineMojo(ch.ivyteam.ivy.maven.test.StartTestEngineMojo) Redirect(java.net.http.HttpClient.Redirect) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) OsgiDir(ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory.OsgiDir) StopWatch(org.apache.commons.lang3.time.StopWatch) ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) IOUtils(org.apache.commons.io.IOUtils) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 25 with ExecuteWatchdog

use of org.apache.commons.exec.ExecuteWatchdog in project tycho by eclipse.

the class DefaultEquinoxLauncher method execute.

@Override
public int execute(LaunchConfiguration configuration, int forkedProcessTimeoutInSeconds) throws EquinoxLaunchingException {
    String executable = configuration.getJvmExecutable();
    if (executable == null || "".equals(executable)) {
        // use the same JVM as the one used to run Maven (the "java.home" one)
        executable = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
        if (File.separatorChar == '\\') {
            executable = executable + ".exe";
        }
    }
    CommandLine cli = new CommandLine(executable);
    final boolean handleQuotes = false;
    cli.addArguments(configuration.getVMArguments(), handleQuotes);
    cli.addArguments(new String[] { "-jar", getCanonicalPath(configuration.getLauncherJar()) }, handleQuotes);
    cli.addArguments(configuration.getProgramArguments(), handleQuotes);
    log.info("Command line:\n\t" + cli.toString());
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = null;
    if (forkedProcessTimeoutInSeconds > 0) {
        watchdog = new ExecuteWatchdog(forkedProcessTimeoutInSeconds * 1000L);
        executor.setWatchdog(watchdog);
    }
    // best effort to avoid orphaned child process
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    executor.setWorkingDirectory(configuration.getWorkingDirectory());
    try {
        return executor.execute(cli, getMergedEnvironment(configuration));
    } catch (ExecuteException e) {
        if (watchdog != null && watchdog.killedProcess()) {
            log.error("Timeout " + forkedProcessTimeoutInSeconds + " s exceeded. Process was killed.");
        }
        return e.getExitValue();
    } catch (IOException e) {
        throw new EquinoxLaunchingException(e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException) EquinoxLaunchingException(org.eclipse.sisu.equinox.launching.EquinoxLaunchingException) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Aggregations

ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)37 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)36 CommandLine (org.apache.commons.exec.CommandLine)25 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)24 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ExecuteException (org.apache.commons.exec.ExecuteException)14 File (java.io.File)12 Executor (org.apache.commons.exec.Executor)10 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)8 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)7 HashMap (java.util.HashMap)4 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)3 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)3 Jinjava (com.hubspot.jinjava.Jinjava)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2