Search in sources :

Example 6 with RunningEnvironment

use of org.jboss.pnc.spi.environment.RunningEnvironment in project pnc by project-ncl.

the class TermdBuildDriver method startProjectBuild.

public RunningBuild startProjectBuild(BuildExecutionSession buildExecutionSession, RunningEnvironment runningEnvironment, Consumer<CompletedBuild> onComplete, Consumer<Throwable> onError, Optional<Consumer<Status>> onStatusUpdate) throws BuildDriverException {
    logger.info("[{}] Starting build for Build Execution Session {}", runningEnvironment.getId(), buildExecutionSession.getId());
    TermdRunningBuild termdRunningBuild = new TermdRunningBuild(runningEnvironment, buildExecutionSession.getBuildExecutionConfiguration(), onComplete, onError);
    DebugData debugData = runningEnvironment.getDebugData();
    String buildScript = prepareBuildScript(termdRunningBuild, debugData);
    if (!termdRunningBuild.isCanceled()) {
        String terminalUrl = getBuildAgentUrl(runningEnvironment);
        final RemoteInvocation remoteInvocation = new RemoteInvocation(clientFactory, terminalUrl, onStatusUpdate, httpCallbackMode, buildExecutionSession.getId(), buildExecutionSession.getAccessToken());
        buildExecutionSession.setBuildStatusUpdateConsumer(remoteInvocation.getClientStatusUpdateConsumer());
        FileTransfer fileTransfer = new ClientFileTransfer(remoteInvocation.getBuildAgentClient(), MAX_LOG_SIZE);
        fileTransferReadTimeout.ifPresent(fileTransfer::setReadTimeout);
        CompletableFuture<Void> prepareBuildFuture = CompletableFuture.supplyAsync(() -> {
            logger.debug("Uploading build script to build environment ...");
            return uploadTask(termdRunningBuild.getRunningEnvironment(), buildScript, fileTransfer);
        }, executor).thenApplyAsync(scriptPath -> {
            logger.debug("Setting the script path ...");
            remoteInvocation.setScriptPath(scriptPath);
            return null;
        }, executor).thenRunAsync(() -> {
            logger.debug("Invoking remote script ...");
            invokeRemoteScript(remoteInvocation);
        }, executor);
        CompletableFuture<RemoteInvocationCompletion> buildLivenessFuture = prepareBuildFuture.thenComposeAsync(nul -> {
            logger.debug("Starting liveness monitor ...");
            return monitorBuildLiveness(remoteInvocation);
        }, executor);
        CompletableFuture<RemoteInvocationCompletion> buildCompletionFuture = prepareBuildFuture.thenComposeAsync(nul -> {
            logger.debug("Waiting fo remote script to complete...");
            return remoteInvocation.getCompletionNotifier();
        }, executor);
        CompletableFuture<RemoteInvocationCompletion> optionallyEnableDebug = buildCompletionFuture.thenApplyAsync(remoteInvocationCompletion -> {
            Status status = remoteInvocationCompletion.getStatus();
            if (status.isFinal()) {
                logger.debug("Script completionNotifier completed with status {}.", status);
                if ((status == FAILED || status == SYSTEM_ERROR) && debugData.isEnableDebugOnFailure()) {
                    debugData.setDebugEnabled(true);
                    remoteInvocation.enableSsh();
                }
            }
            return remoteInvocationCompletion;
        }, executor);
        CompletableFuture<Object> buildFuture = CompletableFuture.anyOf(buildLivenessFuture, optionallyEnableDebug);
        buildFuture.handle((result, exception) -> {
            RemoteInvocationCompletion completion;
            if (result != null) {
                // both of combined futures return the same type
                RemoteInvocationCompletion remoteInvocationCompletion = (RemoteInvocationCompletion) result;
                if (remoteInvocationCompletion.getException() != null) {
                    logger.warn("Completing build execution.", remoteInvocationCompletion.getException());
                } else {
                    logger.debug("Completing build execution. Status: {};", remoteInvocationCompletion.getStatus());
                }
                completion = remoteInvocationCompletion;
            } else if (exception != null && exception.getCause() instanceof java.util.concurrent.CancellationException) {
                // canceled in non build operation (completableFuture cancel), non graceful completion
                logger.warn("Completing build execution. Cancelled;");
                completion = new RemoteInvocationCompletion(INTERRUPTED, Optional.empty());
            } else {
                logger.warn("Completing build execution. System error.", exception);
                completion = new RemoteInvocationCompletion(new BuildDriverException("System error.", exception));
            }
            termdRunningBuild.setCancelHook(null);
            remoteInvocation.close();
            complete(termdRunningBuild, completion, fileTransfer);
            return null;
        });
        termdRunningBuild.setCancelHook(() -> {
            // try to cancel remote execution
            remoteInvocation.cancel();
            ScheduledFuture<?> forceCancel_ = scheduledExecutorService.schedule(() -> {
                logger.debug("Force cancelling build ...");
                prepareBuildFuture.cancel(true);
            }, internalCancelTimeoutMillis, TimeUnit.MILLISECONDS);
            remoteInvocation.addPreClose(() -> forceCancel_.cancel(false));
        });
    } else {
        logger.debug("Skipping script uploading (cancel flag) ...");
    }
    return termdRunningBuild;
}
Also used : FileTransfer(org.jboss.pnc.termdbuilddriver.transfer.FileTransfer) ScheduledFuture(java.util.concurrent.ScheduledFuture) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ClientFileTransfer(org.jboss.pnc.termdbuilddriver.transfer.ClientFileTransfer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Inject(javax.inject.Inject) SYSTEM_ERROR(org.jboss.pnc.buildagent.api.Status.SYSTEM_ERROR) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) INTERRUPTED(org.jboss.pnc.buildagent.api.Status.INTERRUPTED) Logger(org.slf4j.Logger) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) FAILED(org.jboss.pnc.buildagent.api.Status.FAILED) BuildStatus(org.jboss.pnc.enums.BuildStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) COMPLETED(org.jboss.pnc.buildagent.api.Status.COMPLETED) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) CompletionStage(java.util.concurrent.CompletionStage) Paths(java.nio.file.Paths) MDCExecutors(org.jboss.pnc.common.concurrent.MDCExecutors) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Optional(java.util.Optional) Status(org.jboss.pnc.buildagent.api.Status) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) ApplicationScoped(javax.enterprise.context.ApplicationScoped) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) NamedThreadFactory(org.jboss.pnc.common.concurrent.NamedThreadFactory) BuildDriver(org.jboss.pnc.spi.builddriver.BuildDriver) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildStatus(org.jboss.pnc.enums.BuildStatus) Status(org.jboss.pnc.buildagent.api.Status) DebugData(org.jboss.pnc.spi.builddriver.DebugData) ClientFileTransfer(org.jboss.pnc.termdbuilddriver.transfer.ClientFileTransfer) FileTransfer(org.jboss.pnc.termdbuilddriver.transfer.FileTransfer) ClientFileTransfer(org.jboss.pnc.termdbuilddriver.transfer.ClientFileTransfer) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException)

