Search in sources :

Example 11 with LineConsumer

use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.

the class ComposerCommandExecutor method execute.

public static void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
    try (WebsocketMessageConsumer<ComposerOutput> websocketMessageConsumer = new WebsocketMessageConsumer<>(Constants.COMPOSER_CHANNEL_NAME)) {
        websocketMessageConsumer.consume(new ComposerOutputImpl(String.join(" ", commandLine), ComposerOutput.State.START));
        LineConsumer lineConsumer = new AbstractLineConsumer() {

            @Override
            public void writeLine(String line) throws IOException {
                websocketMessageConsumer.consume(new ComposerOutputImpl(line, ComposerOutput.State.IN_PROGRESS));
            }
        };
        // process will be stopped after timeout
        Watchdog watcher = new Watchdog(10, TimeUnit.MINUTES);
        try {
            final Process process = pb.start();
            final ValueHolder<Boolean> isTimeoutExceeded = new ValueHolder<>(false);
            watcher.start(() -> {
                isTimeoutExceeded.set(true);
                ProcessUtil.kill(process);
            });
            // consume logs until process ends
            ProcessUtil.process(process, lineConsumer);
            process.waitFor();
            websocketMessageConsumer.consume(new ComposerOutputImpl("Done", ComposerOutput.State.DONE));
            if (isTimeoutExceeded.get()) {
                LOG.error("Command time expired : command-line " + Arrays.toString(commandLine));
                websocketMessageConsumer.consume(new ComposerOutputImpl("Installing dependencies time expired", ComposerOutput.State.ERROR));
                throw new TimeoutException();
            } else if (process.exitValue() != 0) {
                LOG.error("Command failed : command-line " + Arrays.toString(commandLine));
                websocketMessageConsumer.consume(new ComposerOutputImpl("Error occurred", ComposerOutput.State.ERROR));
                throw new IOException("Process failed. Exit code " + process.exitValue() + " command-line : " + Arrays.toString(commandLine));
            }
        } finally {
            watcher.stop();
        }
    }
}
Also used : AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) IOException(java.io.IOException) ValueHolder(org.eclipse.che.api.core.util.ValueHolder) WebsocketMessageConsumer(org.eclipse.che.api.core.util.WebsocketMessageConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) Watchdog(org.eclipse.che.api.core.util.Watchdog) ComposerOutput(org.eclipse.che.plugin.composer.shared.dto.ComposerOutput) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with LineConsumer

use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.

the class AntUtils method getAntEnvironmentInformation.

public static Map<String, String> getAntEnvironmentInformation() throws IOException {
    final Map<String, String> versionInfo = new HashMap<>();
    final LineConsumer cmdOutput = new LineConsumer() {

        boolean end = false;

        @Override
        public void writeLine(String line) throws IOException {
            if (line.isEmpty()) {
                end = true;
            }
            if (end) {
                return;
            }
            String key = null;
            int keyEnd = 0;
            int valueStart = 0;
            final int l = line.length();
            while (keyEnd < l) {
                if (line.charAt(keyEnd) == ':') {
                    valueStart = keyEnd + 1;
                    break;
                }
                keyEnd++;
            }
            if (keyEnd > 0) {
                key = line.substring(0, keyEnd);
            }
            if (key != null) {
                while (valueStart < l && Character.isWhitespace(line.charAt(valueStart))) {
                    valueStart++;
                }
                if ("Ant version".equals(key)) {
                    int valueEnd = line.indexOf("compiled on", valueStart);
                    final String value = line.substring(valueStart, valueEnd).trim();
                    versionInfo.put(key, value);
                } else {
                    final String value = line.substring(valueStart);
                    versionInfo.put(key, value);
                }
            }
        }

        @Override
        public void close() throws IOException {
        }
    };
    readAntEnvironmentInformation(cmdOutput);
    return versionInfo;
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap)

Example 13 with LineConsumer

use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.

the class ConcurrentCompositeLineConsumerTest method shouldIgnoreWriteToSubConsumersAfterCloseWasCalled.

@Test
public void shouldIgnoreWriteToSubConsumersAfterCloseWasCalled() throws Exception {
    // given
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(lineConsumer2).close();
    executor.execute(() -> concurrentCompositeLineConsumer.close());
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // when
    concurrentCompositeLineConsumer.writeLine("Test line");
    waitingAnswer.completeAnswer();
    // then
    awaitFinalization();
    for (LineConsumer consumer : subConsumers) {
        verify(consumer, never()).writeLine(anyString());
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Test(org.testng.annotations.Test)

Example 14 with LineConsumer

use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.

the class ConcurrentCompositeLineConsumerTest method shouldWriteMessageIntoEachConsumer.

@Test
public void shouldWriteMessageIntoEachConsumer() throws Exception {
    // given
    final String message = "Test line";
    // when
    concurrentCompositeLineConsumer.writeLine(message);
    // then
    for (LineConsumer subConsumer : subConsumers) {
        verify(subConsumer).writeLine(eq(message));
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 15 with LineConsumer

use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.

the class ConcurrentCompositeLineConsumerTest method shouldBeAbleToWriteIntoSubConsumersSimultaneously.

@Test
public void shouldBeAbleToWriteIntoSubConsumersSimultaneously() throws Exception {
    // given
    final String message1 = "Message 1";
    final String message2 = "Message 2";
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(lineConsumer2).writeLine(eq(message1));
    executor.execute(() -> concurrentCompositeLineConsumer.writeLine(message1));
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // when
    concurrentCompositeLineConsumer.writeLine(message2);
    waitingAnswer.completeAnswer();
    // then
    awaitFinalization();
    for (LineConsumer consumer : subConsumers) {
        verify(consumer).writeLine(eq(message1));
        verify(consumer).writeLine(eq(message2));
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

LineConsumer (org.eclipse.che.api.core.util.LineConsumer)21 IOException (java.io.IOException)11 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)8 Test (org.testng.annotations.Test)8 Matchers.anyString (org.mockito.Matchers.anyString)6 HashMap (java.util.HashMap)3 ServerException (org.eclipse.che.api.core.ServerException)3 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)3 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 Command (org.eclipse.che.api.core.model.machine.Command)2 ConcurrentCompositeLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer)2 ConcurrentFileLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer)2 Instance (org.eclipse.che.api.machine.server.spi.Instance)2 WaitingAnswer (org.eclipse.che.commons.test.mockito.answer.WaitingAnswer)2 MoreObjects (com.google.common.base.MoreObjects)1