Search in sources :

Example 1 with ProcessResult

use of org.zeroturnaround.exec.ProcessResult in project mongo-hadoop by mongodb.

the class BaseHadoopTest method mongoImport.

public void mongoImport(final String collection, final File file) {
    try {
        final List<String> command = new ArrayList<String>();
        command.addAll(asList(MONGO_IMPORT, "--drop", "--db", "mongo_hadoop", "--collection", collection, "--file", file.getAbsolutePath()));
        if (isAuthEnabled()) {
            final List<String> list = new ArrayList<String>(asList("-u", "bob", "-p", "pwd123"));
            if (!System.getProperty("mongodb_server", "").equals("22-release")) {
                list.addAll(asList("--authenticationDatabase", "admin"));
            }
            command.addAll(list);
        }
        final StringBuilder output = new StringBuilder();
        final Iterator<String> iterator = command.iterator();
        while (iterator.hasNext()) {
            final String s = iterator.next();
            if (output.length() != 0) {
                output.append("\t");
            } else {
                output.append("\n");
            }
            output.append(s);
            if (iterator.hasNext()) {
                output.append(" \\");
            }
            output.append("\n");
        }
        LOG.info(output.toString());
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
        final ProcessExecutor executor = new ProcessExecutor().command(command).readOutput(true).redirectOutput(outStream).redirectError(errStream);
        final ProcessResult result = executor.execute();
        if (result.getExitValue() != 0) {
            LOG.error(result.getOutput().getString());
            throw new RuntimeException(String.format("mongoimport failed with exit code %d: %s", result.getExitValue(), result.getOutput().getString()));
        }
    } catch (final IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final InterruptedException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final TimeoutException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) ProcessResult(org.zeroturnaround.exec.ProcessResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ProcessResult

use of org.zeroturnaround.exec.ProcessResult in project ninja by ninjaframework.

the class RunClassInSeparateJvmMachine method restart.

public synchronized void restart() {
    restarting.set(true);
    try {
        if (this.activeProcess != null) {
            log.debug("Attempting to destroy previous {} process", name);
            this.activeProcess.getProcess().destroy();
            log.debug("Waiting for previous {} process to terminate", name);
            ProcessResult result = this.activeProcess.getFuture().get();
        }
        log.debug("Starting new {}", name);
        this.activeProcess = startProcess();
    } catch (ExecutionException | InterruptedException | IOException e) {
        log.error("Something fishy happenend. Unable to cleanly restart {}!", name, e);
        log.error("You'll probably need to restart maven?");
    } finally {
        restarting.set(false);
    }
}
Also used : ProcessResult(org.zeroturnaround.exec.ProcessResult) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ProcessResult

use of org.zeroturnaround.exec.ProcessResult in project ninja by ninjaframework.

the class RunClassInSeparateJvmMachineTest method startProcess.

@Test
public void startProcess() throws Exception {
    RunClassInSeparateJvmMachine rcsjm = new RunClassInSeparateJvmMachine("FakeDaemon", FakeDaemonMain.class.getName(), getOurClassPath(), Collections.EMPTY_LIST, new File("."));
    // override output so we can capture it
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    rcsjm.setOutput(baos);
    StartedProcess startedProcess = rcsjm.startProcess();
    assertTrue(isAlive(startedProcess.getProcess()));
    waitOrTimeout(fakeDaemonCondition(baos), Timeout.timeout(Duration.millis(10000)));
    startedProcess.getProcess().destroy();
    ProcessResult processResult = startedProcess.getFuture().get();
}
Also used : ProcessResult(org.zeroturnaround.exec.ProcessResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) StartedProcess(org.zeroturnaround.exec.StartedProcess) Test(org.junit.Test)

Aggregations

ProcessResult (org.zeroturnaround.exec.ProcessResult)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 ProcessExecutor (org.zeroturnaround.exec.ProcessExecutor)1 StartedProcess (org.zeroturnaround.exec.StartedProcess)1