public Work assignWorkToAgent(AgentIdentifier agent) { return assignWorkToAgent(agentService.findAgentAndRefreshStatus(agent.getUuid())); }
public void onTimer() { if (maintenanceModeService.isMaintenanceMode()) { LOGGER.debug("[Maintenance Mode] GoCD server is in 'maintenance' mode, skip checking build assignments"); return; } reloadJobPlans(); matchingJobForRegisteredAgents(); }
public void initialize() { goConfigService.register(this); goConfigService.register(pipelineConfigChangedListener()); }
private void matchingJobForRegisteredAgents() { Map<String, Agent> agents = agentRemoteHandler.connectedAgents(); if (agents.isEmpty()) { return; } Long start = System.currentTimeMillis(); for (Map.Entry<String, Agent> entry : agents.entrySet()) { String agentUUId = entry.getKey(); Agent agent = entry.getValue(); AgentInstance agentInstance = agentService.findAgentAndRefreshStatus(agentUUId); if (!agentInstance.isRegistered()) { agent.send(new Message(Action.reregister)); continue; } if (agentInstance.isDisabled() || !agentInstance.isIdle()) { LOGGER.debug("Ignore agent [{}] that is {} and {}", agentInstance.getAgentIdentifier(), agentInstance.getRuntimeStatus(), agentInstance.getAgentConfigStatus()); continue; } Work work = assignWorkToAgent(agentInstance); if (work != NO_WORK) { if (agentInstance.getSupportsBuildCommandProtocol()) { BuildSettings buildSettings = createBuildSettings(((BuildWork) work).getAssignment()); agent.send(new Message(Action.build, MessageEncoding.encodeData(buildSettings))); } else { agent.send(new Message(Action.assignWork, MessageEncoding.encodeWork(work))); } } } LOGGER.debug("Matching {} agents with {} jobs took: {}ms", agents.size(), jobPlans.size(), System.currentTimeMillis() - start); }
final JobPlan job = findMatchingJob(agent); if (job != null) { Work buildWork = createWork(agent, job); AgentBuildingInfo buildingInfo = new AgentBuildingInfo(job.getIdentifier().buildLocatorForDisplay(), job.getIdentifier().buildLocator());
private Work createWork(final AgentInstance agent, final JobPlan job) { try { return (Work) transactionTemplate.transactionSurrounding(() -> { final String agentUuid = agent.getUuid(); //TODO: Use fullPipeline and get the Stage from it? final Pipeline pipeline; try { pipeline = scheduledPipelineLoader.pipelineWithPasswordAwareBuildCauseByBuildId(job.getJobId()); } catch (StaleMaterialsOnBuildCause e) { return NO_WORK; } List<Task> tasks = goConfigService.tasksForJob(pipeline.getName(), job.getIdentifier().getStageName(), job.getName()); final List<Builder> builders = builderFactory.buildersForTasks(pipeline, tasks, resolver); return transactionTemplate.execute((TransactionCallback) status -> { if (scheduleService.updateAssignedInfo(agentUuid, job)) { return NO_WORK; } EnvironmentVariableContext contextFromEnvironment = environmentConfigService.environmentVariableContextFor(job.getIdentifier().getPipelineName()); final ArtifactStores requiredArtifactStores = goConfigService.artifactStores().getArtifactStores(getArtifactStoreIdsRequiredByArtifactPlans(job.getArtifactPlans())); BuildAssignment buildAssignment = BuildAssignment.create(job, pipeline.getBuildCause(), builders, pipeline.defaultWorkingFolder(), contextFromEnvironment, requiredArtifactStores); return new BuildWork(buildAssignment, systemEnvironment.consoleLogCharset()); }); }); } catch (PipelineNotFoundException e) { removeJobIfNotPresentInCruiseConfig(goConfigService.getCurrentConfig(), job); throw e; } }
public void onConfigChange(CruiseConfig newCruiseConfig) { LOGGER.info("[Configuration Changed] Removing jobs for pipelines that no longer exist in configuration."); synchronized (this) { List<JobPlan> jobsToRemove = new ArrayList<>(); for (JobPlan jobPlan : jobPlans) { if (!newCruiseConfig.hasBuildPlan(new CaseInsensitiveString(jobPlan.getPipelineName()), new CaseInsensitiveString(jobPlan.getStageName()), jobPlan.getName(), true)) { jobsToRemove.add(jobPlan); } } forAllDo(jobsToRemove, o -> removeJob((JobPlan) o)); } }
agentService.initialize(); pipelineLockService.initialize(); buildAssignmentService.initialize(); materialUpdateService.initialize(); pipelineLabelCorrector.correctPipelineLabelCountEntries();
private void removeJobIfNotPresentInCruiseConfig(CruiseConfig newCruiseConfig, JobPlan jobPlan) { if (!newCruiseConfig.hasBuildPlan(new CaseInsensitiveString(jobPlan.getPipelineName()), new CaseInsensitiveString(jobPlan.getStageName()), jobPlan.getName(), true)) { removeJob(jobPlan); } }
public void onMessage(IdleAgentMessage idleAgentMessage) { AgentIdentifier agent = idleAgentMessage.getAgentIdentifier(); Work work = null; long startTime = System.currentTimeMillis(); try { work = buildAssignmentService.assignWorkToAgent(agent); } finally { if (work == null) { work = NO_WORK; } workAssignmentPerformanceLogger.assignedWorkToAgent(work, agent, startTime, System.currentTimeMillis()); try { assignedWorkTopic.post(new WorkAssignedMessage(agent, work)); } catch (Throwable e) { LOGGER.error(FATAL, null, e); } } }