use of co.cask.cdap.internal.schedule.TimeSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method toScheduleDetail.
private ScheduleDetail toScheduleDetail(ApplicationId appId, ScheduleSpecification scheduleSpec) {
if (scheduleSpec.getSchedule() == null) {
throw new IllegalArgumentException("Schedule specification must contain schedule");
}
Trigger trigger;
if (scheduleSpec.getSchedule() instanceof TimeSchedule) {
trigger = new TimeTrigger(((TimeSchedule) scheduleSpec.getSchedule()).getCronEntry());
} else {
StreamSizeSchedule streamSchedule = (StreamSizeSchedule) scheduleSpec.getSchedule();
StreamId streamId = appId.getParent().stream(streamSchedule.getStreamName());
trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
}
List<Constraint> runConstraints = toConstraints(scheduleSpec.getSchedule().getRunConstraints());
return new ScheduleDetail(scheduleSpec.getSchedule().getName(), scheduleSpec.getSchedule().getDescription(), scheduleSpec.getProgram(), scheduleSpec.getProperties(), trigger, runConstraints, null);
}
use of co.cask.cdap.internal.schedule.TimeSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testSchedules.
@Test
public void testSchedules() throws Exception {
// deploy an app with schedule
Id.Artifact artifactId = Id.Artifact.from(TEST_NAMESPACE_META1.getNamespaceId().toId(), AppWithSchedule.NAME, VERSION1);
addAppArtifact(artifactId, AppWithSchedule.class);
AppRequest<? extends Config> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));
ApplicationId defaultAppId = TEST_NAMESPACE_META1.getNamespaceId().app(AppWithSchedule.NAME);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
// deploy another version of the app
ApplicationId appV2Id = TEST_NAMESPACE_META1.getNamespaceId().app(AppWithSchedule.NAME, VERSION2);
Assert.assertEquals(200, deploy(appV2Id, request).getStatusLine().getStatusCode());
// list schedules for default version app, for the workflow and for the app, they should be same
List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
Assert.assertEquals(1, schedules.size());
ScheduleDetail schedule = schedules.get(0);
Assert.assertEquals(SchedulableProgramType.WORKFLOW, schedule.getProgram().getProgramType());
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, schedule.getProgram().getProgramName());
Assert.assertEquals(new TimeTrigger("0/15 * * * * ?"), schedule.getTrigger());
// there should be two schedules now
List<ScheduleDetail> schedulesForApp = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
Assert.assertEquals(1, schedulesForApp.size());
Assert.assertEquals(schedules, schedulesForApp);
// validate backward compatible api
List<ScheduleSpecification> specsForApp = listScheduleSpecs(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
Assert.assertEquals(1, specsForApp.size());
ScheduleSpecification spec = specsForApp.get(0);
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, spec.getProgram().getProgramName());
Assert.assertTrue(spec.getSchedule() instanceof TimeSchedule);
Assert.assertEquals("0/15 * * * * ?", ((TimeSchedule) spec.getSchedule()).getCronEntry());
List<ScheduleDetail> schedules2 = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, AppWithSchedule.WORKFLOW_NAME);
Assert.assertEquals(1, schedules2.size());
ScheduleDetail schedule2 = schedules2.get(0);
Assert.assertEquals(SchedulableProgramType.WORKFLOW, schedule2.getProgram().getProgramType());
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, schedule2.getProgram().getProgramName());
Assert.assertEquals(new TimeTrigger("0/15 * * * * ?"), schedule2.getTrigger());
String newSchedule = "newTimeSchedule";
testAddSchedule(newSchedule);
testDeleteSchedule(appV2Id, newSchedule);
testUpdateSchedule(appV2Id);
}
use of co.cask.cdap.internal.schedule.TimeSchedule in project cdap by caskdata.
the class Schedulers method toScheduleCreationSpec.
public static ScheduleCreationSpec toScheduleCreationSpec(NamespaceId deployNamespace, Schedule schedule, String programName, Map<String, String> properties) {
Trigger trigger;
if (schedule instanceof TimeSchedule) {
trigger = new TimeTrigger(((TimeSchedule) schedule).getCronEntry());
} else {
StreamSizeSchedule streamSizeSchedule = ((StreamSizeSchedule) schedule);
trigger = new StreamSizeTrigger(deployNamespace.stream(streamSizeSchedule.getStreamName()), streamSizeSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ScheduleCreationSpec(schedule.getName(), schedule.getDescription(), programName, properties, trigger, constraints, Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
}
use of co.cask.cdap.internal.schedule.TimeSchedule in project cdap by caskdata.
the class Schedulers method toProgramSchedule.
public static ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleSpecification spec) {
Schedule schedule = spec.getSchedule();
ProgramType programType = ProgramType.valueOfSchedulableType(spec.getProgram().getProgramType());
ProgramId programId = appId.program(programType, spec.getProgram().getProgramName());
Trigger trigger;
if (schedule instanceof TimeSchedule) {
TimeSchedule timeSchedule = (TimeSchedule) schedule;
trigger = new TimeTrigger(timeSchedule.getCronEntry());
} else {
StreamSizeSchedule streamSchedule = (StreamSizeSchedule) schedule;
StreamId streamId = programId.getNamespaceId().stream(streamSchedule.getStreamName());
trigger = new StreamSizeTrigger(streamId, streamSchedule.getDataTriggerMB());
}
Integer maxConcurrentRuns = schedule.getRunConstraints().getMaxConcurrentRuns();
List<Constraint> constraints = maxConcurrentRuns == null ? ImmutableList.<Constraint>of() : ImmutableList.<Constraint>of(new ConcurrencyConstraint(maxConcurrentRuns));
return new ProgramSchedule(schedule.getName(), schedule.getDescription(), programId, spec.getProperties(), trigger, constraints);
}
use of co.cask.cdap.internal.schedule.TimeSchedule in project cdap by caskdata.
the class ScheduleDetail method toScheduleSpec.
/**
* Return an equivalent schedule specification, or null if there is no equivalent one.
*/
@Deprecated
@Nullable
public ScheduleSpecification toScheduleSpec() {
RunConstraints constraints = RunConstraints.NONE;
if (getConstraints() != null) {
for (Constraint runConstraint : getConstraints()) {
if (runConstraint instanceof ProtoConstraint.ConcurrencyConstraint) {
constraints = new RunConstraints(((ProtoConstraint.ConcurrencyConstraint) runConstraint).getMaxConcurrency());
break;
}
}
}
Schedule schedule;
if (getTrigger() instanceof ProtoTrigger.TimeTrigger) {
ProtoTrigger.TimeTrigger trigger = ((ProtoTrigger.TimeTrigger) getTrigger());
schedule = new TimeSchedule(getName(), getDescription(), trigger.getCronExpression(), constraints);
} else if (getTrigger() instanceof ProtoTrigger.StreamSizeTrigger) {
ProtoTrigger.StreamSizeTrigger trigger = (ProtoTrigger.StreamSizeTrigger) getTrigger();
schedule = new StreamSizeSchedule(getName(), getDescription(), trigger.getStreamId().getStream(), trigger.getTriggerMB(), constraints);
} else {
return null;
}
return new ScheduleSpecification(schedule, getProgram(), getProperties());
}
Aggregations