Search in sources :

Example 41 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler 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 42 with PumpStreamHandler

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

the class ScriptJobRunner method execute.

private SaturnJobReturn execute(long timeoutSeconds) {
    SaturnJobReturn saturnJobReturn;
    ProcessOutputStream processOutputStream = new ProcessOutputStream(1);
    DefaultExecutor executor = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(processOutputStream);
    // 关闭线程等待时间, (注意commons-exec会固定增加2秒的addition)
    streamHandler.setStopTimeout(timeoutSeconds * 1000);
    executor.setExitValue(0);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(getWatchdog());
    // filter env key in execParameter. like cd ${mypath} -> cd /root/my.
    Map<String, String> env = ScriptPidUtils.loadEnv();
    CommandLine commandLine = createCommandLine(env);
    try {
        long start = System.currentTimeMillis();
        LogUtils.info(log, jobName, "Begin executing {}-{} {}", jobName, item, commandLine);
        int exitValue = executor.execute(commandLine, env);
        long end = System.currentTimeMillis();
        LogUtils.info(log, jobName, "Finish executing {}-{} {}, the exit value is {}, cost={}ms", jobName, item, commandLine, exitValue, (end - start));
        SaturnJobReturn tmp = readSaturnJobReturn();
        if (tmp == null) {
            tmp = new SaturnJobReturn("the exit value is " + exitValue);
        }
        saturnJobReturn = tmp;
    } catch (Exception e) {
        saturnJobReturn = handleException(timeoutSeconds, e);
    } finally {
        try {
            // 将日志set进jobLog, 写不写zk再由ExecutionService控制
            handleJobLog(processOutputStream.getJobLog());
            processOutputStream.close();
        } catch (Exception ex) {
            LogUtils.error(log, jobName, "{}-{} Error at closing output stream. Should not be concern: {}", jobName, item, ex.getMessage(), ex);
        }
        stopStreamHandler(streamHandler);
        ScriptPidUtils.removePidFile(job.getExecutorName(), jobName, "" + item, watchdog.getPid());
    }
    return saturnJobReturn;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException)

Example 43 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project ddf by codice.

the class VideoThumbnailPlugin method getVideoDuration.

private Duration getVideoDuration(final String videoFilePath) throws IOException, InterruptedException {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    final CommandLine command = getFFmpegInfoCommand(videoFilePath);
    final DefaultExecuteResultHandler resultHandler = executeFFmpeg(command, 15, streamHandler);
    resultHandler.waitFor();
    return parseVideoDuration(outputStream.toString(StandardCharsets.UTF_8.name()));
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project stanbol by apache.

the class JarExecutor method start.

/**
 * Start the jar if not done yet, and setup runtime hook to stop it.
 */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        @Override
        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        @Override
        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = System.getProperty("jar.executor.vm.options");
    final Executor e = new DefaultExecutor();
    if (this.workingDirectory != null) {
        e.setWorkingDirectory(this.workingDirectory);
    }
    final CommandLine cl = new CommandLine(javaExecutable);
    if (vmOptions != null && vmOptions.length() > 0) {
        // not the case for common usage patterns
        for (String option : StringUtils.split(vmOptions, " ")) {
            cl.addArgument(option);
        }
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    cl.addArgument("-p");
    cl.addArgument(String.valueOf(serverPort));
    log.info("Executing " + cl);
    e.setStreamHandler(new PumpStreamHandler());
    e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    e.execute(cl, h);
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 45 with PumpStreamHandler

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

the class EngineControl method createEngineLogStreamForwarder.

private PumpStreamHandler createEngineLogStreamForwarder(Consumer<String> logLineHandler) throws FileNotFoundException {
    OutputStream output = getEngineLogTarget();
    OutputStream engineLogStream = new LineOrientedOutputStreamRedirector(output) {

        @Override
        protected void processLine(byte[] b) throws IOException {
            // write file log
            super.processLine(b);
            String line = new String(b);
            context.log.debug("engine: " + line);
            if (logLineHandler != null) {
                logLineHandler.accept(line);
            }
        }
    };
    PumpStreamHandler streamHandler = new PumpStreamHandler(engineLogStream, System.err) {

        @Override
        public void stop() throws IOException {
            super.stop();
            // we opened the stream - we're responsible to close it!
            engineLogStream.close();
        }
    };
    return streamHandler;
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) LineOrientedOutputStreamRedirector(ch.ivyteam.ivy.maven.util.stream.LineOrientedOutputStreamRedirector)

Aggregations

PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)59 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)50 CommandLine (org.apache.commons.exec.CommandLine)45 IOException (java.io.IOException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)29 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)23 ExecuteException (org.apache.commons.exec.ExecuteException)22 File (java.io.File)17 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 Executor (org.apache.commons.exec.Executor)9 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)5 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3