use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.
the class AbstractCompositeTriggerBuilder method getBuiltTriggers.
protected SatisfiableTrigger[] getBuiltTriggers(String namespace, String applicationName, String applicationVersion) {
int numTriggers = triggers.length;
SatisfiableTrigger[] builtTriggers = new SatisfiableTrigger[numTriggers];
for (int i = 0; i < numTriggers; i++) {
Trigger trigger = triggers[i];
builtTriggers[i] = trigger instanceof TriggerBuilder ? ((TriggerBuilder) trigger).build(namespace, applicationName, applicationVersion) : (SatisfiableTrigger) trigger;
}
return builtTriggers;
}
use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.
the class AppWithFrequentScheduledWorkflows method configure.
@Override
public void configure() {
setName(NAME);
setDescription("Sample application with multiple Workflows");
addWorkflow(new DummyWorkflow(SOME_WORKFLOW));
addWorkflow(new DummyWorkflow(ANOTHER_WORKFLOW));
addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_1));
addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_2));
addWorkflow(new DummyWorkflow(COMPOSITE_WORKFLOW));
schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_1, ProgramType.WORKFLOW, SOME_WORKFLOW).triggerOnPartitions(DATASET_NAME1, 1));
schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_2, ProgramType.WORKFLOW, ANOTHER_WORKFLOW).triggerOnPartitions(DATASET_NAME2, 2));
// Schedule the workflow to run in every ten seconds
schedule(buildSchedule(TEN_SECOND_SCHEDULE_1, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1).triggerByTime("*/10 * * * * ?"));
// Schedule the workflow to run in every ten seconds
schedule(buildSchedule(TEN_SECOND_SCHEDULE_2, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_2).triggerByTime("*/10 * * * * ?"));
// OrTrigger with only PartitionTrigger to be triggered
Trigger orTrigger1 = getTriggerFactory().or(getTriggerFactory().onPartitions(DATASET_NAME2, 3), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
// OrTrigger with only TimeTrigger to be triggered
Trigger orTrigger2 = getTriggerFactory().or(getTriggerFactory().byTime("*/5 * * * * ?"), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
schedule(buildSchedule(COMPOSITE_SCHEDULE, ProgramType.WORKFLOW, COMPOSITE_WORKFLOW).triggerOn(getTriggerFactory().and(orTrigger1, orTrigger2)));
}
use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.
the class ProtoTriggerCodecTest method testTriggerCodec.
private void testTriggerCodec(ProtoTrigger trigger) {
String json = GSON.toJson(trigger);
Trigger trigger1 = GSON.fromJson(json, Trigger.class);
Assert.assertEquals(trigger, trigger1);
json = GSON.toJson(trigger, Trigger.class);
trigger1 = GSON.fromJson(json, Trigger.class);
Assert.assertEquals(trigger, trigger1);
}
use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.
the class ProgramScheduleStoreDataset method modifySchedulesTriggeredByDeletedProgram.
/**
* Update all schedules that can be triggered by the given deleted program. A schedule will be removed if
* the only {@link ProgramStatusTrigger} in it is triggered by the deleted program. Schedules with composite triggers
* will be updated if the composite trigger can still be satisfied after the program is deleted, otherwise the
* schedules will be deleted.
*
* @param programId the program id for which to delete the schedules
* @return the IDs of the schedules that were deleted
*/
public List<ProgramSchedule> modifySchedulesTriggeredByDeletedProgram(ProgramId programId) throws IOException {
long deleteTime = System.currentTimeMillis();
List<ProgramSchedule> deleted = new ArrayList<>();
Set<ProgramScheduleRecord> scheduleRecords = new HashSet<>();
for (ProgramStatus status : ProgramStatus.values()) {
scheduleRecords.addAll(findSchedules(Schedulers.triggerKeyForProgramStatus(programId, status)));
}
for (ProgramScheduleRecord scheduleRecord : scheduleRecords) {
ProgramSchedule schedule = scheduleRecord.getSchedule();
markScheduleAsDeleted(schedule.getScheduleId(), deleteTime);
triggerStore.deleteAll(Range.singleton(getScheduleKeys(schedule.getScheduleId())));
if (schedule.getTrigger() instanceof AbstractSatisfiableCompositeTrigger) {
// get the updated composite trigger by removing the program status trigger of the given program
Trigger updatedTrigger = ((AbstractSatisfiableCompositeTrigger) schedule.getTrigger()).getTriggerWithDeletedProgram(programId);
if (updatedTrigger == null) {
deleted.add(schedule);
continue;
}
// if the updated composite trigger is not null, add the schedule back with updated composite trigger
try {
addScheduleWithStatus(new ProgramSchedule(schedule.getName(), schedule.getDescription(), schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis());
} catch (AlreadyExistsException e) {
// this should never happen
LOG.warn("Failed to add the schedule '{}' triggered by '{}' with updated trigger '{}', " + "skip adding this schedule.", schedule.getScheduleId(), programId, updatedTrigger, e);
}
} else {
deleted.add(schedule);
}
}
return deleted;
}
use of io.cdap.cdap.api.schedule.Trigger in project cdap by caskdata.
the class TriggerInfoCodec method deserialize.
@Override
public TriggerInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
if (!(json instanceof JsonObject)) {
throw new JsonParseException("Expected a JsonObject but found a " + json.getClass().getName());
}
JsonObject object = (JsonObject) json;
JsonElement typeJson = object.get("type");
Trigger.Type triggerType = context.deserialize(typeJson, Trigger.Type.class);
Class<? extends TriggerInfo> subClass = typeClassMap.get(triggerType);
if (subClass == null) {
throw new JsonParseException("Unable to map trigger type " + triggerType + " to a TriggerInfo class");
}
return context.deserialize(json, subClass);
}
Aggregations