Search in sources :

Example 1 with BuildProcessException

use of org.jboss.pnc.executor.exceptions.BuildProcessException in project pnc by project-ncl.

the class DefaultBuildExecutor method stopRunningEnvironment.

/**
 * Tries to stop running environment if the exception contains information about running environment
 *
 * @param ex Exception in build process (To stop the environment it has to be instance of BuildProcessException)
 */
private void stopRunningEnvironment(Throwable ex) {
    DestroyableEnvironment destroyableEnvironment = null;
    if (ex instanceof BuildProcessException) {
        BuildProcessException bpEx = (BuildProcessException) ex;
        destroyableEnvironment = bpEx.getDestroyableEnvironment();
    } else if (ex.getCause() instanceof BuildProcessException) {
        BuildProcessException bpEx = (BuildProcessException) ex.getCause();
        destroyableEnvironment = bpEx.getDestroyableEnvironment();
    } else {
        // It shouldn't never happen - Throwable should be caught in all steps of build chain
        // and BuildProcessException should be thrown instead of that
        log.warn("Possible leak of a running environment! Build process ended with exception, " + "but the exception didn't contain information about running environment.", ex);
    }
    try {
        if (destroyableEnvironment != null) {
            destroyableEnvironment.destroyEnvironment();
        }
    } catch (Throwable envE) {
        log.warn("Running environment" + destroyableEnvironment + " couldn't be destroyed!", envE);
    }
}
Also used : DestroyableEnvironment(org.jboss.pnc.spi.environment.DestroyableEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 2 with BuildProcessException

use of org.jboss.pnc.executor.exceptions.BuildProcessException in project pnc by project-ncl.

the class DefaultBuildExecutor method runTheBuild.

private CompletableFuture<CompletedBuild> runTheBuild(DefaultBuildExecutionSession buildExecutionSession) {
    CompletableFuture<CompletedBuild> waitToCompleteFuture = new CompletableFuture<>();
    if (buildExecutionSession.isCanceled()) {
        waitToCompleteFuture.complete(null);
        return waitToCompleteFuture;
    }
    ProcessStageUtils.logProcessStageBegin(BuildExecutionStatus.BUILD_SETTING_UP.toString(), "Running the build ...");
    buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_SETTING_UP);
    RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
    try {
        Consumer<CompletedBuild> onComplete = value -> {
            ProcessStageUtils.logProcessStageEnd(BuildExecutionStatus.BUILD_SETTING_UP.toString(), "Build completed.");
            waitToCompleteFuture.complete(value);
        };
        Consumer<Throwable> onError = (e) -> {
            ProcessStageUtils.logProcessStageEnd(BuildExecutionStatus.BUILD_SETTING_UP.toString(), "Build failed.");
            waitToCompleteFuture.completeExceptionally(new BuildProcessException(e, runningEnvironment));
        };
        String buildAgentUrl = runningEnvironment.getBuildAgentUrl();
        String liveLogWebSocketUrl = "ws" + StringUtils.addEndingSlash(buildAgentUrl).replaceAll("http(s?):", ":") + "socket/text/ro";
        log.debug("Setting live log websocket url: {}", liveLogWebSocketUrl);
        buildExecutionSession.setLiveLogsUri(Optional.of(new URI(liveLogWebSocketUrl)));
        BuildDriver buildDriver = buildDriverFactory.getBuildDriver();
        RunningBuild runningBuild = buildDriver.startProjectBuild(buildExecutionSession, runningEnvironment, onComplete, onError);
        buildExecutionSession.setCancelHook(runningBuild::cancel);
        buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_WAITING);
    } catch (Throwable e) {
        throw new BuildProcessException(e, runningEnvironment);
    }
    return waitToCompleteFuture;
}
Also used : CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) RepositoryManagerFactory(org.jboss.pnc.executor.servicefactories.RepositoryManagerFactory) StringUtils(org.jboss.pnc.common.util.StringUtils) PreDestroy(javax.annotation.PreDestroy) RepositoryManager(org.jboss.pnc.spi.repositorymanager.RepositoryManager) KeycloakServiceClient(org.jboss.pnc.auth.KeycloakServiceClient) DestroyableEnvironment(org.jboss.pnc.spi.environment.DestroyableEnvironment) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) URI(java.net.URI) EnvironmentDriver(org.jboss.pnc.spi.environment.EnvironmentDriver) BuildDriverFactory(org.jboss.pnc.executor.servicefactories.BuildDriverFactory) RepositoryManagerException(org.jboss.pnc.spi.repositorymanager.RepositoryManagerException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) BuildStatus(org.jboss.pnc.enums.BuildStatus) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) PncConfigProvider(org.jboss.pnc.common.json.moduleprovider.PncConfigProvider) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) MDCExecutors(org.jboss.pnc.common.concurrent.MDCExecutors) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Optional(java.util.Optional) 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) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException) CompletableFuture(java.util.concurrent.CompletableFuture) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) ConfigurationParseException(org.jboss.pnc.common.json.ConfigurationParseException) RepositoryType(org.jboss.pnc.enums.RepositoryType) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent) ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) BuildType(org.jboss.pnc.enums.BuildType) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) Configuration(org.jboss.pnc.common.Configuration) ProcessStageUtils(org.jboss.pnc.common.util.ProcessStageUtils) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) Consumer(java.util.function.Consumer) StartedEnvironment(org.jboss.pnc.spi.environment.StartedEnvironment) AlreadyRunningException(org.jboss.pnc.spi.executor.exceptions.AlreadyRunningException) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) BuildExecutionStatus(org.jboss.pnc.enums.BuildExecutionStatus) EnvironmentDriverFactory(org.jboss.pnc.executor.servicefactories.EnvironmentDriverFactory) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) URI(java.net.URI) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException) BuildDriver(org.jboss.pnc.spi.builddriver.BuildDriver) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 3 with BuildProcessException

