private HealthStateType stageScopedHealthState(String pipelineName, String stageName) { return HealthStateType.general(HealthStateScope.forStage(pipelineName, stageName)); }
private boolean stageDoesNotExist(String pipelineName, String counterOrLabel, String stageName, HttpOperationResult result, Stage stage) { if (stage.getState() == Unknown) { String msg = String.format("Stage '%s/%s/%s' not found.", pipelineName, counterOrLabel, stageName); LOGGER.error(msg); result.notFound(msg, "", HealthStateType.general(HealthStateScope.forStage(pipelineName, stageName))); return true; } return false; }
private ServerHealthState stageSchedulingSuccessfulState(String pipelineName, String stageName) { return ServerHealthState.success(HealthStateType.general(HealthStateScope.forStage(pipelineName, stageName))); }
@Test public void shouldHaveUniqueScopeForStages() throws Exception { HealthStateScope scope1 = HealthStateScope.forStage("blahPipeline","blahStage"); HealthStateScope scope2 = HealthStateScope.forStage("blahPipeline","blahStage"); HealthStateScope scope25 = HealthStateScope.forStage("blahPipeline","blahOtherStage"); HealthStateScope scope3 = HealthStateScope.forStage("blahOtherPipeline","blahOtherStage"); assertThat(scope1, is(scope2)); assertThat(scope1, not(scope25)); assertThat(scope1, not(scope3)); }
/** * IMPORTANT: this method is only meant for TOP level usage(never use this within a transaction). It gobbles exception. */ public Stage rerunFailedJobs(final Stage stage, final HttpOperationResult result) { final StageIdentifier identifier = stage.getIdentifier(); JobInstances jobInstances = stage.jobsWithResult(JobResult.Cancelled, JobResult.Failed); List<String> jobNames = jobInstances.stream().map(JobInstance::getName).collect(Collectors.toList()); if (jobNames.isEmpty()) { String message = "There are no failed jobs in the stage that could be re-run"; result.badRequest(message, message, HealthStateType.general(HealthStateScope.forStage(identifier.getPipelineName(), identifier.getStageName()))); return null; } return rerunJobs(stage, jobNames, result); }
/** * Top-level operation only; consumes exceptions */ public Stage rerunStage(String pipelineName, Integer pipelineCounter, String stageName, HttpOperationResult result) { String identifier = StringUtils.join(Arrays.asList(pipelineName, pipelineCounter, stageName), "/"); HealthStateType healthStateType = HealthStateType.general(HealthStateScope.forStage(pipelineName, stageName)); Stage stage = null; try { stage = rerunStage(pipelineName, pipelineCounter, stageName, new ResultUpdatingErrorHandler(result)); if (result.isSuccess()) { // this check is for clarity, but is not needed as failures result in RunTimeExceptions result.accepted(String.format("Request to schedule stage %s accepted", identifier), "", healthStateType); } } catch (RuntimeException e) { // if the result is successful, but there's still an exception, treat it as a 500 // else the result is assumed to contain the right status code and error message if (result.isSuccess()) { String message = String.format( "Stage rerun request for stage [%s] could not be completed " + "because of an unexpected failure. Cause: %s", identifier, e.getMessage() ); LOGGER.error(message, e); result.internalServerError(message, healthStateType); } } return stage; }
public String rerunSelectedJobs(Request req, Response res) throws IOException { HttpOperationResult result = new HttpOperationResult(); haltIfRequestBodyDoesNotContainPropertyJobs(req); JsonReader requestBody = GsonTransformer.getInstance().jsonReaderFrom(req.body()); List<String> requestedJobs = requestBody.readStringArrayIfPresent(JOB_NAMES_PROPERTY).get(); Optional<Stage> optionalStage = getStageFromRequestParam(req, result); optionalStage.ifPresent(stage -> { HealthStateType healthStateType = HealthStateType.general(HealthStateScope.forStage(stage.getIdentifier() .getPipelineName(), stage.getName())); Set<String> jobsInStage = stage.getJobInstances() .stream() .map(JobInstance::getName) .collect(Collectors.toSet()); List<String> unknownJobs = requestedJobs.stream() .filter(jobToRun -> !jobsInStage.contains(jobToRun)) .collect(Collectors.toList()); if (unknownJobs.isEmpty()) { scheduleService.rerunJobs(stage, requestedJobs, result); } else { String msg = String.format("Job(s) %s does not exist in stage '%s'.", unknownJobs, stage.getIdentifier().getStageLocator()); result.notFound(msg, "", healthStateType); } }); return renderHTTPOperationResult(result, req, res); }
private ServerHealthState stageSchedulingFailedState(String pipelineName, CannotScheduleException e) { return ServerHealthState.failedToScheduleStage(HealthStateType.general(HealthStateScope.forStage(pipelineName, e.getStageName())), pipelineName, e.getStageName(), e.getMessage()); }
@Test public void shouldRemoveScopeWhenStageIsRemovedFromConfig() throws Exception { CruiseConfig config = GoConfigMother.pipelineHavingJob("blahPipeline", "blahStage", "blahJob", "fii", "baz"); assertThat(HealthStateScope.forPipeline("fooPipeline").isRemovedFromConfig(config),is(true)); assertThat(HealthStateScope.forPipeline("blahPipeline").isRemovedFromConfig(config),is(false)); assertThat(HealthStateScope.forStage("fooPipeline","blahStage").isRemovedFromConfig(config),is(true)); assertThat(HealthStateScope.forStage("blahPipeline","blahStageRemoved").isRemovedFromConfig(config),is(true)); assertThat(HealthStateScope.forStage("blahPipeline","blahStage").isRemovedFromConfig(config),is(false)); }
HealthStateType healthStateForStage = HealthStateType.general(HealthStateScope.forStage(identifier.getPipelineName(), identifier.getStageName()));
@Test public void stateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forPipeline(PIPELINE_NAME)))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forStage(PIPELINE_NAME, "stage1")))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); serverHealthService = new ServerHealthService(); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forJob(PIPELINE_NAME, "stage1", "job1")))); assertThat((serverHealthService.logs().get(0)).getPipelineNames(config), equalTo(Collections.singleton(PIPELINE_NAME))); }