Search in sources :

Example 21 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project yeoman-maven-plugin by trecloux.

the class YeomanMojo method executeCommand.

void executeCommand(String command) throws MojoExecutionException {
    try {
        if (isWindows()) {
            command = "cmd /c " + command;
        }
        CommandLine cmdLine = CommandLine.parse(command);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setWorkingDirectory(yeomanProjectDirectory);
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new MojoExecutionException("Error during : " + command, e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException)

Example 22 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class ShellInterpreter method open.

@Override
public void open() {
    super.open();
    long timeoutThreshold = Long.parseLong(getProperty(TIMEOUT_PROPERTY, DEFAULT_TIMEOUT));
    long timeoutCheckInterval = Long.parseLong(getProperty(TIMEOUT_CHECK_INTERVAL_PROPERTY, DEFAULT_CHECK_INTERVAL));
    LOGGER.info("Command timeout property: {}", timeoutThreshold);
    executorMap = new ConcurrentHashMap<>();
    contextMap = new ConcurrentHashMap<>();
    shellOutputCheckExecutor.scheduleAtFixedRate(() -> {
        try {
            for (Map.Entry<String, DefaultExecutor> entry : executorMap.entrySet()) {
                String paragraphId = entry.getKey();
                DefaultExecutor executor = entry.getValue();
                InterpreterContext context = contextMap.get(paragraphId);
                if (context == null) {
                    LOGGER.warn("No InterpreterContext associated with paragraph: {}", paragraphId);
                    continue;
                }
                if ((System.currentTimeMillis() - context.out.getLastWriteTimestamp()) > timeoutThreshold) {
                    LOGGER.info("No output for paragraph {} for the last {} milli-seconds, so kill it", paragraphId, timeoutThreshold);
                    executor.getWatchdog().destroyProcess();
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error when checking shell command timeout", e);
        }
    }, timeoutCheckInterval, timeoutCheckInterval, TimeUnit.MILLISECONDS);
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecuteException(org.apache.commons.exec.ExecuteException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException)

Example 23 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class SubmarineJobTest method defaultExecutorTest.

@Test
public void defaultExecutorTest() throws IOException {
    DefaultExecutor executor = new DefaultExecutor();
    CommandLine cmdLine = CommandLine.parse(shell);
    URL urlTemplate = Resources.getResource(DEFAULT_EXECUTOR_TEST);
    cmdLine.addArgument(urlTemplate.getFile(), false);
    Map<String, String> env = new HashMap<>();
    env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`");
    env.put("EVN", "test");
    AtomicBoolean cmdLineRunning = new AtomicBoolean(true);
    StringBuffer sbLogOutput = new StringBuffer();
    executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {

        @Override
        protected void processLine(String line, int level) {
            // LOGGER.info(line);
            sbLogOutput.append(line + "\n");
        }
    }));
    executor.execute(cmdLine, env, new DefaultExecuteResultHandler() {

        @Override
        public void onProcessComplete(int exitValue) {
            cmdLineRunning.set(false);
        }

        @Override
        public void onProcessFailed(ExecuteException e) {
            cmdLineRunning.set(false);
            LOGGER.error(e.getMessage());
        }
    });
    int loopCount = 100;
    while ((loopCount-- > 0) && cmdLineRunning.get()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    LOGGER.info(sbLogOutput.toString());
}
Also used : DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) HashMap(java.util.HashMap) URL(java.net.URL) LogOutputStream(org.apache.commons.exec.LogOutputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) Test(org.junit.Test)

Example 24 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class JupyterKernelInterpreter method activateCondaEnv.

private String activateCondaEnv(String envName) throws IOException {
    LOGGER.info("Activating conda env: {}", envName);
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    if (!new File(envName).exists()) {
        throw new IOException("Fail to activating conda env because no environment folder: " + envName);
    }
    File scriptFile = Files.createTempFile("zeppelin_jupyter_kernel_", ".sh").toFile();
    try (FileWriter writer = new FileWriter(scriptFile)) {
        IOUtils.write(String.format("chmod 777 -R %s \nsource %s/bin/activate \nconda-unpack", envName, envName), writer);
    }
    scriptFile.setExecutable(true, false);
    scriptFile.setReadable(true, false);
    CommandLine cmd = new CommandLine(scriptFile.getAbsolutePath());
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(psh);
    int exitCode = executor.execute(cmd);
    if (exitCode != 0) {
        throw new IOException("Fail to activate conda env, " + stdout.toString());
    } else {
        LOGGER.info("Activate conda env successfully");
        this.condaEnv = envName;
        return envName + "/bin/python";
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) FileWriter(java.io.FileWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 25 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.

the class ProcessLauncher method launch.

public void launch() {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(processOutput));
    this.watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);
    try {
        executor.execute(commandLine, envs, this);
        transition(State.LAUNCHED);
        LOGGER.info("Process is launched: {}", commandLine);
    } catch (IOException e) {
        this.processOutput.stopCatchLaunchOutput();
        LOGGER.error("Fail to launch process: {}", commandLine, e);
        transition(State.TERMINATED);
        errorMessage = e.getMessage();
    }
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) IOException(java.io.IOException)

Aggregations

DefaultExecutor (org.apache.commons.exec.DefaultExecutor)82 CommandLine (org.apache.commons.exec.CommandLine)62 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)47 IOException (java.io.IOException)36 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)34 ExecuteException (org.apache.commons.exec.ExecuteException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 File (java.io.File)25 Executor (org.apache.commons.exec.Executor)12 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 URL (java.net.URL)4 Properties (java.util.Properties)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 InputStream (java.io.InputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 HashSet (java.util.HashSet)3