Search in sources :

Example 1 with GitProcessExecutor

use of jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor in project teamcity-git by JetBrains.

the class FetchCommandImpl method fetchInSeparateProcess.

private void fetchInSeparateProcess(@NotNull Repository repository, @NotNull URIish uri, @NotNull FetchSettings settings) throws VcsException {
    final Collection<RefSpec> specs = settings.getRefSpecs();
    final String debugInfo = getDebugInfo(repository, uri, specs);
    final ProcessXmxProvider xmxProvider = new ProcessXmxProvider(new RepositoryXmxStorage(repository, "fetch"), myConfig, "fetch", debugInfo);
    Integer xmx = xmxProvider.getNextXmx();
    int attempt = 0;
    while (xmx != null) {
        attempt++;
        File gitPropertiesFile = null;
        File teamcityPrivateKey = null;
        GitProcessStuckMonitor processStuckMonitor = null;
        try {
            File gcDump = getDumpFile(repository, "gc");
            gitPropertiesFile = myFetcherProperties.getPropertiesFile();
            teamcityPrivateKey = getTeamCityPrivateKey(settings.getAuthSettings());
            final GeneralCommandLine cl = createFetcherCommandLine(repository, uri, xmx);
            final String commandLineString = cl.getCommandLineString();
            final GitProcessExecutor processExecutor = new GitProcessExecutor(cl);
            processStuckMonitor = new GitProcessStuckMonitor(gcDump, xmx.longValue(), commandLineString) {

                @Override
                protected void stuckDetected() {
                    processExecutor.interrupt();
                }
            };
            processStuckMonitor.start();
            final GitProcessExecutor.GitExecResult gitResult = processExecutor.runProcess(getFetchProcessInputBytes(getAuthSettings(settings, teamcityPrivateKey), repository.getDirectory(), uri, specs, getDumpFile(repository, null), gcDump, gitPropertiesFile), myConfig.getFetchTimeout(), settings.createStdoutBuffer(), new ByteArrayOutputStream(), new GitProcessExecutor.ProcessExecutorAdapter() {

                @Override
                public void processStarted() {
                    if (LOG.isDebugEnabled())
                        LOG.debug("git fetch process for " + debugInfo + " started in separate process with command line: " + commandLineString);
                    settings.getProgress().reportProgress("git fetch " + uri);
                }

                @Override
                public void processFinished() {
                    if (LOG.isDebugEnabled())
                        LOG.debug("git fetch process for " + debugInfo + " finished");
                    settings.getProgress().reportProgress("git fetch " + uri + " finished");
                }

                @Override
                public void processFailed(@NotNull final ExecutionException e) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("git fetch process for " + debugInfo + " failed");
                    settings.getProgress().reportProgress("git fetch " + uri + " failed");
                }
            });
            final ExecResult result = gitResult.getExecResult();
            VcsException commandError = CommandLineUtil.getCommandLineError("git fetch", " (repository dir: <TeamCity data dir>/system/caches/git/" + repository.getDirectory().getName() + ")", result, true, true);
            if (commandError != null) {
                commandError.setRecoverable(isRecoverable(commandError));
                /* if the process had not enough memory or we killed it because gc */
                if (gitResult.isOutOfMemoryError() || gitResult.isInterrupted()) {
                    final Integer nextXmx = xmxProvider.getNextXmx();
                    if (nextXmx != null) {
                        xmx = nextXmx;
                        clean(repository);
                        continue;
                    }
                    commandError = new VcsException("There is not enough memory for git fetch (last attempted -Xmx" + xmx + "M). Please contact your system administrator", commandError);
                } else if (attempt == 1 && settings.getAuthSettings().doesTokenNeedRefresh()) {
                    LOG.debug("git fetch process failed due to suspected token expiration for \"" + uri + "\" in directory \"" + repository.getDirectory() + "\", took " + TimePrinter.createMillisecondsFormatter().formatTime(gitResult.getDuration()) + ". Retrying with token refresh.");
                    continue;
                }
                LOG.info("git fetch process failed for \"" + uri + "\" in directory \"" + repository.getDirectory() + "\", took " + TimePrinter.createMillisecondsFormatter().formatTime(gitResult.getDuration()));
                if (gitResult.isTimeout()) {
                    logTimeout(debugInfo, getDumpFile(repository, null));
                }
                clean(repository);
                throw commandError;
            }
            LOG.info("git fetch process finished for: " + uri + " in directory: " + repository.getDirectory() + ", took " + gitResult.getDuration() + "ms");
            if (result.getStderr().length() > 0) {
                LOG.warn("Error output produced by git fetch:\n" + result.getStderr());
            }
            LOG.debug("git fetch process output:\n" + result.getStdout());
            break;
        } finally {
            if (teamcityPrivateKey != null) {
                FileUtil.delete(teamcityPrivateKey);
            }
            if (gitPropertiesFile != null) {
                FileUtil.delete(gitPropertiesFile);
            }
            if (processStuckMonitor != null) {
                processStuckMonitor.finish();
            }
        }
    }
}
Also used : RepositoryXmxStorage(jetbrains.buildServer.buildTriggers.vcs.git.process.RepositoryXmxStorage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RefSpec(org.eclipse.jgit.transport.RefSpec) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) VcsException(jetbrains.buildServer.vcs.VcsException) GitProcessExecutor(jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File) GitProcessStuckMonitor(jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessStuckMonitor) ExecResult(jetbrains.buildServer.ExecResult)

