public Stage scheduleStage(final Pipeline pipeline, final String stageName, final String username, final StageInstanceCreator creator, final ErrorConditionHandler errorHandler) {
return (Stage) transactionTemplate.execute((TransactionCallback) status -> {
String pipelineName = pipeline.getName();
PipelineConfig pipelineConfig = goConfigService.pipelineConfigNamed(new CaseInsensitiveString(pipelineName));
StageConfig stageConfig = pipelineConfig.findBy(new CaseInsensitiveString(stageName));
if (stageConfig == null) {
throw new StageNotFoundException(pipelineName, stageName);
}
SchedulingContext context = schedulingContext(username, pipelineConfig, stageConfig);
pipelineLockService.lockIfNeeded(pipeline);
Stage instance = null;
try {
instance = creator.create(pipelineName, stageName, context);
LOGGER.info("[Stage Schedule] Scheduling stage {} for pipeline {}", stageName, pipeline.getName());
} catch (CannotScheduleException e) {
serverHealthService.update(stageSchedulingFailedState(pipelineName, e));
errorHandler.cantSchedule(e, pipelineName);
}
serverHealthService.update(stageSchedulingSuccessfulState(pipelineName, stageName));
stageService.save(pipeline, instance);
return instance;
});
}