Example 7 with RunningEnvironment

use of org.jboss.pnc.spi.environment.RunningEnvironment in project pnc by project-ncl.

the class DefaultBuildExecutor method cancel.

@Override
public void cancel(String executionConfigurationId) throws ExecutorException {
    DefaultBuildExecutionSession buildExecutionSession = runningExecutions.get(executionConfigurationId);
    if (buildExecutionSession != null) {
        log.info("Cancelling build {}.", buildExecutionSession.getId());
        if (buildExecutionSession.cancel()) {
            String buildContentId = buildExecutionSession.getBuildExecutionConfiguration().getBuildContentId();
            RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
            if (runningEnvironment == null) {
                log.warn("Could not retrieve running environment for build {}", buildContentId);
            } else {
                RepositorySession repositorySession = runningEnvironment.getRepositorySession();
                if (repositorySession == null) {
                    log.warn("Could not retrieve repository session for build {}", buildContentId);
                } else {
                    try {
                        repositorySession.deleteBuildGroup();
                    } catch (RepositoryManagerException e) {
                        log.error("Error deleting build group for build " + buildContentId + " when canceling it.", e);
                    }
                }
            }
        }
    } else {
        log.warn("Trying to cancel non existing session.");
    }
}
Also used : RepositoryManagerException(org.jboss.pnc.spi.repositorymanager.RepositoryManagerException) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment)

Example 8 with RunningEnvironment

use of org.jboss.pnc.spi.environment.RunningEnvironment in project pnc by project-ncl.

