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));
}
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));
}
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;
}
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);
}
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;
}
Aggregations