use of org.jboss.pnc.executor.exceptions.BuildProcessException in project pnc by project-ncl.

the class DefaultBuildExecutor method retrieveBuildDriverResults.

private Void retrieveBuildDriverResults(BuildExecutionSession buildExecutionSession, CompletedBuild completedBuild) {
    if (completedBuild == null) {
        userLog.warn("Unable to retrieve build driver results. Most likely due to cancelled operation.");
        return null;
    }
    try {
        buildExecutionSession.setStatus(BuildExecutionStatus.COLLECTING_RESULTS_FROM_BUILD_DRIVER);
        BuildDriverResult buildResult = completedBuild.getBuildResult();
        BuildStatus buildStatus = buildResult.getBuildStatus();
        buildExecutionSession.setBuildDriverResult(buildResult);
        if (buildStatus.completedSuccessfully()) {
            userLog.info("Build successfully completed.");
            buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_COMPLETED_SUCCESS);
        } else if (buildStatus.equals(BuildStatus.CANCELLED)) {
            userLog.info("Build has been canceled.");
            buildExecutionSession.setStatus(BuildExecutionStatus.CANCELLED);
        } else {
            userLog.warn("Build completed with errors.");
            buildExecutionSession.setStatus(BuildExecutionStatus.BUILD_COMPLETED_WITH_ERROR);
        }
        return null;
    } catch (Throwable e) {
        throw new BuildProcessException(e, completedBuild.getRunningEnvironment());
    }
}
Also used : BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) BuildStatus(org.jboss.pnc.enums.BuildStatus) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 4 with BuildProcessException

use of org.jboss.pnc.executor.exceptions.BuildProcessException in project pnc by project-ncl.

the class DefaultBuildExecutor method configureRepository.

private RepositorySession configureRepository(DefaultBuildExecutionSession buildExecutionSession) {
    if (buildExecutionSession.isCanceled()) {
        return null;
    }
    ProcessStageUtils.logProcessStageBegin(BuildExecutionStatus.REPO_SETTING_UP.toString(), "Setting up repository...");
    buildExecutionSession.setStatus(BuildExecutionStatus.REPO_SETTING_UP);
    BuildType buildType = buildExecutionSession.getBuildExecutionConfiguration().getBuildType();
    if (buildType == null) {
        throw new BuildProcessException("Missing required value buildExecutionConfiguration.buildType");
    }
    RepositoryType repositoryType = BuildTypeToRepositoryType.getRepositoryType(buildType);
    try {
        RepositoryManager repositoryManager = repositoryManagerFactory.getRepositoryManager(repositoryType);
        BuildExecution buildExecution = buildExecutionSession.getBuildExecutionConfiguration();
        String serviceAccountToken = (serviceClient == null ? null : serviceClient.getAuthToken());
        RepositorySession buildRepository = repositoryManager.createBuildRepositoryWithRetries(buildExecution, buildExecutionSession.getAccessToken(), serviceAccountToken, repositoryType, buildExecutionSession.getBuildExecutionConfiguration().getGenericParameters(), buildExecutionSession.getBuildExecutionConfiguration().isBrewPullActive());
        return buildRepository;
    } catch (Throwable e) {
        throw new BuildProcessException(e);
    } finally {
        ProcessStageUtils.logProcessStageEnd(BuildExecutionStatus.REPO_SETTING_UP.toString(), "Repository setup complete.");
    }
}
Also used : RepositoryType(org.jboss.pnc.enums.RepositoryType) BuildType(org.jboss.pnc.enums.BuildType) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) RepositoryManager(org.jboss.pnc.spi.repositorymanager.RepositoryManager) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Example 5 with BuildProcessException

