Search in sources :

Example 16 with LineConsumer

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

the class ConcurrentCompositeLineConsumerTest method shouldNotWriteIntoSubConsumersAfterClosingCompositeConsumer.

@Test
public void shouldNotWriteIntoSubConsumersAfterClosingCompositeConsumer() throws Exception {
    // given
    final String message = "Test line";
    // when
    concurrentCompositeLineConsumer.close();
    concurrentCompositeLineConsumer.writeLine(message);
    // then
    for (LineConsumer subConsumer : subConsumers) {
        verify(subConsumer, never()).writeLine(anyString());
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 17 with LineConsumer

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

the class ConcurrentCompositeLineConsumerTest method closeOperationShouldWaitUntilAllCurrentOperationsWillBeFinished.

@Test
public void closeOperationShouldWaitUntilAllCurrentOperationsWillBeFinished() throws Exception {
    // given
    final String message1 = "Message 1";
    final String message2 = "Message 2";
    WaitingAnswer<Void> waitingAnswer1 = waitOnWrite(lineConsumer2, message1);
    WaitingAnswer<Void> waitingAnswer2 = waitOnWrite(lineConsumer2, message2);
    // when
    executor.execute(concurrentCompositeLineConsumer::close);
    waitingAnswer1.completeAnswer();
    waitingAnswer2.completeAnswer();
    // then
    awaitFinalization();
    assertFalse(concurrentCompositeLineConsumer.isOpen());
    for (LineConsumer consumer : subConsumers) {
        verify(consumer).writeLine(eq(message1));
        verify(consumer).writeLine(eq(message2));
        verify(consumer, last()).close();
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 18 with LineConsumer

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

the class MachineProviderImpl method startService.

@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
    // copy to not affect/be affected by changes in origin
    service = new CheServiceImpl(service);
    ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
    ProgressMonitor progressMonitor = currentProgressStatus -> {
        try {
            machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
        } catch (IOException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    };
    String container = null;
    try {
        String image = prepareImage(machineName, service, progressMonitor);
        container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
        connectContainerToAdditionalNetworks(container, service);
        docker.startContainer(StartContainerParams.create(container));
        readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
        DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
        dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
        final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
        MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
        return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
    } catch (SourceNotFoundException e) {
        throw e;
    } catch (RuntimeException | ServerException | NotFoundException | IOException e) {
        cleanUpContainer(container);
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : ConnectContainer(org.eclipse.che.plugin.docker.client.json.network.ConnectContainer) RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) NetworkingConfig(org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) FileCleaner(org.eclipse.che.api.core.util.FileCleaner) Collectors.toSet(java.util.stream.Collectors.toSet) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) GetContainerLogsParams(org.eclipse.che.plugin.docker.client.params.GetContainerLogsParams) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) Set(java.util.Set) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) ConnectContainerToNetworkParams(org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams) List(java.util.List) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Pattern(java.util.regex.Pattern) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EndpointConfig(org.eclipse.che.plugin.docker.client.json.network.EndpointConfig) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) Size(org.eclipse.che.commons.lang.Size) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) LATEST_TAG(org.eclipse.che.plugin.docker.machine.DockerInstance.LATEST_TAG) CreateNetworkParams(org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) SocketTimeoutException(java.net.SocketTimeoutException) MachineInstanceProvider(org.eclipse.che.api.environment.server.MachineInstanceProvider) Collections.singletonMap(java.util.Collections.singletonMap) Named(javax.inject.Named) Instance(org.eclipse.che.api.machine.server.spi.Instance) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) Files(java.nio.file.Files) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) FileWriter(java.io.FileWriter) MoreObjects(com.google.common.base.MoreObjects) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) SystemInfo(org.eclipse.che.api.core.util.SystemInfo) RemoveNetworkParams(org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams) NotFoundException(org.eclipse.che.api.core.NotFoundException) File(java.io.File) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) IOException(java.io.IOException) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor)

Example 19 with LineConsumer

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

Example 20 with LineConsumer

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

the class CloneTest method testLineConsumerOutputWhenCloning.

@Test(dataProvider = "GitConnectionFactory", dataProviderClass = GitConnectionFactoryProvider.class)
public void testLineConsumerOutputWhenCloning(GitConnectionFactory connectionFactory) throws ServerException, IOException, UnauthorizedException, URISyntaxException {
    //given
    GitConnection remoteConnection = connectToGitRepositoryWithContent(connectionFactory, remoteRepo);
    GitConnection localConnection = connectionFactory.getConnection(localRepo.getAbsolutePath());
    LineConsumerFactory lineConsumerFactory = mock(LineConsumerFactory.class);
    LineConsumer lineConsumer = mock(LineConsumer.class);
    when(lineConsumerFactory.newLineConsumer()).thenReturn(lineConsumer);
    localConnection.setOutputLineConsumerFactory(lineConsumerFactory);
    //when
    localConnection.clone(CloneParams.create(remoteConnection.getWorkingDir().getAbsolutePath()));
    //then
    verify(lineConsumer, atLeastOnce()).writeLine(anyString());
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) GitConnection(org.eclipse.che.api.git.GitConnection) LineConsumerFactory(org.eclipse.che.api.core.util.LineConsumerFactory) Test(org.testng.annotations.Test)

Aggregations

LineConsumer (org.eclipse.che.api.core.util.LineConsumer)21 IOException (java.io.IOException)11 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)8 Test (org.testng.annotations.Test)8 Matchers.anyString (org.mockito.Matchers.anyString)6 HashMap (java.util.HashMap)3 ServerException (org.eclipse.che.api.core.ServerException)3 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)3 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 Command (org.eclipse.che.api.core.model.machine.Command)2 ConcurrentCompositeLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer)2 ConcurrentFileLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer)2 Instance (org.eclipse.che.api.machine.server.spi.Instance)2 WaitingAnswer (org.eclipse.che.commons.test.mockito.answer.WaitingAnswer)2 MoreObjects (com.google.common.base.MoreObjects)1