Example 2 with GitProcessExecutor

use of jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor in project teamcity-git by JetBrains.

the class GitPatchBuilderDispatcher method buildPatchInSeparateProcess.

private void buildPatchInSeparateProcess() throws Exception {
    final String rootStr = LogUtil.describe(myGitRoot);
    final ProcessXmxProvider xmxProvider = new ProcessXmxProvider(new RepositoryXmxStorage(myContext.getRepository(), "patch"), myConfig, "patch", "(root: " + rootStr + ")");
    Integer xmx = xmxProvider.getNextXmx();
    while (xmx != null) {
        final GeneralCommandLine patchCmd = createPatchCommandLine(xmx);
        final File patchFile = FileUtil.createTempFile("git", "patch");
        final File internalProperties = getPatchPropertiesFile();
        try {
            final ByteArrayOutputStream stdout = new LineAwareByteArrayOutputStream(Charset.forName("UTF-8"), new NoOpLineListener(), false);
            final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
            final GitProcessExecutor.GitExecResult gitResult = new GitProcessExecutor(patchCmd).runProcess(getInput(patchFile, internalProperties), myConfig.getPatchProcessIdleTimeoutSeconds(), stdout, stderr, new GitProcessExecutor.ProcessExecutorAdapter());
            VcsException patchError = CommandLineUtil.getCommandLineError("build patch", gitResult.getExecResult());
            if (patchError != null) {
                if (gitResult.isOutOfMemoryError()) {
                    final Integer nextXmx = xmxProvider.getNextXmx();
                    if (nextXmx != null) {
                        xmx = nextXmx;
                        FileUtil.delete(patchFile);
                        continue;
                    }
                    throw new VcsException("There is not enough memory for git patch (last attempted -Xmx" + xmx + "M). Please contact your system administrator", patchError);
                } else if (gitResult.isTimeout()) {
                    throw new VcsException("git patch for root " + rootStr + " was idle for more than " + myConfig.getFetchTimeout() + " second(s), try increasing the timeout using the " + PluginConfigImpl.TEAMCITY_GIT_IDLE_TIMEOUT_SECONDS + " property", patchError);
                }
                throw patchError;
            }
            new LowLevelPatcher(new FileInputStream(patchFile)).applyPatch(new NoExitLowLevelPatchTranslator(((PatchBuilderEx) myBuilder).getLowLevelBuilder()));
            break;
        } finally {
            FileUtil.delete(patchFile);
            FileUtil.delete(internalProperties);
        }
    }
}
Also used : RepositoryXmxStorage(jetbrains.buildServer.buildTriggers.vcs.git.process.RepositoryXmxStorage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) FileInputStream(java.io.FileInputStream) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) VcsException(jetbrains.buildServer.vcs.VcsException) GitProcessExecutor(jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor) File(java.io.File)

Aggregations

GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 GitProcessExecutor (jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor)2 RepositoryXmxStorage (jetbrains.buildServer.buildTriggers.vcs.git.process.RepositoryXmxStorage)2 VcsException (jetbrains.buildServer.vcs.VcsException)2 ExecutionException (com.intellij.execution.ExecutionException)1 FileInputStream (java.io.FileInputStream)1 ExecResult (jetbrains.buildServer.ExecResult)1 LineAwareByteArrayOutputStream (jetbrains.buildServer.LineAwareByteArrayOutputStream)1 GitProcessStuckMonitor (jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessStuckMonitor)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1