Search in sources :

Example 1 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class BuildAssignmentServiceTest method shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration.

@Test
void shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration() throws IOException, IllegalArtifactLocationException {
    PipelineConfig pipelineWithElasticJob = PipelineConfigMother.pipelineWithElasticJob(elasticProfileId1);
    JobPlan jobPlan = new InstanceFactory().createJobPlan(pipelineWithElasticJob.first().getJobs().first(), schedulingContext);
    jobPlans.add(jobPlan);
    JobInstance jobInstance = mock(JobInstance.class);
    doThrow(new RulesViolationException("some rules related violation message")).when(elasticAgentPluginService).shouldAssignWork(elasticAgentInstance.elasticAgentMetadata(), null, jobPlan.getElasticProfile(), jobPlan.getClusterProfile(), jobPlan.getIdentifier());
    when(jobInstance.getState()).thenReturn(JobState.Scheduled);
    when(jobInstanceService.buildById(anyLong())).thenReturn(jobInstance);
    buildAssignmentService.onTimer();
    assertThatCode(() -> {
        JobPlan matchingJob = buildAssignmentService.findMatchingJob(elasticAgentInstance);
        assertThat(matchingJob).isNull();
        assertThat(buildAssignmentService.jobPlans()).containsExactly(jobPlan);
    }).doesNotThrowAnyException();
    InOrder inOrder = inOrder(jobInstanceService, scheduleService, consoleService, jobStatusTopic);
    inOrder.verify(jobInstanceService).buildById(jobPlan.getJobId());
    inOrder.verify(consoleService).appendToConsoleLog(jobPlan.getIdentifier(), "\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: some rules related violation message");
    inOrder.verify(scheduleService).failJob(jobInstance);
    inOrder.verify(jobStatusTopic).post(new JobStatusMessage(jobPlan.getIdentifier(), JobState.Scheduled, elasticAgentInstance.getUuid()));
}
Also used : InOrder(org.mockito.InOrder) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Example 2 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class BuildRepositoryRemoteImpl method reportCompleted.

public void reportCompleted(final AgentRuntimeInfo agentRuntimeInfo, final JobIdentifier jobIdentifier, final JobResult result) {
    final JobState state = JobState.Completed;
    handleFailuresDuringReporting(agentRuntimeInfo, jobIdentifier, "status and result", String.format("%s, %s", state, result), () -> {
        // TODO: may be i don't belong here, ping already updates agent runtime info
        agentService.updateRuntimeInfo(agentRuntimeInfo);
        buildRepositoryService.completing(jobIdentifier, result, agentRuntimeInfo.getUUId());
        buildRepositoryService.updateStatusFromAgent(jobIdentifier, state, agentRuntimeInfo.getUUId());
        jobStatusTopic.post(new JobStatusMessage(jobIdentifier, state, agentRuntimeInfo.getUUId()));
    });
}
Also used : JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) JobState(com.thoughtworks.go.domain.JobState)

Example 3 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class ElasticAgentPluginService method createAgentsFor.

public void createAgentsFor(List<JobPlan> old, List<JobPlan> newPlan) {
    Collection<JobPlan> starvingJobs = new ArrayList<>();
    for (JobPlan jobPlan : newPlan) {
        if (jobPlan.requiresElasticAgent()) {
            if (!jobCreationTimeMap.containsKey(jobPlan.getJobId())) {
                continue;
            }
            long lastTryTime = jobCreationTimeMap.get(jobPlan.getJobId());
            if ((timeProvider.currentTimeMillis() - lastTryTime) >= goConfigService.elasticJobStarvationThreshold()) {
                starvingJobs.add(jobPlan);
            }
        }
    }
    ArrayList<JobPlan> jobsThatRequireAgent = new ArrayList<>();
    jobsThatRequireAgent.addAll(Sets.difference(new HashSet<>(newPlan), new HashSet<>(old)));
    jobsThatRequireAgent.addAll(starvingJobs);
    List<JobPlan> plansThatRequireElasticAgent = jobsThatRequireAgent.stream().filter(isElasticAgent()).collect(toList());
    // messageTimeToLive is lesser than the starvation threshold to ensure there are no duplicate create agent message
    long messageTimeToLive = goConfigService.elasticJobStarvationThreshold() - 10000;
    for (JobPlan plan : plansThatRequireElasticAgent) {
        jobCreationTimeMap.put(plan.getJobId(), timeProvider.currentTimeMillis());
        ElasticProfile elasticProfile = plan.getElasticProfile();
        ClusterProfile clusterProfile = plan.getClusterProfile();
        JobIdentifier jobIdentifier = plan.getIdentifier();
        if (clusterProfile == null) {
            String cancellationMessage = "\nThis job was cancelled by GoCD. The version of your GoCD server requires elastic profiles to be associated with a cluster(required from Version 19.3.0). " + "This job is configured to run on an Elastic Agent, but the associated elastic profile does not have information about the cluster.  \n\n" + "The possible reason for the missing cluster information on the elastic profile could be, an upgrade of the GoCD server to a version >= 19.3.0 before the completion of the job.\n\n" + "A re-run of this job should fix this issue.";
            logToJobConsole(jobIdentifier, cancellationMessage);
            scheduleService.cancelJob(jobIdentifier);
        } else if (elasticAgentPluginRegistry.has(clusterProfile.getPluginId())) {
            String environment = environmentConfigService.envForPipeline(plan.getPipelineName());
            try {
                resolveSecrets(clusterProfile, elasticProfile);
                createAgentQueue.post(new CreateAgentMessage(ephemeralAutoRegisterKeyService.autoRegisterKey(), environment, elasticProfile, clusterProfile, jobIdentifier), messageTimeToLive);
                serverHealthService.removeByScope(scopeForJob(jobIdentifier));
            } catch (RulesViolationException | SecretResolutionFailureException e) {
                JobInstance jobInstance = jobInstanceSqlMapDao.buildById(plan.getJobId());
                String failureMessage = format("\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: %s", e.getMessage());
                logToJobConsole(jobIdentifier, failureMessage);
                scheduleService.failJob(jobInstance);
                jobStatusTopic.post(new JobStatusMessage(jobIdentifier, jobInstance.getState(), plan.getAgentUuid()));
            }
        } else {
            String jobConfigIdentifier = jobIdentifier.jobConfigIdentifier().toString();
            String description = format("Plugin [%s] associated with %s is missing. Either the plugin is not " + "installed or could not be registered. Please check plugins tab " + "and server logs for more details.", clusterProfile.getPluginId(), jobConfigIdentifier);
            serverHealthService.update(error(format("Unable to find agent for %s", jobConfigIdentifier), description, general(scopeForJob(jobIdentifier))));
            LOGGER.error(description);
        }
    }
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) JobPlan(com.thoughtworks.go.domain.JobPlan) JobInstance(com.thoughtworks.go.domain.JobInstance) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 4 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class ScheduleServiceIntegrationTest method shouldUseEnvVariableFromNewConfigWhenAPipelineIsRetriggered.

