Search in sources :

Example 1 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession 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 BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession 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 BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession in project pnc by project-ncl.

the class BuildExecutorMock method cancel.

@Override
public void cancel(String executionConfigurationId) throws ExecutorException {
    BuildExecutionSession buildExecutionSession = runningExecutions.get(executionConfigurationId);
    if (buildExecutionSession == null) {
        log.error("Unable to cancel build {}. The build is not running.", executionConfigurationId);
        return;
    }
    log.info("Cancelling build {}.", executionConfigurationId);
    runningFutures.get(executionConfigurationId).cancel(true);
    BuildDriverResult driverResult = BuildDriverResultMock.mockResult(BuildStatus.CANCELLED);
    buildExecutionSession.setBuildDriverResult(driverResult);
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult)

Example 4 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession in project pnc by project-ncl.

the class BuildExecutorMock method startBuilding.

// @Deprecated //CDI workaround
// public BuildExecutorMock() {
// }
// 
// @Inject
// public BuildExecutorMock(RepositoryManagerFactory repositoryManagerFactory, BuildDriverFactory
// buildDriverFactory, EnvironmentDriverFactory environmentDriverFactory, Configuration configuration) {
// 
// }
@Override
public BuildExecutionSession startBuilding(BuildExecutionConfiguration buildExecutionConfiguration, Consumer<BuildExecutionStatusChangedEvent> onBuildExecutionStatusChangedEvent, String accessToken) throws ExecutorException {
    log.info("Starting mock build execution for buildExecutionConfiguration.id {}", buildExecutionConfiguration.getId());
    BuildExecutionSession buildExecutionSession = new BuildExecutionSessionMock(buildExecutionConfiguration, onBuildExecutionStatusChangedEvent);
    buildExecutionSession.setStatus(BuildExecutionStatus.NEW);
    runningExecutions.put(buildExecutionConfiguration.getId(), buildExecutionSession);
    Consumer<BuildExecutionStatus> onCompleteInternal = (buildStatus) -> {
        log.debug("Removing buildExecutionConfiguration.id [" + buildExecutionConfiguration.getId() + "] form list of running tasks.");
        runningExecutions.remove(buildExecutionConfiguration.getId());
        buildExecutionSession.setStatus(buildStatus);
    };
    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> mockBuild(buildExecutionSession), executor).handleAsync((buildPassed, e) -> complete(buildPassed, e, onCompleteInternal), executor);
    runningFutures.put(buildExecutionConfiguration.getId(), future);
    return buildExecutionSession;
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Logger(org.slf4j.Logger) RepositoryManagerResultMock(org.jboss.pnc.mock.repositorymanager.RepositoryManagerResultMock) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) BuildStatus(org.jboss.pnc.enums.BuildStatus) BuildDriverResultMock(org.jboss.pnc.mock.builddriver.BuildDriverResultMock) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) Consumer(java.util.function.Consumer) MDCExecutors(org.jboss.pnc.common.concurrent.MDCExecutors) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) Map(java.util.Map) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) NamedThreadFactory(org.jboss.pnc.common.concurrent.NamedThreadFactory) BuildExecutionStatus(org.jboss.pnc.enums.BuildExecutionStatus) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) ExecutorService(java.util.concurrent.ExecutorService) BuildExecutionStatus(org.jboss.pnc.enums.BuildExecutionStatus)

Example 5 with BuildExecutionSession

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

Aggregations

BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)15 Consumer (java.util.function.Consumer)11 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)9 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)8 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)8 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)8 Status (org.jboss.pnc.buildagent.api.Status)6 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)6 Inject (javax.inject.Inject)5 TaskStatusUpdateEvent (org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent)5 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)5 BuildExecutor (org.jboss.pnc.spi.executor.BuildExecutor)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 ExecutorService (java.util.concurrent.ExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ApplicationScoped (javax.enterprise.context.ApplicationScoped)4