Search in sources :

Example 76 with CronTrigger

use of org.quartz.CronTrigger in project candlepin by candlepin.

the class PinsetterKernel method addUniqueJob.

/**
 * Adds a unique job, replacing any old ones with different schedules.
 */
private void addUniqueJob(List<JobEntry> pendingJobs, String jobImpl, List<CronTrigger> existingCronTriggers, String schedule) throws SchedulerException {
    // If trigger already exists with same schedule, nothing to do
    if (existingCronTriggers.size() == 1 && existingCronTriggers.get(0).getCronExpression().equals(schedule)) {
        return;
    }
    /*
         * Otherwise, we know there are existing triggers, delete them all and create
         * one with our new schedule. Normally there should only ever be one, but past
         * bugs caused duplicates so we handle this situation by default now.
         *
         * This could be cleaning up some with the same schedule we want, but we can't
         * allow there to be multiple with the same schedule so simpler to just make sure
         * there's only one.
         */
    if (existingCronTriggers.size() > 0) {
        log.warn("Cleaning up " + existingCronTriggers.size() + " obsolete triggers.");
    }
    for (CronTrigger t : existingCronTriggers) {
        boolean result = scheduler.deleteJob(t.getJobKey());
        log.warn(t.getJobKey() + " deletion success?: " + result);
    }
    // Create our new job:
    pendingJobs.add(new JobEntry(jobImpl, schedule));
}
Also used : CronTrigger(org.quartz.CronTrigger)

Example 77 with CronTrigger

use of org.quartz.CronTrigger in project candlepin by candlepin.

the class PinsetterKernelTest method updateMultipleSchedules.

@Test
public void updateMultipleSchedules() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(ConfigProperties.DEFAULT_TASKS, JobCleaner.class.getName());
    props.put("org.quartz.jobStore.isClustered", "true");
    props.put("pinsetter.org.candlepin.pinsetter.tasks." + "JobCleaner.schedule", "*/1 * * * * ?");
    Configuration config = new MapConfiguration(props);
    JobDetail jobDetail = mock(JobDetail.class);
    // Hack multiple job schedules for same job:
    String crongrp = "cron group";
    Set<JobKey> jobs = new HashSet<>();
    JobKey key = jobKey("org.candlepin.pinsetter.tasks.JobCleaner");
    jobs.add(key);
    JobKey key2 = jobKey("org.candlepin.pinsetter.tasks.JobCleaner2");
    jobs.add(key2);
    CronTrigger cronTrigger = mock(CronTrigger.class);
    when(cronTrigger.getJobKey()).thenReturn(key);
    when(cronTrigger.getCronExpression()).thenReturn("*/7 * * * * ?");
    when(sched.getJobKeys(eq(jobGroupEquals(crongrp)))).thenReturn(jobs);
    when(sched.getTrigger(any(TriggerKey.class))).thenReturn(cronTrigger);
    when(sched.getJobDetail(any(JobKey.class))).thenReturn(jobDetail);
    doReturn(JobCleaner.class).when(jobDetail).getJobClass();
    pk = new PinsetterKernel(config, jfactory, jlistener, jcurator, sfactory, triggerListener, modeManager);
    pk.startup();
    verify(sched, times(2)).deleteJob(any(JobKey.class));
    verify(jcurator).create(any(JobStatus.class));
}
Also used : CronTrigger(org.quartz.CronTrigger) MapConfiguration(org.candlepin.common.config.MapConfiguration) Configuration(org.candlepin.common.config.Configuration) HashMap(java.util.HashMap) MapConfiguration(org.candlepin.common.config.MapConfiguration) TriggerKey(org.quartz.TriggerKey) JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) JobCleaner(org.candlepin.pinsetter.tasks.JobCleaner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 78 with CronTrigger

use of org.quartz.CronTrigger in project motech by motech.

the class SchedulableJobBuilder method buildCronSchedulableJob.

private static SchedulableJob buildCronSchedulableJob(Trigger trigger, JobDataMap dataMap) {
    CronTrigger cronTrigger = (CronTrigger) trigger;
    CronSchedulableJob job = new CronSchedulableJob();
    job.setEndDate(getEndDate(cronTrigger));
    job.setCronExpression(cronTrigger.getCronExpression());
    job.setIgnorePastFiresAtStart(dataMap.getBoolean(IGNORE_PAST_FIRES_AT_START));
    return job;
}
Also used : CronTrigger(org.quartz.CronTrigger) CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob)

