Search in sources :

Example 1 with ComposerOutput

use of org.eclipse.che.plugin.composer.shared.dto.ComposerOutput 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)

Aggregations

IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)1 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)1 ValueHolder (org.eclipse.che.api.core.util.ValueHolder)1 Watchdog (org.eclipse.che.api.core.util.Watchdog)1 WebsocketMessageConsumer (org.eclipse.che.api.core.util.WebsocketMessageConsumer)1 ComposerOutput (org.eclipse.che.plugin.composer.shared.dto.ComposerOutput)1