use of org.jboss.pnc.executor.exceptions.BuildProcessException in project pnc by project-ncl.

the class DefaultBuildExecutor method completeExecution.

private Void completeExecution(DefaultBuildExecutionSession buildExecutionSession, Throwable e) {
    String buildExecutionId = buildExecutionSession.getId();
    try {
        // Ends when the result is stored by the Orchestrator
        ProcessStageUtils.logProcessStageBegin("FINALIZING_BUILD", "Finalizing build ...");
        if (e != null) {
            log.debug("Finalizing FAILED execution. Exception: ", e);
        } else {
            log.debug("Finalizing SUCCESS execution.");
        }
        buildExecutionSession.setStatus(BuildExecutionStatus.FINALIZING_EXECUTION);
        if (buildExecutionSession.getStartTime() == null) {
            buildExecutionSession.setException(new ExecutorException("Missing start time."));
        }
        if (e != null) {
            stopRunningEnvironment(e);
        } else {
            try {
                destroyEnvironment(buildExecutionSession);
            } catch (BuildProcessException destroyException) {
                e = destroyException;
            }
        }
        if (e != null) {
            buildExecutionSession.setException(new ExecutorException(e));
        }
        if (buildExecutionSession.getEndTime() != null) {
            buildExecutionSession.setException(new ExecutorException("End time already set."));
        } else {
            buildExecutionSession.setEndTime(new Date());
        }
        // check if any of previous statuses indicated "failed" state
        if (buildExecutionSession.isCanceled()) {
            buildExecutionSession.setStatus(BuildExecutionStatus.CANCELLED);
            userLog.info("Build execution completed (canceled).");
        } else if (buildExecutionSession.hasFailed()) {
            buildExecutionSession.setStatus(BuildExecutionStatus.DONE_WITH_ERRORS);
            userLog.warn("Build execution completed with errors.");
        } else {
            buildExecutionSession.setStatus(BuildExecutionStatus.DONE);
            userLog.info("Build execution completed successfully.");
        }
        log.debug("Removing buildExecutionTask [" + buildExecutionId + "] from list of running tasks.");
        runningExecutions.remove(buildExecutionId);
        userLog.info("Build execution completed.");
    } catch (Throwable t) {
        userLog.error("Unable to complete execution!", t);
        String executorException = "Unable to recover, see system log for the details.";
        if (t.getMessage() != null) {
            executorException += " " + t.getMessage();
        }
        buildExecutionSession.setException(new ExecutorException(executorException));
        buildExecutionSession.setEndTime(new Date());
        buildExecutionSession.setStatus(BuildExecutionStatus.SYSTEM_ERROR);
        runningExecutions.remove(buildExecutionId);
    } finally {
        RunningEnvironment runningEnvironment = buildExecutionSession.getRunningEnvironment();
        if (runningEnvironment == null) {
            log.warn("Could not close Maven repository session [" + buildExecutionId + "]. Running environment was not set!");
        } else {
            RepositorySession repositorySession = runningEnvironment.getRepositorySession();
            if (repositorySession == null) {
                log.warn("Could not close Maven repository session [" + buildExecutionId + "]. Repository session was not set!");
            } else {
                log.debug("Closing Maven repository session [" + buildExecutionId + "].");
                repositorySession.close();
            }
        }
    }
    return null;
}
Also used : ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) Date(java.util.Date) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) BuildProcessException(org.jboss.pnc.executor.exceptions.BuildProcessException)

Aggregations

BuildProcessException (org.jboss.pnc.executor.exceptions.BuildProcessException)8 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)4 RepositorySession (org.jboss.pnc.spi.repositorymanager.model.RepositorySession)4 Date (java.util.Date)2 BuildStatus (org.jboss.pnc.enums.BuildStatus)2 BuildType (org.jboss.pnc.enums.BuildType)2 RepositoryType (org.jboss.pnc.enums.RepositoryType)2 BuildDriverResult (org.jboss.pnc.spi.builddriver.BuildDriverResult)2 DestroyableEnvironment (org.jboss.pnc.spi.environment.DestroyableEnvironment)2 EnvironmentDriver (org.jboss.pnc.spi.environment.EnvironmentDriver)2 ExecutorException (org.jboss.pnc.spi.executor.exceptions.ExecutorException)2 BuildExecution (org.jboss.pnc.spi.repositorymanager.BuildExecution)2 RepositoryManager (org.jboss.pnc.spi.repositorymanager.RepositoryManager)2 URI (java.net.URI)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Consumer (java.util.function.Consumer)1