Example 79 with CronTrigger

use of org.quartz.CronTrigger in project motech by motech.

the class MotechSchedulerServiceImpl method scheduleCronJob.

private void scheduleCronJob(CronSchedulableJob job, boolean isDayOfWeek, boolean update) {
    logObjectIfNotNull(job);
    validateCronSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    JobId jobId = new CronJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    Map<String, Object> metadata = new HashMap<>();
    metadata.put(IS_DAY_OF_WEEK, isDayOfWeek);
    metadata.put(UI_DEFINED, job.isUiDefined());
    metadata.put(IGNORE_PAST_FIRES_AT_START, job.isIgnorePastFiresAtStart());
    metadata.putAll(motechEvent.getMetadata());
    jobDetail.getJobDataMap().put(EVENT_METADATA, metadata);
    CronScheduleBuilder cronSchedule = cronSchedule(job.getCronExpression());
    // TODO: should take readable names rather than integers
    cronSchedule = setMisfirePolicyForCronTrigger(cronSchedule, schedulerSettings.getProperty("scheduler.cron.trigger.misfire.policy"));
    CronTrigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(cronSchedule).startAt(job.getStartDate() != null ? job.getStartDate().toDate() : now().toDate()).endAt(DateUtil.toDate(job.getEndDate())).build();
    Trigger existingTrigger;
    try {
        existingTrigger = scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(format("Schedule or reschedule the job: %s.\n%s", jobId, e.getMessage()), "scheduler.error.cantRescheduleJob", Arrays.asList(jobId.value(), e.getMessage()), e);
    }
    if (existingTrigger != null) {
        unscheduleJob(jobId.value());
    }
    DateTime now = now();
    if (job.isIgnorePastFiresAtStart() && (job.getStartDate() == null || job.getStartDate().isBefore(now))) {
        Date newStartTime = trigger.getFireTimeAfter(now.toDate());
        if (newStartTime == null) {
            newStartTime = now.toDate();
        }
        trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(cronSchedule).startAt(newStartTime).endAt(DateUtil.toDate(job.getEndDate())).build();
    }
    scheduleJob(jobDetail, trigger, update);
}
Also used : CronScheduleBuilder(org.quartz.CronScheduleBuilder) CronTrigger(org.quartz.CronTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) HashMap(java.util.HashMap) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Date(java.util.Date) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) CronJobId(org.motechproject.scheduler.contract.CronJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) MotechEvent(org.motechproject.event.MotechEvent) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) CronJobId(org.motechproject.scheduler.contract.CronJobId)

Example 80 with CronTrigger

use of org.quartz.CronTrigger in project motech by motech.

the class SchedulableJobBuilder method buildDayOfWeekSchedulableJob.

private static SchedulableJob buildDayOfWeekSchedulableJob(Trigger trigger, JobDataMap dataMap) {
    CronTrigger cronTrigger = (CronTrigger) trigger;
    DayOfWeekSchedulableJob job = new DayOfWeekSchedulableJob();
    job.setEndDate(getEndDate(trigger));
    job.setIgnorePastFiresAtStart(dataMap.getBoolean(IGNORE_PAST_FIRES_AT_START));
    CronExpressionUtil cronExpressionUtil = new CronExpressionUtil(cronTrigger.getCronExpression());
    job.setTime(cronExpressionUtil.getTime());
    job.setDays(cronExpressionUtil.getDaysOfWeek());
    return job;
}
Also used : CronExpressionUtil(org.motechproject.scheduler.util.CronExpressionUtil) CronTrigger(org.quartz.CronTrigger) DayOfWeekSchedulableJob(org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)

Aggregations

CronTrigger (org.quartz.CronTrigger)103 JobDetail (org.quartz.JobDetail)42 Trigger (org.quartz.Trigger)36 SimpleTrigger (org.quartz.SimpleTrigger)29 SchedulerException (org.quartz.SchedulerException)28 Test (org.junit.Test)24 Scheduler (org.quartz.Scheduler)19 TriggerKey (org.quartz.TriggerKey)19 Date (java.util.Date)14 JobKey (org.quartz.JobKey)12 JobDataMap (org.quartz.JobDataMap)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)8 CronScheduleBuilder (org.quartz.CronScheduleBuilder)8 ParseException (java.text.ParseException)7 HashMap (java.util.HashMap)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 JobExecutionException (org.quartz.JobExecutionException)6 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)5 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)5