Search in sources :

Example 1 with ValueHolder

use of org.eclipse.che.api.core.util.ValueHolder 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 2 with ValueHolder

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

the class ProjectServiceTest method registerImporter.

private void registerImporter(String importType, InputStream zip) throws Exception {
    final ValueHolder<FolderEntry> folderHolder = new ValueHolder<>();
    importerRegistry.register(new ProjectImporter() {

        @Override
        public String getId() {
            return importType;
        }

        @Override
        public boolean isInternal() {
            return false;
        }

        @Override
        public String getDescription() {
            return "Chuck importer";
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage) throws ConflictException, ServerException, ForbiddenException {
            importSources(baseFolder, storage, LineConsumerFactory.NULL);
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage, LineConsumerFactory importOutputConsumerFactory) throws ConflictException, ServerException, ForbiddenException {
            // Don't really use location in this test.
            baseFolder.getVirtualFile().unzip(zip, true, 0);
            folderHolder.set(baseFolder);
        }

        @Override
        public ImporterCategory getCategory() {
            return ImporterCategory.ARCHIVE;
        }
    });
}
Also used : SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) LineConsumerFactory(org.eclipse.che.api.core.util.LineConsumerFactory) ValueHolder(org.eclipse.che.api.core.util.ValueHolder) ProjectImporter(org.eclipse.che.api.project.server.importer.ProjectImporter)

Example 3 with ValueHolder

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

the class ProjectManagerWriteTest method registerImporter.

private void registerImporter(String importType, InputStream zip) throws Exception {
    final ValueHolder<FolderEntry> folderHolder = new ValueHolder<>();
    importerRegistry.register(new ProjectImporter() {

        @Override
        public String getId() {
            return importType;
        }

        @Override
        public boolean isInternal() {
            return false;
        }

        @Override
        public String getDescription() {
            return "importer";
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage) throws ConflictException, ServerException, ForbiddenException {
            importSources(baseFolder, storage, LineConsumerFactory.NULL);
        }

        @Override
        public void importSources(FolderEntry baseFolder, SourceStorage storage, LineConsumerFactory importOutputConsumerFactory) throws ConflictException, ServerException, ForbiddenException {
            // Don't really use location in this test.
            baseFolder.getVirtualFile().unzip(zip, true, 0);
            folderHolder.set(baseFolder);
        }

        @Override
        public ImporterCategory getCategory() {
            return ProjectImporter.ImporterCategory.ARCHIVE;
        }
    });
}
Also used : SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) LineConsumerFactory(org.eclipse.che.api.core.util.LineConsumerFactory) ValueHolder(org.eclipse.che.api.core.util.ValueHolder) ProjectImporter(org.eclipse.che.api.project.server.importer.ProjectImporter)

Example 4 with ValueHolder

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

the class DockerProcess method getErrorMessage.

private String getErrorMessage() {
    final StringBuilder errorMessage = new StringBuilder("Command output read timeout is reached.");
    try {
        // check if process is alive
        final Exec checkProcessExec = docker.createExec(CreateExecParams.create(container, new String[] { "/bin/sh", "-c", format("if kill -0 $(cat %1$s 2>/dev/null) 2>/dev/null; then cat %1$s; fi", pidFilePath) }).withDetach(false));
        ValueHolder<String> pidHolder = new ValueHolder<>();
        docker.startExec(StartExecParams.create(checkProcessExec.getId()), message -> {
            if (message.getType() == LogMessage.Type.STDOUT) {
                pidHolder.set(message.getContent());
            }
        });
        if (pidHolder.get() != null) {
            errorMessage.append(" Process is still running and has id ").append(pidHolder.get()).append(" inside machine");
        }
    } catch (IOException ignore) {
    }
    return errorMessage.toString();
}
Also used : Exec(org.eclipse.che.plugin.docker.client.Exec) IOException(java.io.IOException) ValueHolder(org.eclipse.che.api.core.util.ValueHolder)

Example 5 with ValueHolder

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

the class ComposerCommandExecutor method execute.

public static void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
    try (WebsocketMessageConsumer<ComposerOutput> websocketMessageConsumer = new WebsocketMessageConsumer<>(Constants.COMPOSER_CHANNEL_NAME)) {
        websocketMessageConsumer.consume(new ComposerOutputImpl(String.join(" ", commandLine), ComposerOutput.State.START));
        LineConsumer lineConsumer = new AbstractLineConsumer() {

            @Override
            public void writeLine(String line) throws IOException {
                websocketMessageConsumer.consume(new ComposerOutputImpl(line, ComposerOutput.State.IN_PROGRESS));
            }
        };
        // process will be stopped after timeout
        Watchdog watcher = new Watchdog(10, TimeUnit.MINUTES);
        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 ComposerOutputImpl("Done", ComposerOutput.State.DONE));
            if (isTimeoutExceeded.get()) {
                LOG.error("Command time expired : command-line " + Arrays.toString(commandLine));
                websocketMessageConsumer.consume(new ComposerOutputImpl("Installing dependencies time expired", ComposerOutput.State.ERROR));
                throw new TimeoutException();
            } else if (process.exitValue() != 0) {
                LOG.error("Command failed : command-line " + Arrays.toString(commandLine));
                websocketMessageConsumer.consume(new ComposerOutputImpl("Error occurred", ComposerOutput.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) Watchdog(org.eclipse.che.api.core.util.Watchdog) ComposerOutput(org.eclipse.che.plugin.composer.shared.dto.ComposerOutput) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ValueHolder (org.eclipse.che.api.core.util.ValueHolder)5 IOException (java.io.IOException)3 TimeoutException (java.util.concurrent.TimeoutException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)2 ServerException (org.eclipse.che.api.core.ServerException)2 SourceStorage (org.eclipse.che.api.core.model.project.SourceStorage)2 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)2 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)2 LineConsumerFactory (org.eclipse.che.api.core.util.LineConsumerFactory)2 Watchdog (org.eclipse.che.api.core.util.Watchdog)2 WebsocketMessageConsumer (org.eclipse.che.api.core.util.WebsocketMessageConsumer)2 ProjectImporter (org.eclipse.che.api.project.server.importer.ProjectImporter)2 ComposerOutput (org.eclipse.che.plugin.composer.shared.dto.ComposerOutput)1 Exec (org.eclipse.che.plugin.docker.client.Exec)1 ArchetypeOutput (org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput)1