Search in sources :

Example 1 with TimerConfig

use of com.thoughtworks.go.config.TimerConfig in project gocd by gocd.

the class TimedBuild method canProduce.

public void canProduce(PipelineConfig pipelineConfig, SchedulingCheckerService schedulingChecker, ServerHealthService serverHealthService, OperationResult operationResult) {
    schedulingChecker.canTriggerPipelineWithTimer(pipelineConfig, operationResult);
    if (!operationResult.canContinue()) {
        ServerHealthState serverHealthState = operationResult.getServerHealthState();
        LOGGER.info(String.format("'%s'  because '%s'", serverHealthState.getMessage(), serverHealthState.getDescription()));
    } else {
        TimerConfig timer = pipelineConfig.getTimer();
        String timerSpec = timer == null ? "Missing timer spec" : timer.getTimerSpec();
        LOGGER.info(String.format("Timer scheduling pipeline '%s' using spec '%s'", pipelineConfig.name(), timerSpec));
    }
}
Also used : TimerConfig(com.thoughtworks.go.config.TimerConfig) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState)

Example 2 with TimerConfig

use of com.thoughtworks.go.config.TimerConfig in project gocd by gocd.

the class TimerScheduler method scheduleJob.

private void scheduleJob(Scheduler scheduler, PipelineConfig pipelineConfig) {
    TimerConfig timer = pipelineConfig.getTimer();
    if (timer != null) {
        try {
            Trigger trigger = new CronTrigger(CaseInsensitiveString.str(pipelineConfig.name()), QUARTZ_GROUP, timer.getTimerSpec());
            JobDetail jobDetail = new JobDetail(CaseInsensitiveString.str(pipelineConfig.name()), QUARTZ_GROUP, SchedulePipelineQuartzJob.class);
            jobDetail.setJobDataMap(jobDataMapFor(pipelineConfig));
            scheduler.scheduleJob(jobDetail, trigger);
            LOG.info("Initialized timer for pipeline " + pipelineConfig.name() + " with " + 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", "");
        }
    }
}
Also used : TimerConfig(com.thoughtworks.go.config.TimerConfig) ParseException(java.text.ParseException)

Example 3 with TimerConfig

use of com.thoughtworks.go.config.TimerConfig in project gocd by gocd.

the class TimerSchedulerTest method shouldRescheduleTimerTriggerPipelineWhenItsConfigChanges.

@Test
public void shouldRescheduleTimerTriggerPipelineWhenItsConfigChanges() throws SchedulerException {
    GoConfigService goConfigService = mock(GoConfigService.class);
    when(schedulerFactory.getScheduler()).thenReturn(scheduler);
    String pipelineName = "timer-based-pipeline";
    when(scheduler.getJobDetail(pipelineName, TimerScheduler.QUARTZ_GROUP)).thenReturn(mock(JobDetail.class));
    TimerScheduler timerScheduler = new TimerScheduler(schedulerFactory, goConfigService, null, null);
    ArgumentCaptor<ConfigChangedListener> captor = ArgumentCaptor.forClass(ConfigChangedListener.class);
    doNothing().when(goConfigService).register(captor.capture());
    timerScheduler.initialize();
    List<ConfigChangedListener> listeners = captor.getAllValues();
    assertThat(listeners.get(1) instanceof EntityConfigChangedListener, is(true));
    EntityConfigChangedListener<PipelineConfig> pipelineConfigChangeListener = (EntityConfigChangedListener<PipelineConfig>) listeners.get(1);
    PipelineConfig pipelineConfig = mock(PipelineConfig.class);
    when(pipelineConfig.name()).thenReturn(new CaseInsensitiveString(pipelineName));
    when(pipelineConfig.getTimer()).thenReturn(new TimerConfig("* * * * * ?", true));
    ArgumentCaptor<JobDetail> jobDetailArgumentCaptor = ArgumentCaptor.forClass(JobDetail.class);
    ArgumentCaptor<CronTrigger> triggerArgumentCaptor = ArgumentCaptor.forClass(CronTrigger.class);
    when(scheduler.scheduleJob(jobDetailArgumentCaptor.capture(), triggerArgumentCaptor.capture())).thenReturn(new Date());
    pipelineConfigChangeListener.onEntityConfigChange(pipelineConfig);
    assertThat(jobDetailArgumentCaptor.getValue().getName(), is(pipelineName));
    assertThat(triggerArgumentCaptor.getValue().getCronExpression(), is("* * * * * ?"));
    verify(schedulerFactory).getScheduler();
    verify(scheduler).start();
    verify(scheduler).getJobDetail(pipelineName, TimerScheduler.QUARTZ_GROUP);
    verify(scheduler).unscheduleJob(pipelineName, TimerScheduler.QUARTZ_GROUP);
    verify(scheduler).deleteJob(pipelineName, TimerScheduler.QUARTZ_GROUP);
    verify(scheduler).scheduleJob(jobDetailArgumentCaptor.getValue(), triggerArgumentCaptor.getValue());
}
Also used : PipelineConfig(com.thoughtworks.go.config.PipelineConfig) TimerConfig(com.thoughtworks.go.config.TimerConfig) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) EntityConfigChangedListener(com.thoughtworks.go.listener.EntityConfigChangedListener) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Date(java.util.Date) EntityConfigChangedListener(com.thoughtworks.go.listener.EntityConfigChangedListener) ConfigChangedListener(com.thoughtworks.go.listener.ConfigChangedListener) Test(org.junit.Test)

Aggregations

TimerConfig (com.thoughtworks.go.config.TimerConfig)3 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)1 PipelineConfig (com.thoughtworks.go.config.PipelineConfig)1 ConfigChangedListener (com.thoughtworks.go.listener.ConfigChangedListener)1 EntityConfigChangedListener (com.thoughtworks.go.listener.EntityConfigChangedListener)1 ServerHealthState (com.thoughtworks.go.serverhealth.ServerHealthState)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 Test (org.junit.Test)1