use of org.quartz.TriggerKey in project cdap by caskdata.
the class DatasetBasedTimeScheduleStore method upgradeTriggers.
private void upgradeTriggers(Table table) {
Row result = table.get(TRIGGER_KEY);
if (result.isEmpty()) {
return;
}
for (byte[] column : result.getColumns().values()) {
TriggerStatusV2 triggerStatus = (TriggerStatusV2) SerializationUtils.deserialize(column);
TriggerKey oldTriggerKey = triggerStatus.trigger.getKey();
boolean modified = addDefaultAppVersionIfNeeded(triggerStatus);
if (modified) {
// Write the new triggerStatus only if the upgraded key doesn't exist already
if (readTrigger(table, triggerStatus.trigger.getKey()) == null) {
persistTrigger(table, triggerStatus.trigger, triggerStatus.state);
}
removeTrigger(table, oldTriggerKey);
}
}
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class DatasetBasedTimeScheduleStore method addDefaultAppVersionIfNeeded.
private boolean addDefaultAppVersionIfNeeded(TriggerStatusV2 oldTrigger) {
TriggerKey oldTriggerKey = oldTrigger.trigger.getKey();
JobKey oldTriggerJobKey = oldTrigger.trigger.getJobKey();
String[] triggerSplits = oldTriggerKey.getName().split(":");
String[] oldJobNameSplits = oldTriggerJobKey.getName().split(":");
// If TriggerKey and JobKey have the new format, then return
if (triggerSplits.length == 6 && oldJobNameSplits.length == 5) {
return false;
}
if (triggerSplits.length == 5) {
// This trigger key has the old format. So replace it with new format.
// New TriggerKey name has format = namespace:application:version:type:program:schedule
String newTriggerName = ScheduleUpgradeUtil.getNameWithDefaultVersion(triggerSplits, 2);
oldTrigger.trigger.setKey(new TriggerKey(newTriggerName, oldTriggerKey.getGroup()));
}
if (oldJobNameSplits.length == 4) {
// This job key has the old format. So replace it with new format.
// New JobKey name has format = namespace:application:version:type:program
String newJobName = ScheduleUpgradeUtil.getNameWithDefaultVersion(oldJobNameSplits, 2);
oldTrigger.trigger.setJobKey(new JobKey(newJobName, oldTriggerJobKey.getGroup()));
} else {
LOG.error("TriggerKey {} has application version in it but JobKey {} didn't.", oldTrigger.trigger.getKey(), oldTrigger.trigger.getJobKey());
}
return true;
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class TimeScheduler method addProgramSchedule.
public void addProgramSchedule(ProgramSchedule schedule) throws AlreadyExistsException, SchedulerException {
// Verify every trigger does not exist first before adding any of them to Quartz scheduler
try {
Map<String, TriggerKey> cronTriggerKeyMap = getCronTriggerKeyMap(schedule);
for (TriggerKey triggerKey : cronTriggerKeyMap.values()) {
assertTriggerDoesNotExist(triggerKey);
}
ProgramId program = schedule.getProgramId();
SchedulableProgramType programType = program.getType().getSchedulableType();
JobDetail job = addJob(program, programType);
for (Map.Entry<String, TriggerKey> entry : cronTriggerKeyMap.entrySet()) {
scheduleJob(entry.getValue(), schedule.getName(), entry.getKey(), job);
}
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class TimeScheduler method deleteProgramSchedule.
public void deleteProgramSchedule(ProgramSchedule schedule) throws NotFoundException, SchedulerException {
try {
Collection<TriggerKey> triggerKeys = getGroupedTriggerKeys(schedule);
// Must assert all trigger keys exist before processing each trigger key
assertTriggerKeysExist(triggerKeys);
for (TriggerKey triggerKey : triggerKeys) {
Trigger trigger = getTrigger(triggerKey, schedule.getProgramId(), schedule.getName());
scheduler.unscheduleJob(trigger.getKey());
JobKey jobKey = trigger.getJobKey();
if (scheduler.getTriggersOfJob(jobKey).isEmpty()) {
scheduler.deleteJob(jobKey);
}
}
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
}
use of org.quartz.TriggerKey in project cdap by caskdata.
the class TimeScheduler method getCronTriggerKeyMap.
/**
* @return A Map with cron expression as keys and corresponding trigger key as values.
* Trigger keys are created from program name, programType and scheduleName (and cron expression if the trigger
* in the schedule is a composite trigger) and TimeScheuler#PAUSED_NEW_TRIGGERS_GROUP
* if it exists in this group else returns the {@link TriggerKey} prepared with null which gets it with
* {@link Key#DEFAULT_GROUP}
* @throws org.quartz.SchedulerException
*/
private Map<String, TriggerKey> getCronTriggerKeyMap(ProgramSchedule schedule) throws org.quartz.SchedulerException {
ProgramId program = schedule.getProgramId();
SchedulableProgramType programType = program.getType().getSchedulableType();
co.cask.cdap.api.schedule.Trigger trigger = schedule.getTrigger();
Map<String, TriggerKey> cronTriggerKeyMap = new HashMap<>();
// Get a set of TimeTrigger if the schedule's trigger is a composite trigger
if (trigger instanceof AbstractSatisfiableCompositeTrigger) {
Set<SatisfiableTrigger> triggerSet = ((AbstractSatisfiableCompositeTrigger) trigger).getUnitTriggers().get(ProtoTrigger.Type.TIME);
if (triggerSet == null) {
return ImmutableMap.of();
}
for (SatisfiableTrigger timeTrigger : triggerSet) {
String cron = ((TimeTrigger) timeTrigger).getCronExpression();
String triggerName = AbstractTimeSchedulerService.getTriggerName(program, programType, schedule.getName(), cron);
cronTriggerKeyMap.put(cron, triggerKeyForName(triggerName));
}
return cronTriggerKeyMap;
}
// No need to include cron expression in trigger key if the trigger is not composite trigger
String triggerName = AbstractTimeSchedulerService.scheduleIdFor(program, programType, schedule.getName());
cronTriggerKeyMap.put(((TimeTrigger) schedule.getTrigger()).getCronExpression(), triggerKeyForName(triggerName));
return cronTriggerKeyMap;
}
Aggregations