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
}
}
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 cdapio.
the class TimeScheduler method getScheduledRuntime.
private List<ScheduledRuntime> getScheduledRuntime(ProgramId program, boolean previousRuntimeRequested) throws SchedulerException {
List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
SchedulableProgramType schedulableType = program.getType().getSchedulableType();
if (schedulableType == null) {
throw new IllegalArgumentException("Program " + program + " cannot be scheduled");
}
try {
for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, schedulableType))) {
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 {
// skip the trigger that is not enabled, since it cannot launch program as scheduled
if (scheduler.getTriggerState(trigger.getKey()) == Trigger.TriggerState.PAUSED) {
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 io.cdap.cdap.proto.ScheduledRuntime in project cdap by cdapio.
the class TimeScheduler method getAllScheduledRunTimes.
/**
* Get all the scheduled run time of the program within the given time range in the future.
* A program may contain multiple schedules. This method returns the scheduled runtimes for all the schedules
* within the given time range. This method only takes
* + into account schedules based on time. For schedules based on data, an empty list will
* + be returned.
*
* @param program program to fetch the next runtime.
* @param programType type of program.
* @param startTimeSecs the start of the time range in seconds (inclusive, i.e. scheduled time larger or
* equal to the start will be returned)
* @param endTimeSecs the end of the time range in seconds (exclusive, i.e. scheduled time smaller than the end
* will be returned)
* @return list of scheduled runtimes for the program. Empty list if there are no schedules
* or if the program is not found
* @throws SchedulerException on unforeseen error.
*/
public List<ScheduledRuntime> getAllScheduledRunTimes(ProgramId program, SchedulableProgramType programType, long startTimeSecs, long endTimeSecs) throws SchedulerException {
// decrease the start time by one second to include the next fire time that is equal to the start time
Date startTime = new Date(TimeUnit.SECONDS.toMillis(startTimeSecs - 1));
long endTimeMillis = TimeUnit.SECONDS.toMillis(endTimeSecs);
List<ScheduledRuntime> scheduledRuntimes = new ArrayList<>();
try {
for (Trigger trigger : scheduler.getTriggersOfJob(jobKeyFor(program, programType))) {
// skip the trigger that is not enabled, since it cannot launch program as scheduled
if (Trigger.TriggerState.PAUSED.equals(scheduler.getTriggerState(trigger.getKey()))) {
continue;
}
Date nextFireTime = trigger.getFireTimeAfter(startTime);
String triggerKeyString = trigger.getKey().toString();
while (nextFireTime != null && nextFireTime.getTime() < endTimeMillis) {
ScheduledRuntime runtime = new ScheduledRuntime(triggerKeyString, nextFireTime.getTime());
scheduledRuntimes.add(runtime);
nextFireTime = trigger.getFireTimeAfter(nextFireTime);
}
}
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(e);
}
return scheduledRuntimes;
}
use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.
the class OperationsDashboardHttpHandler method getScheduledDashboardRecords.
/**
* For a given time schedule, gets all the scheduled program run in the given time range
*
* @param schedule the schedule to get scheduled program run with
* @param startTimeSecs the start of the time range in seconds (inclusive, i.e. scheduled time larger or equal to the
* start will be returned)
* @param endTimeSecs the end of the time range in seconds (exclusive, i.e. scheduled time smaller than the end will
* be returned)
* @return a list of dashboard program run records with scheduled time as start time
* @throws Exception
*/
private List<DashboardProgramRunRecord> getScheduledDashboardRecords(ProgramSchedule schedule, long startTimeSecs, long endTimeSecs) throws Exception {
ProgramId programId = schedule.getProgramId();
// get all the scheduled run times within the given time range of the given program
List<ScheduledRuntime> scheduledRuntimes = timeSchedulerService.getAllScheduledRunTimes(programId, programId.getType().getSchedulableType(), startTimeSecs, endTimeSecs);
String userId = schedule.getProperties().get(ProgramOptionConstants.USER_ID);
String artifactId = schedule.getProperties().get(ProgramOptionConstants.ARTIFACT_ID);
ArtifactSummary artifactSummary = artifactId == null ? null : ArtifactSummary.from(GSON.fromJson(artifactId, ArtifactId.class));
// for each scheduled runtime, construct a dashboard record for it with the scheduled time as start time
return scheduledRuntimes.stream().map(scheduledRuntime -> new DashboardProgramRunRecord(programId.getNamespace(), artifactSummary, new DashboardProgramRunRecord.ApplicationNameVersion(programId.getApplication(), programId.getVersion()), programId.getType().name(), programId.getProgram(), null, userId, SCHEDULED, // convert the scheduled time from millis to seconds as start time
TimeUnit.MILLISECONDS.toSeconds(scheduledRuntime.getTime()), null, null, null, null, null)).collect(Collectors.toList());
}
Aggregations