Search in sources :

Example 1 with AbstractLineConsumer

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

the class Gdb method suspend.

public Location suspend(final String file, boolean isRemoteConnection) throws IOException, InterruptedException, DebuggerException {
    if (pid < 0) {
        throw new DebuggerException("Gdb process not found.");
    }
    if (isRemoteConnection) {
        Runtime.getRuntime().exec("kill -SIGINT " + pid).waitFor();
        sendCommand("signal SIGSTOP ");
    } else {
        final List<String> outputs = new ArrayList<>();
        final ProcessBuilder processBuilder = new ProcessBuilder().command("ps", "-o", "pid,cmd", "--ppid", String.valueOf(pid));
        final Process process = processBuilder.start();
        LineConsumer stdout = new AbstractLineConsumer() {

            @Override
            public void writeLine(String line) throws IOException {
                outputs.add(line);
            }
        };
        ProcessUtil.process(process, stdout);
        int processId = -1;
        for (String output : outputs) {
            try {
                final ProcessInfo processInfo = ProcessInfo.parse(output);
                if (file.equals(processInfo.getProcessName())) {
                    processId = processInfo.getProcessId();
                }
            } catch (Exception e) {
            //we can't get info about current process, but we are trying to get info about another processes
            }
        }
        if (processId == -1) {
            throw new DebuggerException(format("Process %s not found.", file));
        }
        Runtime.getRuntime().exec("kill -SIGINT " + processId).waitFor();
    }
    final GdbOutput gdbOutput = sendCommand("backtrace");
    final GdbBacktrace backtrace = GdbBacktrace.parse(gdbOutput);
    final Map<Integer, Location> frames = backtrace.getFrames();
    if (frames.containsKey(0)) {
        return frames.get(0);
    }
    throw new DebuggerException("Unable recognize current location for debugger session. ");
}
Also used : AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) ArrayList(java.util.ArrayList) ProcessInfo(org.eclipse.che.plugin.gdb.server.parser.ProcessInfo) GdbPrint(org.eclipse.che.plugin.gdb.server.parser.GdbPrint) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) IOException(java.io.IOException) GdbException(org.eclipse.che.plugin.gdb.server.exception.GdbException) GdbTerminatedException(org.eclipse.che.plugin.gdb.server.exception.GdbTerminatedException) GdbOutput(org.eclipse.che.plugin.gdb.server.parser.GdbOutput) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) GdbBacktrace(org.eclipse.che.plugin.gdb.server.parser.GdbBacktrace) Location(org.eclipse.che.api.debug.shared.model.Location)

Example 2 with AbstractLineConsumer

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

the class ArchetypeGenerator method execute.

/**
     * Execute maven archetype command
     *
     * @param commandLine
     *         command to execution e.g.
     *         mvn archetype:generate -DarchetypeGroupId=<archetype-groupId>  -DarchetypeArtifactId=<archetype-artifactId>
     *               -DarchetypeVersion=<archetype-version> -DgroupId=<my.groupid>      -DartifactId=<my-artifactId>
     * @param workDir
     *         folder where command will execute in common use root dir of workspace
     * @throws TimeoutException
     * @throws IOException
     * @throws InterruptedException
     */
private void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
    WebsocketMessageConsumer<ArchetypeOutput> websocketMessageConsumer = new WebsocketMessageConsumer(MAVEN_ARCHETYPE_CHANEL_NAME);
    websocketMessageConsumer.consume(new ArchetypeOutputImpl("Start Project generation", ArchetypeOutput.State.START));
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            websocketMessageConsumer.consume(new ArchetypeOutputImpl(line, ArchetypeOutput.State.IN_PROGRESS));
        }
    };
    // process will be stopped after timeout
    Watchdog watcher = new Watchdog(60, TimeUnit.SECONDS);
    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 ArchetypeOutputImpl("Done", ArchetypeOutput.State.DONE));
        if (isTimeoutExceeded.get()) {
            LOG.error("Generation project time expired : command-line " + Arrays.toString(commandLine));
            websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project time expired", ArchetypeOutput.State.ERROR));
            throw new TimeoutException();
        } else if (process.exitValue() != 0) {
            LOG.error("Generation project fail : command-line " + Arrays.toString(commandLine));
            websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project occurs error", ArchetypeOutput.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) ArchetypeOutput(org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput) Watchdog(org.eclipse.che.api.core.util.Watchdog) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AbstractLineConsumer

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

the class CheEnvironmentEngine method getMachineLogger.

private LineConsumer getMachineLogger(MessageConsumer<MachineLogMessage> environmentLogger, String machineId, String machineName) throws ServerException {
    createMachineLogsDir(machineId);
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            environmentLogger.consume(new MachineLogMessageImpl(machineName, line));
        }
    };
    try {
        return new ConcurrentCompositeLineConsumer(new ConcurrentFileLineConsumer(getMachineLogsFile(machineId)), lineConsumer);
    } catch (IOException e) {
        throw new MachineException(format("Unable create log file '%s' for machine '%s'.", e.getLocalizedMessage(), machineId));
    }
}
Also used : ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineLogMessageImpl(org.eclipse.che.api.machine.server.model.impl.MachineLogMessageImpl) IOException(java.io.IOException)

Example 4 with AbstractLineConsumer

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

the class AbstractAgentLauncher method launch.

@Override
public void launch(Instance machine, Agent agent) throws ServerException {
    if (isNullOrEmpty(agent.getScript())) {
        return;
    }
    ListLineConsumer agentLogger = new ListLineConsumer();
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
            agentLogger.writeLine(line);
        }
    };
    try {
        final InstanceProcess process = start(machine, agent, lineConsumer);
        LOG.debug("Waiting for agent {} is launched. Workspace ID:{}", agent.getId(), machine.getWorkspaceId());
        final long pingStartTimestamp = System.currentTimeMillis();
        while (System.currentTimeMillis() - pingStartTimestamp < agentMaxStartTimeMs) {
            if (agentLaunchingChecker.isLaunched(agent, process, machine)) {
                return;
            } else {
                Thread.sleep(agentPingDelayMs);
            }
        }
        process.kill();
    } catch (MachineException e) {
        logAsErrorAgentStartLogs(agent.getName(), agentLogger.getText());
        throw new ServerException(e.getServiceError());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new ServerException(format("Launching agent %s is interrupted", agent.getName()));
    } finally {
        try {
            lineConsumer.close();
        } catch (IOException ignored) {
        }
        agentLogger.close();
    }
    logAsErrorAgentStartLogs(agent.getName(), agentLogger.getText());
    throw new ServerException(format("Fail launching agent %s. Workspace ID:%s", agent.getName(), machine.getWorkspaceId()));
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ServerException(org.eclipse.che.api.core.ServerException) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

Example 5 with AbstractLineConsumer

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

the class DefaultAgentLauncher method launch.

@Override
public void launch(Instance machine, Agent agent) throws ServerException {
    if (isNullOrEmpty(agent.getScript())) {
        return;
    }
    final Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
    final InstanceProcess process = machine.createProcess(command, null);
    final LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
        }
    };
    try {
        process.start(lineConsumer);
    } catch (ConflictException e) {
        try {
            machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
        } catch (IOException ignored) {
        }
    } finally {
        try {
            lineConsumer.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) 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) ConflictException(org.eclipse.che.api.core.ConflictException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

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