Search in sources :

Example 1 with CreateAgentMessage

use of com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage 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 (!map.containsKey(jobPlan.getJobId())) {
                continue;
            }
            long lastTryTime = map.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(Collectors.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) {
        map.put(plan.getJobId(), timeProvider.currentTimeMillis());
        if (elasticAgentPluginRegistry.has(plan.getElasticProfile().getPluginId())) {
            String environment = environmentConfigService.envForPipeline(plan.getPipelineName());
            createAgentQueue.post(new CreateAgentMessage(goConfigService.serverConfig().getAgentAutoRegisterKey(), environment, plan.getElasticProfile(), plan.getIdentifier()), messageTimeToLive);
            serverHealthService.removeByScope(HealthStateScope.forJob(plan.getIdentifier().getPipelineName(), plan.getIdentifier().getStageName(), plan.getIdentifier().getBuildName()));
        } else {
            String jobConfigIdentifier = plan.getIdentifier().jobConfigIdentifier().toString();
            String description = String.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.", plan.getElasticProfile().getPluginId(), jobConfigIdentifier);
            serverHealthService.update(ServerHealthState.error(String.format("Unable to find agent for %s", jobConfigIdentifier), description, HealthStateType.general(HealthStateScope.forJob(plan.getIdentifier().getPipelineName(), plan.getIdentifier().getStageName(), plan.getIdentifier().getBuildName()))));
            LOGGER.error(description);
        }
    }
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) JobPlan(com.thoughtworks.go.domain.JobPlan)

Example 2 with CreateAgentMessage

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

the class ElasticAgentPluginServiceTest method shouldRetryCreateAgentForJobThatHasBeenWaitingForAnAgentForALongTime.

@Test
public void shouldRetryCreateAgentForJobThatHasBeenWaitingForAnAgentForALongTime() {
    when(goConfigService.elasticJobStarvationThreshold()).thenReturn(0L);
    JobPlan plan1 = plan(1, "docker");
    ArgumentCaptor<CreateAgentMessage> captor = ArgumentCaptor.forClass(CreateAgentMessage.class);
    ArgumentCaptor<Long> ttl = ArgumentCaptor.forClass(Long.class);
    service.createAgentsFor(new ArrayList<>(), Arrays.asList(plan1));
    // invoke create again
    service.createAgentsFor(Arrays.asList(plan1), Arrays.asList(plan1));
    verify(createAgentQueue, times(2)).post(captor.capture(), ttl.capture());
    CreateAgentMessage createAgentMessage = captor.getValue();
    assertThat(createAgentMessage.autoregisterKey(), is(autoRegisterKey));
    assertThat(createAgentMessage.pluginId(), is(plan1.getElasticProfile().getPluginId()));
    assertThat(createAgentMessage.configuration(), is(plan1.getElasticProfile().getConfigurationAsMap(true)));
    verifyNoMoreInteractions(createAgentQueue);
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) Test(org.junit.Test)

Example 3 with CreateAgentMessage

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

the class ElasticAgentPluginServiceTest method shouldCreateAgentForNewlyAddedJobPlansOnly.

@Test
public void shouldCreateAgentForNewlyAddedJobPlansOnly() {
    when(goConfigService.elasticJobStarvationThreshold()).thenReturn(10000L);
    JobPlan plan1 = plan(1, "docker");
    JobPlan plan2 = plan(2, "docker");
    ArgumentCaptor<CreateAgentMessage> createAgentMessageArgumentCaptor = ArgumentCaptor.forClass(CreateAgentMessage.class);
    ArgumentCaptor<Long> ttl = ArgumentCaptor.forClass(Long.class);
    when(environmentConfigService.envForPipeline("pipeline-2")).thenReturn("env-2");
    service.createAgentsFor(Arrays.asList(plan1), Arrays.asList(plan1, plan2));
    verify(createAgentQueue).post(createAgentMessageArgumentCaptor.capture(), ttl.capture());
    CreateAgentMessage createAgentMessage = createAgentMessageArgumentCaptor.getValue();
    assertThat(createAgentMessage.autoregisterKey(), is(autoRegisterKey));
    assertThat(createAgentMessage.pluginId(), is(plan2.getElasticProfile().getPluginId()));
    assertThat(createAgentMessage.configuration(), is(plan2.getElasticProfile().getConfigurationAsMap(true)));
    assertThat(createAgentMessage.environment(), is("env-2"));
    assertThat(createAgentMessage.jobIdentifier(), is(plan2.getIdentifier()));
}
Also used : CreateAgentMessage(com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage) Test(org.junit.Test)

Aggregations

CreateAgentMessage (com.thoughtworks.go.server.messaging.elasticagents.CreateAgentMessage)3 Test (org.junit.Test)2 JobPlan (com.thoughtworks.go.domain.JobPlan)1