use of org.eclipse.che.api.machine.server.spi.InstanceProcess 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.machine.server.spi.InstanceProcess 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.machine.server.spi.InstanceProcess in project che by eclipse.
the class MachineProcessManager method exec.
/**
* Execute a command in machine
*
* @param machineId
* id of the machine where command should be executed
* @param command
* command that should be executed in the machine
* @return {@link org.eclipse.che.api.machine.server.spi.InstanceProcess} that represents started process in machine
* @throws NotFoundException
* if machine with specified id not found
* @throws BadRequestException
* if value of required parameter is invalid
* @throws MachineException
* if other error occur
*/
public InstanceProcess exec(String workspaceId, String machineId, Command command, @Nullable String outputChannel) throws NotFoundException, MachineException, BadRequestException {
requiredNotNull(machineId, "Machine ID is required");
requiredNotNull(command, "Command is required");
requiredNotNull(command.getCommandLine(), "Command line is required");
requiredNotNull(command.getName(), "Command name is required");
requiredNotNull(command.getType(), "Command type is required");
final Instance machine = environmentEngine.getMachine(workspaceId, machineId);
final InstanceProcess instanceProcess = machine.createProcess(command, outputChannel);
final int pid = instanceProcess.getPid();
final LineConsumer processLogger = getProcessLogger(machineId, pid, outputChannel);
executor.execute(ThreadLocalPropagateContext.wrap(() -> {
try {
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.STARTED).withMachineId(machineId).withProcessId(pid));
instanceProcess.start(processLogger);
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.STOPPED).withMachineId(machineId).withProcessId(pid));
} catch (ConflictException | MachineException error) {
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.ERROR).withMachineId(machineId).withProcessId(pid).withError(error.getLocalizedMessage()));
try {
processLogger.writeLine(String.format("[ERROR] %s", error.getMessage()));
} catch (IOException ignored) {
}
} finally {
try {
processLogger.close();
} catch (IOException ignored) {
}
}
}));
return instanceProcess;
}
use of org.eclipse.che.api.machine.server.spi.InstanceProcess in project che by eclipse.
the class AbstractAgentLauncher method start.
protected InstanceProcess start(Instance machine, Agent agent, LineConsumer lineConsumer) throws ServerException {
Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
InstanceProcess process = machine.createProcess(command, null);
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) {
}
}
}));
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;
}
use of org.eclipse.che.api.machine.server.spi.InstanceProcess in project che by eclipse.
the class CommandExistsAgentChecker method isLaunched.
@Override
public boolean isLaunched(Agent agent, InstanceProcess process, Instance machine) throws MachineException {
Command command = new CommandImpl(format("Wait for %s, try %d", agent.getId(), ++counter), checkingCommand, "test");
try (ListLineConsumer lineConsumer = new ListLineConsumer()) {
InstanceProcess waitProcess = machine.createProcess(command, null);
waitProcess.start(lineConsumer);
return lineConsumer.getText().endsWith("[STDOUT] 0");
} catch (ConflictException e) {
throw new MachineException(e.getServiceError());
}
}
Aggregations