public void canProduce(PipelineConfig pipelineConfig, SchedulingCheckerService schedulingChecker, ServerHealthService serverHealthService, OperationResult operationResult) { schedulingChecker.canTriggerPipelineWithTimer(pipelineConfig, operationResult); if (!operationResult.canContinue()) { ServerHealthState serverHealthState = operationResult.getServerHealthState(); LOGGER.info("'{}' because '{}'", serverHealthState.getMessage(), serverHealthState.getDescription()); } else { TimerConfig timer = pipelineConfig.getTimer(); String timerSpec = timer == null ? "Missing timer spec" : timer.getTimerSpec(); LOGGER.info("Timer scheduling pipeline '{}' using spec '{}'", pipelineConfig.name(), timerSpec); } }
private void scheduleJob(Scheduler scheduler, PipelineConfig pipelineConfig) { TimerConfig timer = pipelineConfig.getTimer(); if (timer != null) { try { CronTrigger trigger = newTrigger() .withIdentity(triggerKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP)) .withSchedule(cronSchedule(new CronExpression(timer.getTimerSpec()))) .build(); JobDetail jobDetail = newJob() .withIdentity(jobKey(CaseInsensitiveString.str(pipelineConfig.name()), PIPELINE_TRIGGGER_TIMER_GROUP)) .ofType(SchedulePipelineQuartzJob.class) .usingJobData(jobDataMapFor(pipelineConfig)) .build(); scheduler.scheduleJob(jobDetail, trigger); LOG.info("Initialized timer for pipeline {} with {}", pipelineConfig.name(), timer.getTimerSpec()); } catch (ParseException e) { showPipelineError(pipelineConfig, e, "Bad timer specification for timer in Pipeline: " + pipelineConfig.name(), "Cannot schedule pipeline using the timer"); } catch (SchedulerException e) { showPipelineError(pipelineConfig, e, "Could not register pipeline '" + pipelineConfig.name() + "' with timer", ""); } } }
@Test public void shouldSetPipelineConfigFromConfigAttributesForTimerConfig() { PipelineConfig pipelineConfig = new PipelineConfig(); HashMap timerConfigMap = new HashMap(); String cronSpec = "0 0 11 * * ?"; timerConfigMap.put(TimerConfig.TIMER_SPEC, cronSpec); timerConfigMap.put(TimerConfig.TIMER_ONLY_ON_CHANGES, "1"); Map configMap = new HashMap(); configMap.put(PipelineConfig.LABEL_TEMPLATE, "LABEL123-${COUNT}"); configMap.put(PipelineConfig.TIMER_CONFIG, timerConfigMap); pipelineConfig.setConfigAttributes(configMap); assertThat(pipelineConfig.getLabelTemplate(), is("LABEL123-${COUNT}")); assertThat(pipelineConfig.getTimer().getTimerSpec(), is(cronSpec)); assertThat(pipelineConfig.getTimer().shouldTriggerOnlyOnChanges(), is(true)); }
@Test public void shouldSetPipelineConfigFromConfigAttributes() { PipelineConfig pipelineConfig = new PipelineConfig(); HashMap mingleConfigMap = new HashMap(); mingleConfigMap.put("mingleconfig", "mingleconfig"); HashMap trackingToolMap = new HashMap(); trackingToolMap.put("trackingtool", "trackingtool"); HashMap timerConfigMap = new HashMap(); String cronSpec = "0 0 11 * * ?"; timerConfigMap.put(TimerConfig.TIMER_SPEC, cronSpec); Map configMap = new HashMap(); configMap.put(PipelineConfig.LABEL_TEMPLATE, "LABEL123-${COUNT}"); configMap.put(PipelineConfig.MINGLE_CONFIG, mingleConfigMap); configMap.put(PipelineConfig.TRACKING_TOOL, trackingToolMap); configMap.put(PipelineConfig.TIMER_CONFIG, timerConfigMap); pipelineConfig.setConfigAttributes(configMap); assertThat(pipelineConfig.getLabelTemplate(), is("LABEL123-${COUNT}")); assertThat(pipelineConfig.getTimer().getTimerSpec(), is(cronSpec)); assertThat(pipelineConfig.getTimer().shouldTriggerOnlyOnChanges(), is(false)); }
@Test public void shouldLoadPipelineWithTimer() throws Exception { CruiseConfig config = ConfigMigrator.loadWithMigration(PIPELINE_WITH_TIMER).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("pipeline")); assertThat(pipelineConfig.getTimer(), is(new TimerConfig("0 15 10 ? * MON-FRI", false))); }
public static void toJSON(OutputWriter jsonWriter, PipelineConfig pipelineConfig) { jsonWriter.addLinks(linksWriter -> linksWriter .addLink("self", Routes.PipelineConfig.name(pipelineConfig.getName().toString())) .addAbsoluteLink("doc", Routes.PipelineConfig.DOC) .addLink("find", Routes.PipelineConfig.find())); // This is needed for the case when there are no materials defined. Refer to pipeline_config_representer.rb#152 pipelineConfig.errors().addAll(pipelineConfig.materialConfigs().errors()); if (!pipelineConfig.errors().isEmpty()) { jsonWriter.addChild("errors", errorWriter -> { HashMap<String, String> errorMapping = new HashMap<>(); errorMapping.put("labelTemplate", "label_template"); errorMapping.put("params", "parameters"); errorMapping.put("variables", "environment_variables"); errorMapping.put("trackingTool", "tracking_tool"); new ErrorGetter(errorMapping).toJSON(errorWriter, pipelineConfig); }); } jsonWriter.add("label_template", pipelineConfig.getLabelTemplate()); jsonWriter.add("lock_behavior", pipelineConfig.getLockBehavior()); jsonWriter.add("name", pipelineConfig.name()); jsonWriter.add("template", pipelineConfig.getTemplateName()); writeOrigin(jsonWriter, pipelineConfig.getOrigin()); jsonWriter.addChildList("parameters", paramsWriter -> ParamRepresenter.toJSONArray(paramsWriter, pipelineConfig.getParams())); jsonWriter.addChildList("environment_variables", envVarsWriter -> EnvironmentVariableRepresenter.toJSON(envVarsWriter, pipelineConfig.getVariables())); jsonWriter.addChildList("materials", materialsWriter -> MaterialRepresenter.toJSONArray(materialsWriter, pipelineConfig.materialConfigs())); writeStages(jsonWriter, pipelineConfig); writeTrackingTool(jsonWriter, pipelineConfig); writeTimer(jsonWriter, pipelineConfig.getTimer()); }
CRPipeline pipelineConfigToCRPipeline(PipelineConfig pipelineConfig, String groupName) { CRPipeline crPipeline = new CRPipeline(); crPipeline.setGroupName(groupName); crPipeline.setName(pipelineConfig.name().toString()); for(StageConfig stage: pipelineConfig.getStages()) { crPipeline.addStage(stageToCRStage(stage)); } for (ParamConfig param: pipelineConfig.getParams()) { crPipeline.addParameter(paramToCRParam(param)); } for (MaterialConfig material: pipelineConfig.materialConfigs()) { crPipeline.addMaterial(materialToCRMaterial(material)); } for (EnvironmentVariableConfig envVar: pipelineConfig.getVariables()) { crPipeline.addEnvironmentVariable(environmentVariableConfigToCREnvironmentVariable(envVar)); } if (pipelineConfig.getTemplateName() != null) crPipeline.setTemplate(pipelineConfig.getTemplateName().toString()); crPipeline.setTrackingTool(trackingToolToCRTrackingTool(pipelineConfig.getTrackingTool())); crPipeline.setTimer(timerConfigToCRTimer(pipelineConfig.getTimer())); crPipeline.setLock_behavior(pipelineConfig.getLockBehavior()); if (pipelineConfig.getMingleConfig().isDefined()) { crPipeline.setMingle(mingleToCRMingle(pipelineConfig.getMingleConfig())); } crPipeline.setLabelTemplate(pipelineConfig.getLabelTemplate()); return crPipeline; }