// #6826
@Test
public void shouldUseEnvVariableFromNewConfigWhenAPipelineIsRetriggered() {
    String pipelineName = "p1";
    String stage = "s1";
    String job = "j1";
    PipelineConfig pipelineConfig = configHelper.addPipeline(pipelineName, stage, job);
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1", "V1"))));
    configHelper.addEnvironmentVariableToStage(pipelineName, stage, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K2", "V2"))));
    configHelper.addEnvironmentVariableToJob(pipelineName, stage, job, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K3", "V3"))));
    Pipeline pipeline = runAndPass(pipelineConfig, 1);
    long jobId = pipeline.getFirstStage().getFirstJob().getId();
    JobPlan jobPlan = jobInstanceDao.loadPlan(jobId);
    EnvironmentVariables variables = jobPlan.getVariables();
    assertThat(variables, hasItems(new EnvironmentVariable("K1", "V1"), new EnvironmentVariable("K2", "V2"), new EnvironmentVariable("K3", "V3")));
    Pipeline successfulPipeline = pass(pipeline);
    JobInstance jobInstance = successfulPipeline.getFirstStage().getFirstJob();
    jobStatusListener.onMessage(new JobStatusMessage(jobInstance.getIdentifier(), jobInstance.getState(), jobInstance.getAgentUuid()));
    jobPlan = jobInstanceDao.loadPlan(jobId);
    assertThat(jobPlan.getVariables().size(), is(0));
    // add
    configHelper.addEnvironmentVariableToPipeline(pipelineName, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K1_updated", "V1_updated"))));
    // delete
    configHelper.addEnvironmentVariableToStage(pipelineName, stage, new EnvironmentVariablesConfig());
    // edit
    configHelper.addEnvironmentVariableToJob(pipelineName, stage, job, new EnvironmentVariablesConfig(Arrays.asList(new EnvironmentVariableConfig("K3", "V3_updated"))));
    pipeline = runAndPass(pipelineConfig, 2);
    jobId = pipeline.getFirstStage().getFirstJob().getId();
    jobPlan = jobInstanceDao.loadPlan(jobId);
    variables = jobPlan.getVariables();
    assertThat(variables, hasItems(new EnvironmentVariable("K1_updated", "V1_updated"), new EnvironmentVariable("K3", "V3_updated")));
}
Also used : JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) Test(org.junit.jupiter.api.Test)

Example 5 with JobStatusMessage

use of com.thoughtworks.go.server.messaging.JobStatusMessage in project gocd by gocd.

the class BuildRepositoryRemoteImpl method reportCurrentStatus.

public void reportCurrentStatus(final AgentRuntimeInfo agentRuntimeInfo, final JobIdentifier jobIdentifier, final JobState state) {
    handleFailuresDuringReporting(agentRuntimeInfo, jobIdentifier, "status", state.toString(), () -> {
        // TODO: may be i don't belong here, ping already updates agent runtime info
        agentService.updateRuntimeInfo(agentRuntimeInfo);
        buildRepositoryService.updateStatusFromAgent(jobIdentifier, state, agentRuntimeInfo.getUUId());
        jobStatusTopic.post(new JobStatusMessage(jobIdentifier, state, agentRuntimeInfo.getUUId()));
    });
}
Also used : JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage)

Aggregations

JobStatusMessage (com.thoughtworks.go.server.messaging.JobStatusMessage)10 Test (org.junit.jupiter.api.Test)7 ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)2 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)2 CreateAgentMessage (com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage)2 InOrder (org.mockito.InOrder)2 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 JobInstance (com.thoughtworks.go.domain.JobInstance)1 JobPlan (com.thoughtworks.go.domain.JobPlan)1 JobState (com.thoughtworks.go.domain.JobState)1 ElasticAgentPluginRegistry (com.thoughtworks.go.plugin.access.elastic.ElasticAgentPluginRegistry)1 SecretResolutionFailureException (com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)1 BuildWork (com.thoughtworks.go.remote.work.BuildWork)1 Work (com.thoughtworks.go.remote.work.Work)1 RulesViolationException (com.thoughtworks.go.server.exceptions.RulesViolationException)1 CreateAgentQueueHandler (com.thoughtworks.go.server.messaging.elasticagents.CreateAgentQueueHandler)1 HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)1 TimeProvider (com.thoughtworks.go.util.TimeProvider)1