Search in sources :

Example 1 with InstanceProcess

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()));
}
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 2 with InstanceProcess

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) {
        }
    }
}
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)

Example 3 with InstanceProcess

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;
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) CompositeLineConsumer(org.eclipse.che.api.core.util.CompositeLineConsumer) WebsocketLineConsumer(org.eclipse.che.api.core.util.WebsocketLineConsumer) FileLineConsumer(org.eclipse.che.api.core.util.FileLineConsumer) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineProcessEvent(org.eclipse.che.api.machine.shared.dto.event.MachineProcessEvent) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

Example 4 with 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;
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) Command(org.eclipse.che.api.core.model.machine.Command) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with InstanceProcess

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());
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess)

Aggregations

InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)9 IOException (java.io.IOException)5 Command (org.eclipse.che.api.core.model.machine.Command)4 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)4 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)4 ConflictException (org.eclipse.che.api.core.ConflictException)3 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)3 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)3 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)2 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Matcher (java.util.regex.Matcher)1 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)1 ServerException (org.eclipse.che.api.core.ServerException)1 CompositeLineConsumer (org.eclipse.che.api.core.util.CompositeLineConsumer)1 FileLineConsumer (org.eclipse.che.api.core.util.FileLineConsumer)1 WebsocketLineConsumer (org.eclipse.che.api.core.util.WebsocketLineConsumer)1 Instance (org.eclipse.che.api.machine.server.spi.Instance)1 MachineProcessEvent (org.eclipse.che.api.machine.shared.dto.event.MachineProcessEvent)1