use of org.eclipse.che.api.core.util.LineConsumer 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()));
}
use of org.eclipse.che.api.core.util.LineConsumer 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) {
}
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldCloseSubConsumerOnException.
@Test(dataProvider = "subConsumersExceptions")
public void shouldCloseSubConsumerOnException(Throwable exception) throws Exception {
// given
final String message = "Test line";
final String message2 = "Test line2";
LineConsumer closedConsumer = mock(LineConsumer.class);
doThrow(exception).when(closedConsumer).writeLine(anyString());
concurrentCompositeLineConsumer = new ConcurrentCompositeLineConsumer(appendTo(subConsumers, closedConsumer));
// when
concurrentCompositeLineConsumer.writeLine(message);
concurrentCompositeLineConsumer.writeLine(message2);
// then
verify(closedConsumer, never()).writeLine(eq(message2));
for (LineConsumer consumer : subConsumers) {
verify(consumer).writeLine(eq(message2));
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldDoNothingOnWriteLineIfAllSubConsumersAreClosed.
@Test
public void shouldDoNothingOnWriteLineIfAllSubConsumersAreClosed() throws Exception {
// given
final String message = "Test line";
LineConsumer[] closedConsumers = subConsumers;
for (LineConsumer consumer : closedConsumers) {
doThrow(ConsumerAlreadyClosedException.class).when(consumer).writeLine(anyString());
}
concurrentCompositeLineConsumer = new ConcurrentCompositeLineConsumer(closedConsumers);
// when
concurrentCompositeLineConsumer.writeLine("Error");
concurrentCompositeLineConsumer.writeLine(message);
// then
for (LineConsumer subConsumer : closedConsumers) {
verify(subConsumer, never()).writeLine(eq(message));
}
}
use of org.eclipse.che.api.core.util.LineConsumer 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. ");
}
Aggregations