use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class ScheduleClient method nextRuntimes.
/**
* Get the next scheduled run time of the program. A program may contain multiple schedules.
* This method returns the next scheduled runtimes for all the schedules. This method only takes
* + into account schedules based on time. Schedules based on data are ignored.
*
* @param workflow Id of the Workflow for which to fetch next run times.
* @return list of Scheduled runtimes for the Workflow. Empty list if there are no schedules.
*/
public List<ScheduledRuntime> nextRuntimes(WorkflowId workflow) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
String path = String.format("apps/%s/workflows/%s/nextruntime", workflow.getApplication(), workflow.getProgram());
URL url = config.resolveNamespacedURLV3(workflow.getNamespaceId(), path);
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
throw new NotFoundException(workflow);
}
ObjectResponse<List<ScheduledRuntime>> objectResponse = ObjectResponse.fromJsonBody(response, new TypeToken<List<ScheduledRuntime>>() {
}.getType(), GSON);
return objectResponse.getResponseObject();
}
use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class TimeScheduler method getScheduledRuntime.
private List<ScheduledRuntime> getScheduledRuntime(ProgramId program, SchedulableProgramType programType, boolean previousRuntimeRequested) throws SchedulerException {
List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
try {
for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, programType))) {
long time;
if (previousRuntimeRequested) {
if (trigger.getPreviousFireTime() == null) {
// previous fire time can be null for the triggers which are not yet fired
continue;
}
time = trigger.getPreviousFireTime().getTime();
} else {
if (scheduler.getTriggerState(trigger.getKey()) == Trigger.TriggerState.PAUSED) {
// if the trigger is paused, then skip getting the next fire time
continue;
}
time = trigger.getNextFireTime().getTime();
}
ScheduledRuntime runtime = new ScheduledRuntime(trigger.getKey().toString(), time);
scheduledRuntimes.add(runtime);
}
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
return scheduledRuntimes;
}
use of co.cask.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);
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
}
}
use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class WorkflowHttpHandlerTest method getScheduledRunTime.
/**
* Returns a list of {@link ScheduledRuntime}.
*
* @param program the program id
* @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
*/
private List<ScheduledRuntime> getScheduledRunTime(Id.Program program, boolean next) throws Exception {
String nextRunTimeUrl = String.format("apps/%s/workflows/%s/%sruntime", program.getApplicationId(), program.getId(), next ? "next" : "previous");
String versionedUrl = getVersionedAPIPath(nextRunTimeUrl, Constants.Gateway.API_VERSION_3_TOKEN, program.getNamespaceId());
HttpResponse response = doGet(versionedUrl);
return readResponse(response, new TypeToken<List<ScheduledRuntime>>() {
}.getType());
}
use of co.cask.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class WorkflowHttpHandlerTest method testWorkflowSchedules.
@Ignore
@Test
public void testWorkflowSchedules() throws Exception {
// Steps for the test:
// 1. Deploy the app
// 2. Verify the schedules
// 3. Verify the history after waiting a while
// 4. Suspend the schedule
// 5. Verify there are no runs after the suspend by looking at the history
// 6. Resume the schedule
// 7. Verify there are runs after the resume by looking at the history
String appName = AppWithSchedule.NAME;
String workflowName = AppWithSchedule.WORKFLOW_NAME;
String sampleSchedule = AppWithSchedule.SCHEDULE;
// deploy app with schedule in namespace 2
HttpResponse response = deploy(AppWithSchedule.class, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Id.Program programId = Id.Program.from(TEST_NAMESPACE2, appName, ProgramType.WORKFLOW, workflowName);
Map<String, String> runtimeArguments = ImmutableMap.of("someKey", "someWorkflowValue", "workflowKey", "workflowValue");
setAndTestRuntimeArgs(programId, runtimeArguments);
// get schedules
List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE2, appName, workflowName);
Assert.assertEquals(1, schedules.size());
String scheduleName = schedules.get(0).getName();
Assert.assertFalse(scheduleName.isEmpty());
// TODO [CDAP-2327] Sagar Investigate why following check fails sometimes. Mostly test case issue.
// List<ScheduledRuntime> previousRuntimes = getScheduledRunTime(programId, scheduleName, "previousruntime");
// Assert.assertTrue(previousRuntimes.size() == 0);
long current = System.currentTimeMillis();
// sampleSchedule is initially suspended, so listing schedules with SCHEDULED status will get 0 schedule
schedules = getSchedules(TEST_NAMESPACE2, appName, ApplicationId.DEFAULT_VERSION, sampleSchedule, ProgramScheduleStatus.SCHEDULED);
Assert.assertEquals(0, schedules.size());
// Resume the schedule
Assert.assertEquals(200, resumeSchedule(TEST_NAMESPACE2, appName, sampleSchedule));
// Check schedule status
assertSchedule(programId, scheduleName, true, 30, TimeUnit.SECONDS);
// sampleSchedule is now resumed in SCHEDULED, so listing schedules with SCHEDULED status
// should return sampleSchedule
schedules = getSchedules(TEST_NAMESPACE2, appName, ApplicationId.DEFAULT_VERSION, sampleSchedule, ProgramScheduleStatus.SCHEDULED);
Assert.assertEquals(1, schedules.size());
Assert.assertEquals(TEST_NAMESPACE2, schedules.get(0).getNamespace());
Assert.assertEquals(appName, schedules.get(0).getApplication());
Assert.assertEquals(ApplicationId.DEFAULT_VERSION, schedules.get(0).getApplicationVersion());
Assert.assertEquals(sampleSchedule, schedules.get(0).getName());
List<ScheduledRuntime> runtimes = getScheduledRunTime(programId, true);
String id = runtimes.get(0).getId();
Assert.assertTrue(String.format("Expected schedule id '%s' to contain schedule name '%s'", id, scheduleName), id.contains(scheduleName));
Long nextRunTime = runtimes.get(0).getTime();
Assert.assertTrue(String.format("Expected nextRuntime '%s' to be greater than current runtime '%s'", nextRunTime, current), nextRunTime > current);
// Verify that at least one program is completed
verifyProgramRuns(programId, ProgramRunStatus.COMPLETED);
// Suspend the schedule
Assert.assertEquals(200, suspendSchedule(TEST_NAMESPACE2, appName, scheduleName));
// check paused state
assertSchedule(programId, scheduleName, false, 30, TimeUnit.SECONDS);
// check that there were at least 1 previous runs
List<ScheduledRuntime> previousRuntimes = getScheduledRunTime(programId, false);
int numRuns = previousRuntimes.size();
Assert.assertTrue(String.format("After sleeping for two seconds, the schedule should have at least triggered " + "once, but found %s previous runs", numRuns), numRuns >= 1);
// Verify no program running
verifyNoRunWithStatus(programId, ProgramRunStatus.RUNNING);
// get number of completed runs after schedule is suspended
int workflowRuns = getProgramRuns(programId, ProgramRunStatus.COMPLETED).size();
// verify that resuming the suspended schedule again has expected behavior (spawns new runs)
Assert.assertEquals(200, resumeSchedule(TEST_NAMESPACE2, appName, scheduleName));
// check scheduled state
assertSchedule(programId, scheduleName, true, 30, TimeUnit.SECONDS);
// Verify that the program ran after the schedule was resumed
verifyProgramRuns(programId, ProgramRunStatus.COMPLETED, workflowRuns);
// Suspend the schedule
Assert.assertEquals(200, suspendSchedule(TEST_NAMESPACE2, appName, scheduleName));
// check paused state
assertSchedule(programId, scheduleName, false, 30, TimeUnit.SECONDS);
// Check status of a non existing schedule
try {
assertSchedule(programId, "invalid", true, 2, TimeUnit.SECONDS);
Assert.fail();
} catch (Exception e) {
// expected
}
// Schedule operations using invalid namespace
try {
assertSchedule(Id.Program.from(TEST_NAMESPACE1, appName, ProgramType.WORKFLOW, workflowName), scheduleName, true, 2, TimeUnit.SECONDS);
Assert.fail();
} catch (Exception e) {
// expected
}
Assert.assertEquals(404, suspendSchedule(TEST_NAMESPACE1, appName, scheduleName));
Assert.assertEquals(404, resumeSchedule(TEST_NAMESPACE1, appName, scheduleName));
verifyNoRunWithStatus(programId, ProgramRunStatus.RUNNING);
deleteApp(Id.Application.from(TEST_NAMESPACE2, AppWithSchedule.class.getSimpleName()), 200);
}
Aggregations