use of io.cdap.cdap.proto.BatchProgramSchedule 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.BatchProgramSchedule in project cdap by caskdata.
the class AppFabricTestBase method getScheduledRunTimes.
/**
* Returns a list of {@link BatchProgramSchedule}.
*
* @param namespace the namespace to query in
* @param programIds list of programs to query for scheuled run time
* @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
* @return a list of {@link BatchProgramSchedule}
*/
protected List<BatchProgramSchedule> getScheduledRunTimes(String namespace, Collection<? extends ProgramId> programIds, boolean next) throws Exception {
Assert.assertTrue(programIds.stream().map(ProgramId::getNamespace).allMatch(namespace::equals));
String url = String.format("%sruntime", next ? "next" : "previous");
String versionedUrl = getVersionedAPIPath(url, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
List<BatchProgram> batchPrograms = programIds.stream().map(id -> new BatchProgram(id.getApplication(), id.getType(), id.getProgram())).collect(Collectors.toList());
HttpResponse response = doPost(versionedUrl, GSON.toJson(batchPrograms));
assertResponseCode(200, response);
return readResponse(response, new TypeToken<List<BatchProgramSchedule>>() {
}.getType());
}
use of io.cdap.cdap.proto.BatchProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method batchRunTimes.
/**
* Fetches scheduled run times for a set of programs.
*
* @param namespace namespace of the programs
* @param programs the list of programs to fetch scheduled run times
* @param previous {@code true} to get the previous scheduled times; {@code false} to get the next scheduled times
* @return a list of {@link BatchProgramSchedule} containing the result
* @throws SchedulerException if failed to fetch schedules
*/
private List<BatchProgramSchedule> batchRunTimes(String namespace, Collection<? extends BatchProgram> programs, boolean previous) throws Exception {
List<ProgramId> programIds = programs.stream().map(p -> new ProgramId(namespace, p.getAppId(), p.getProgramType(), p.getProgramId())).collect(Collectors.toList());
Set<ApplicationId> appIds = programIds.stream().map(ProgramId::getParent).collect(Collectors.toSet());
Map<ApplicationId, ApplicationSpecification> appSpecs = store.getApplications(appIds);
List<BatchProgramSchedule> result = new ArrayList<>();
for (ProgramId programId : programIds) {
ApplicationSpecification spec = appSpecs.get(programId.getParent());
if (spec == null) {
result.add(new BatchProgramSchedule(programId, HttpResponseStatus.NOT_FOUND.code(), new NotFoundException(programId.getParent()).getMessage(), null));
continue;
}
try {
Store.ensureProgramExists(programId, spec);
result.add(new BatchProgramSchedule(programId, HttpResponseStatus.OK.code(), null, getScheduledRunTimes(programId, previous)));
} catch (NotFoundException e) {
result.add(new BatchProgramSchedule(programId, HttpResponseStatus.NOT_FOUND.code(), e.getMessage(), null));
} catch (BadRequestException e) {
result.add(new BatchProgramSchedule(programId, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage(), null));
}
}
return result;
}
Aggregations