the class DefaultBuildExecutor method destroyEnvironment.

private void destroyEnvironment(BuildExecutionSession buildExecutionSession) {
    try {
        RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
        if (runningEnvironment != null) {
            userLog.info("Destroying build environment.");
            buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_ENV_DESTROYING);
            runningEnvironment.destroyEnvironment();
            userLog.info("Build environment destroyed.");
            buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_ENV_DESTROYED);
        } else {
            userLog.warn("Unable to destroy environment. Most likely due to cancelled operation.");
        }
    } catch (Throwable e) {
        throw new BuildProcessException(e);
    }
}
Also used : RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 9 with RunningEnvironment

use of org.jboss.pnc.spi.environment.RunningEnvironment in project pnc by project-ncl.

the class DefaultBuildExecutor method retrieveRepositoryManagerResults.

private Void retrieveRepositoryManagerResults(DefaultBuildExecutionSession buildExecutionSession) {
    try {
        if (!buildExecutionSession.hasFailed() && !buildExecutionSession.isCanceled()) {
            ProcessStageUtils.logProcessStageBegin(BuildExecutionStatus.COLLECTING_RESULTS_FROM_REPOSITORY_MANAGER.toString(), "Collecting results from repository manager ...");
            buildExecutionSession.setStatus(BuildExecutionStatus.COLLECTING_RESULTS_FROM_REPOSITORY_MANAGER);
            RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
            if (runningEnvironment == null) {
                return null;
            }
            RepositorySession repositorySession = runningEnvironment.getRepositorySession();
            if (repositorySession == null) {
                return null;
            }
            RepositoryManagerResult repositoryManagerResult = repositorySession.extractBuildArtifacts(true);
            buildExecutionSession.setRepositoryManagerResult(repositoryManagerResult);
            if (repositoryManagerResult.getCompletionStatus().isFailed()) {
                buildExecutionSession.setStatus(BuildExecutionStatus.COLLECTING_RESULTS_FROM_REPOSITORY_MANAGER_COMPLETED_WITH_ERROR);
            } else {
                buildExecutionSession.setStatus(BuildExecutionStatus.COLLECTING_RESULTS_FROM_REPOSITORY_MANAGER_COMPLETED_SUCCESS);
            }
        }
    } catch (Throwable e) {
        throw new BuildProcessException(e, buildExecutionSession.getRunningEnvironment());
    } finally {
        ProcessStageUtils.logProcessStageEnd(BuildExecutionStatus.COLLECTING_RESULTS_FROM_REPOSITORY_MANAGER.toString(), "Collected results from repository manager.");
    }
    return null;
}
Also used : RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 10 with RunningEnvironment

use of org.jboss.pnc.spi.environment.RunningEnvironment in project pnc by project-ncl.

the class BlockedBuildDriverMock method complete.

protected void complete(BuildExecutionSession buildExecutionSession, final RunningEnvironment runningEnvironment, Consumer<CompletedBuild> onComplete) throws InterruptedException {
    log.info("Running blocked build ...");
    semaphore.acquire();
    setBuildStatus(TestProjectConfigurationBuilder.CANCEL);
    log.info("Blocked build canceled.");
    onComplete.accept(new CompletedBuild() {

        @Override
        public BuildDriverResult getBuildResult() throws BuildDriverException {
            return getBuildResultMock(runningEnvironment);
        }

        @Override
        public RunningEnvironment getRunningEnvironment() {
            return runningEnvironment;
        }
    });
}
Also used : CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment)

Aggregations

RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)12 Consumer (java.util.function.Consumer)6 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)6 DebugData (org.jboss.pnc.spi.builddriver.DebugData)6 RepositorySession (org.jboss.pnc.spi.repositorymanager.model.RepositorySession)6 LoggerFactory (org.slf4j.LoggerFactory)6 TimeUnit (java.util.concurrent.TimeUnit)5 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)5 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)5 Logger (org.slf4j.Logger)5 Paths (java.nio.file.Paths)4 BuildProcessException (org.jboss.pnc.executor.exceptions.BuildProcessException)4 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)4 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)4 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutorService (java.util.concurrent.ExecutorService)3 TermdBuildDriverModuleConfig (org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig)3 BuildStatus (org.jboss.pnc.enums.BuildStatus)3 StartedEnvironment (org.jboss.pnc.spi.environment.StartedEnvironment)3