Search in sources :

Example 6 with AbstractLineConsumer

use of org.eclipse.che.api.core.util.AbstractLineConsumer 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 7 with AbstractLineConsumer

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

the class SshMachineExecAgentLauncher method start.

protected SshMachineProcess start(SshMachineInstance machine, Agent agent) throws ServerException {
    Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
    SshMachineProcess process = machine.createProcess(command, null);
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
        }
    };
    CountDownLatch countDownLatch = new CountDownLatch(1);
    executor.execute(ThreadLocalPropagateContext.wrap(() -> {
        try {
            countDownLatch.countDown();
            process.start(lineConsumer);
        } catch (ConflictException | MachineException e) {
            try {
                machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
            } catch (IOException ignored) {
            }
        } finally {
            try {
                lineConsumer.close();
            } catch (IOException ignored) {
            }
        }
    }));
    try {
        // ensure that code inside of task submitted to executor is called before end of this method
        countDownLatch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return process;
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IOException (java.io.IOException)7 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)7 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)7 TimeoutException (java.util.concurrent.TimeoutException)2 Command (org.eclipse.che.api.core.model.machine.Command)2 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)2 ValueHolder (org.eclipse.che.api.core.util.ValueHolder)2 Watchdog (org.eclipse.che.api.core.util.Watchdog)2 WebsocketMessageConsumer (org.eclipse.che.api.core.util.WebsocketMessageConsumer)2 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)2 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)2 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)2 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ConflictException (org.eclipse.che.api.core.ConflictException)1 ServerException (org.eclipse.che.api.core.ServerException)1 ConcurrentCompositeLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer)1 ConcurrentFileLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer)1 Location (org.eclipse.che.api.debug.shared.model.Location)1 DebuggerException (org.eclipse.che.api.debugger.server.exceptions.DebuggerException)1