use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class ScheduledRunTimeTest method testGetNextRun.
@Test
public void testGetNextRun() throws Exception {
ApplicationId appId = NamespaceId.DEFAULT.app("test");
deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
String scheduleName = "schedule1";
// Add a schedule. Use a constraint to make it not going to be executed
ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
long now = System.currentTimeMillis();
HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
Assert.assertEquals(200, response.getResponseCode());
// Get the next run time
List<ScheduledRuntime> scheduledRunTimes = getScheduledRunTimes(programId, true);
Assert.assertEquals(1, scheduledRunTimes.size());
long nextTime = scheduledRunTimes.get(0).getTime();
Assert.assertTrue(nextTime >= now);
}
use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class ScheduledRunTimeTest method testBatchGetNextRun.
@Test
public void testBatchGetNextRun() throws Exception {
// deploys 5 apps and create schedules for each of them
long now = System.currentTimeMillis();
List<ProgramId> programIds = new ArrayList<>();
// Use a constraint to make it not going to be executed
Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
for (int i = 0; i < 5; i++) {
ApplicationId appId = NamespaceId.DEFAULT.app("test" + i);
deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
String scheduleName = "schedule" + i;
// Add a schedule
ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
programIds.add(programId);
ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
Assert.assertEquals(200, response.getResponseCode());
}
// Add programs that the app or the program doesn't exist
programIds.add(NamespaceId.DEFAULT.app("not-exist").workflow("not-exist"));
programIds.add(NamespaceId.DEFAULT.app("test1").workflow("not-exist"));
List<BatchProgramSchedule> schedules = getScheduledRunTimes(NamespaceId.DEFAULT.getNamespace(), programIds, true);
Assert.assertEquals(programIds.size(), schedules.size());
// For the first 5 programs, they should have a next run
for (int i = 0; i < 5; i++) {
BatchProgramSchedule schedule = schedules.get(i);
Assert.assertEquals(200, schedule.getStatusCode());
List<ScheduledRuntime> nextRuns = schedule.getSchedules();
Assert.assertNotNull(nextRuns);
Assert.assertEquals(1, nextRuns.size());
long nextTime = nextRuns.get(0).getTime();
Assert.assertTrue(nextTime >= now);
}
// The last two should be a not found
Assert.assertEquals(404, schedules.get(5).getStatusCode());
Assert.assertEquals(404, schedules.get(6).getStatusCode());
}
use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class TimeSchedulerTest method testNextSchedule.
@Test
public void testNextSchedule() throws Exception {
// a schedule to be triggered every 5 minutes
ProgramSchedule sched = new ProgramSchedule("tsched11", "two times schedule", PROG1_ID, ImmutableMap.of("prop2", "xx"), new TimeTrigger("*/5 * * * *"), Collections.emptyList());
timeScheduler.addProgramSchedule(sched);
// schedule is by default SUSPENDED after being added, resume it to enable the schedule
timeScheduler.resumeProgramSchedule(sched);
long currentTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
long startTimeInSeconds = currentTimeInSeconds + TimeUnit.HOURS.toSeconds(1);
long endTimeInSeconds = currentTimeInSeconds + TimeUnit.HOURS.toSeconds(3);
List<ScheduledRuntime> nextRuntimes = timeScheduler.getAllScheduledRunTimes(PROG1_ID, SchedulableProgramType.WORKFLOW, startTimeInSeconds, endTimeInSeconds);
// for a scan range of 1pm to 3pm. since start time is inclusive, from 1pm tp 2pm we will have 13 schedules
// and from 2:05 pm to 2:55pm will have 11 schedules as end time is exclusive. in total we expect 24 schedules.
Assert.assertEquals(24, nextRuntimes.size());
}
use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class AppFabricTestBase method getScheduledRunTimes.
/**
* Returns a list of {@link ScheduledRuntime}.
*
* @param programId the program id
* @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
*/
protected List<ScheduledRuntime> getScheduledRunTimes(ProgramId programId, boolean next) throws Exception {
String nextRunTimeUrl = String.format("apps/%s/workflows/%s/%sruntime", programId.getApplication(), programId.getProgram(), next ? "next" : "previous");
String versionedUrl = getVersionedAPIPath(nextRunTimeUrl, Constants.Gateway.API_VERSION_3_TOKEN, programId.getNamespace());
HttpResponse response = doGet(versionedUrl);
assertResponseCode(200, response);
return readResponse(response, new TypeToken<List<ScheduledRuntime>>() {
}.getType());
}
use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class ScheduleClientTestRun method testAll.
@Test
public void testAll() throws Exception {
List<ScheduleDetail> list = scheduleClient.listSchedules(workflow);
Assert.assertEquals(1, list.size());
ScheduleDetail timeSchedule = list.get(0);
ProtoTrigger.TimeTrigger timeTrigger = (ProtoTrigger.TimeTrigger) timeSchedule.getTrigger();
Assert.assertEquals(FakeApp.TIME_SCHEDULE_NAME, timeSchedule.getName());
Assert.assertEquals(FakeApp.SCHEDULE_CRON, timeTrigger.getCronExpression());
String status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SCHEDULED", status);
long startTime = System.currentTimeMillis();
scheduleClient.suspend(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
long endTime = System.currentTimeMillis() + 1;
scheduleClient.reEnableSuspendedSchedules(NamespaceId.DEFAULT, startTime, endTime);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SCHEDULED", status);
scheduleClient.suspend(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SCHEDULED", status);
scheduleClient.suspend(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
List<ScheduledRuntime> scheduledRuntimes = scheduleClient.nextRuntimes(workflow);
scheduleClient.suspend(schedule);
Assert.assertEquals(1, scheduledRuntimes.size());
// simply assert that its scheduled for some time in the future (or scheduled for now, but hasn't quite
// executed yet
Assert.assertTrue(scheduledRuntimes.get(0).getTime() >= System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1));
try {
scheduleClient.nextRuntimes(app.workflow("nonexistentWorkflow"));
Assert.fail("Expected not to be able to retrieve next run times for a nonexistent workflow.");
} catch (NotFoundException expected) {
// expected
}
}
Aggregations