Search in sources :

Example 1 with CompletedBuild

use of org.jboss.pnc.spi.builddriver.CompletedBuild 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 2 with CompletedBuild

use of org.jboss.pnc.spi.builddriver.CompletedBuild in project pnc by project-ncl.

the class DefaultBuildExecutor method startBuilding.

@Override
public BuildExecutionSession startBuilding(BuildExecutionConfiguration buildExecutionConfiguration, Consumer<BuildExecutionStatusChangedEvent> onBuildExecutionStatusChangedEvent, String accessToken) throws ExecutorException {
    DefaultBuildExecutionSession buildExecutionSession = new DefaultBuildExecutionSession(buildExecutionConfiguration, onBuildExecutionStatusChangedEvent);
    String executionConfigurationId = buildExecutionConfiguration.getId();
    DefaultBuildExecutionSession existing = runningExecutions.putIfAbsent(executionConfigurationId, buildExecutionSession);
    if (existing != null) {
        throw new AlreadyRunningException("Build execution with id: " + executionConfigurationId + " is already running.");
    }
    buildExecutionSession.setStartTime(new Date());
    userLog.info("Starting build execution...");
    buildExecutionSession.setStatus(BuildExecutionStatus.NEW);
    buildExecutionSession.setAccessToken(accessToken);
    DebugData debugData = new DebugData(buildExecutionConfiguration.isPodKeptOnFailure());
    CompletableFuture.supplyAsync(() -> configureRepository(buildExecutionSession), executor).thenComposeAsync(repositoryConfiguration -> setUpEnvironment(buildExecutionSession, repositoryConfiguration, debugData), executor).thenComposeAsync(nul -> runTheBuild(buildExecutionSession), executor).thenApplyAsync(completedBuild -> {
        buildExecutionSession.setCancelHook(null);
        return optionallyEnableSsh(buildExecutionSession, completedBuild);
    }, executor).thenApplyAsync(completedBuild -> retrieveBuildDriverResults(buildExecutionSession, completedBuild), executor).thenApplyAsync(nul -> retrieveRepositoryManagerResults(buildExecutionSession), executor).handleAsync((nul, e) -> {
        // make sure there are no references left
        buildExecutionSession.setCancelHook(null);
        return completeExecution(buildExecutionSession, e);
    }, executor);
    // TODO re-connect running instances in case of crash
    return buildExecutionSession;
}
Also used : AlreadyRunningException(org.jboss.pnc.spi.executor.exceptions.AlreadyRunningException) 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) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Date(java.util.Date)

Example 3 with CompletedBuild

use of org.jboss.pnc.spi.builddriver.CompletedBuild in project pnc by project-ncl.

the class LivenessProbeTest method shouldFailTheBuildWhenAgentIsNotResponding.

@Test
public void shouldFailTheBuildWhenAgentIsNotResponding() throws InterruptedException, BuildDriverException {
    TermdBuildDriverModuleConfig buildDriverModuleConfig = mock(TermdBuildDriverModuleConfig.class);
    doReturn(200L).when(buildDriverModuleConfig).getLivenessProbeFrequencyMillis();
    doReturn(500L).when(buildDriverModuleConfig).getLivenessFailTimeoutMillis();
    ClientMockFactory buildAgentClientMockFactory = new ClientMockFactory();
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, buildAgentClientMockFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    RunningEnvironment runningEnvironment = mock(RunningEnvironment.class);
    doReturn(Paths.get("")).when(runningEnvironment).getWorkingDirectory();
    doReturn(new DebugData(false)).when(runningEnvironment).getDebugData();
    doReturn("http://localhost/").when(runningEnvironment).getInternalBuildAgentUrl();
    doReturn(runningEnvironment).when(buildExecution).getRunningEnvironment();
    BlockingQueue<Throwable> result = new ArrayBlockingQueue(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> Assert.fail("Build should complete with error.");
    Consumer<Throwable> onError = (throwable) -> {
        try {
            result.put(throwable);
        } catch (InterruptedException e) {
            Assert.fail("Error in the test. Unable to add the result to queue.");
        }
    };
    // when
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, runningEnvironment, onComplete, onError);
    // then
    Throwable throwable = result.poll(1, TimeUnit.SECONDS);
    Assert.assertNotNull("It should complete with an exception.", throwable);
    Assert.assertEquals("Build Agent has gone away.", throwable.getMessage());
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Logger(org.slf4j.Logger) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) LoggerFactory(org.slf4j.LoggerFactory) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) BlockingQueue(java.util.concurrent.BlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Paths(java.nio.file.Paths) DebugData(org.jboss.pnc.spi.builddriver.DebugData) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) Assert(org.junit.Assert) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.doReturn(org.mockito.Mockito.doReturn) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) DebugData(org.jboss.pnc.spi.builddriver.DebugData) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Test(org.junit.Test)

Example 4 with CompletedBuild

use of org.jboss.pnc.spi.builddriver.CompletedBuild in project pnc by project-ncl.

the class TermdBuildDriverTest method shouldStartAndCancelTheExecutionImmediately.

@Test(timeout = 5_000)
public void shouldStartAndCancelTheExecutionImmediately() throws ConfigurationParseException, BuildDriverException, InterruptedException, IOException {
    // given
    String dirName = "test-workdir";
    String logStart = "Running the command...";
    String logEnd = "Command completed.";
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, clientFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn("echo \"" + logStart + "\"; mvn validate; echo \"" + logEnd + "\";").when(buildExecutionConfiguration).getBuildScript();
    doReturn(dirName).when(buildExecutionConfiguration).getName();
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    AtomicReference<CompletedBuild> buildResult = new AtomicReference<>();
    // when
    CountDownLatch latch = new CountDownLatch(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        buildResult.set(completedBuild);
        latch.countDown();
    };
    Consumer<Throwable> onError = (throwable) -> {
        logger.error("Error received: ", throwable);
        fail(throwable.getMessage());
    };
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer, onComplete, onError);
    runningBuild.cancel();
    latch.await();
    // then
    assertThat(buildResult.get().getBuildResult().getBuildLog()).doesNotContain(logEnd);
    assertThat(buildResult.get().getBuildResult().getBuildStatus()).isEqualTo(CANCELLED);
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) CANCELLED(org.jboss.pnc.enums.BuildStatus.CANCELLED) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationParseException(org.jboss.pnc.common.json.ConfigurationParseException) Assert.fail(org.junit.Assert.fail) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) Path(java.nio.file.Path) Mockito.doReturn(org.mockito.Mockito.doReturn) Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) Files(java.nio.file.Files) MethodHandles(java.lang.invoke.MethodHandles) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) IOException(java.io.IOException) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Status(org.jboss.pnc.buildagent.api.Status) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with CompletedBuild

use of org.jboss.pnc.spi.builddriver.CompletedBuild 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)

Aggregations

CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)11 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)10 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)9 Consumer (java.util.function.Consumer)8 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)8 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)8 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)7 Status (org.jboss.pnc.buildagent.api.Status)5 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)5 DebugData (org.jboss.pnc.spi.builddriver.DebugData)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BuildStatus (org.jboss.pnc.enums.BuildStatus)4 Paths (java.nio.file.Paths)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutorService (java.util.concurrent.ExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3