use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getProgramStatusSchedules.
/**
* Get schedules containing {@link ProgramStatusTrigger} filtered by triggering program, and optionally by
* triggering program statuses or schedule status
* @param triggerNamespaceId namespace of the triggering program in {@link ProgramStatusTrigger}
* @param triggerAppName application name of the triggering program in {@link ProgramStatusTrigger}
* @param triggerAppVersion application version of the triggering program in {@link ProgramStatusTrigger}
* @param triggerProgramType program type of the triggering program in {@link ProgramStatusTrigger}
* @param triggerProgramName program name of the triggering program in {@link ProgramStatusTrigger}
* @param triggerProgramStatuses comma separated {@link ProgramStatus} in {@link ProgramStatusTrigger}.
* Schedules with {@link ProgramStatusTrigger} triggered by none of the
* {@link ProgramStatus} in triggerProgramStatuses will be filtered out.
* If not specified, schedules will be returned regardless of triggering program status.
* @param scheduleStatus status of the schedule. Can only be one of "SCHEDULED" or "SUSPENDED".
* If specified, only schedules with matching status will be returned.
*/
@GET
@Path("schedules/trigger-type/program-status")
public void getProgramStatusSchedules(HttpRequest request, HttpResponder responder, @QueryParam("trigger-namespace-id") String triggerNamespaceId, @QueryParam("trigger-app-name") String triggerAppName, @QueryParam("trigger-app-version") @DefaultValue(ApplicationId.DEFAULT_VERSION) String triggerAppVersion, @QueryParam("trigger-program-type") String triggerProgramType, @QueryParam("trigger-program-name") String triggerProgramName, @QueryParam("trigger-program-statuses") String triggerProgramStatuses, @QueryParam("schedule-status") String scheduleStatus) throws Exception {
if (triggerNamespaceId == null) {
throw new BadRequestException("Must specify trigger-namespace-id as a query param");
}
if (triggerAppName == null) {
throw new BadRequestException("Must specify trigger-app-name as a query param");
}
if (triggerProgramType == null) {
throw new BadRequestException("Must specify trigger-program-type as a query param");
}
if (triggerProgramName == null) {
throw new BadRequestException("Must specify trigger-program-name as a query param");
}
ProgramType programType = getProgramType(triggerProgramType);
ProgramScheduleStatus programScheduleStatus;
try {
programScheduleStatus = scheduleStatus == null ? null : ProgramScheduleStatus.valueOf(scheduleStatus);
} catch (IllegalArgumentException e) {
throw new BadRequestException(String.format("Invalid schedule status '%s'. Must be one of %s.", scheduleStatus, Joiner.on(',').join(ProgramScheduleStatus.values())), e);
}
ProgramId triggerProgramId = new NamespaceId(triggerNamespaceId).app(triggerAppName, triggerAppVersion).program(programType, triggerProgramName);
Set<io.cdap.cdap.api.ProgramStatus> queryProgramStatuses = new HashSet<>();
if (triggerProgramStatuses != null) {
try {
for (String status : triggerProgramStatuses.split(",")) {
queryProgramStatuses.add(io.cdap.cdap.api.ProgramStatus.valueOf(status));
}
} catch (Exception e) {
throw new BadRequestException(String.format("Unable to parse program statuses '%s'. Must be comma separated " + "valid ProgramStatus names such as COMPLETED, FAILED, KILLED.", triggerProgramStatuses), e);
}
} else {
// Query for schedules with all the statuses if no query status is specified
Collections.addAll(queryProgramStatuses, io.cdap.cdap.api.ProgramStatus.values());
}
List<ScheduleDetail> details = programScheduleService.findTriggeredBy(triggerProgramId, queryProgramStatuses).stream().filter(record -> programScheduleStatus == null || record.getMeta().getStatus().equals(programScheduleStatus)).map(ProgramScheduleRecord::toScheduleDetail).collect(Collectors.toList());
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(details, Schedulers.SCHEDULE_DETAILS_TYPE));
}
use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus in project cdap by caskdata.
the class CoreSchedulerService method updateSchedule.
@Override
public void updateSchedule(ProgramSchedule schedule) throws NotFoundException, BadRequestException, ProfileConflictException {
checkStarted();
ProgramScheduleStatus previousStatus = getScheduleStatus(schedule.getScheduleId());
deleteSchedule(schedule.getScheduleId());
try {
addSchedule(schedule);
} catch (AlreadyExistsException e) {
// Should never reach here because we just deleted it
throw new IllegalStateException("Schedule '" + schedule.getScheduleId() + "' already exists despite just being deleted.");
}
// if the schedule was previously enabled, it should still/again enabled be after the update
if (ProgramScheduleStatus.SCHEDULED == previousStatus) {
try {
enableSchedule(schedule.getScheduleId());
} catch (ConflictException e) {
// Should never reach here because we just added this
throw new IllegalStateException("Schedule '" + schedule.getScheduleId() + "' already enabled despite just being added.");
}
}
}
use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus in project cdap by caskdata.
the class ProgramScheduleStoreDataset method extractMetaFromRow.
/**
* Reads the meta data from a row in the schedule store.
*
* @throws IllegalStateException if one of the expected fields is missing or ill-formed.
*/
private ProgramScheduleMeta extractMetaFromRow(ScheduleId scheduleId, StructuredRow row) {
Long updatedTime = row.getLong(StoreDefinition.ProgramScheduleStore.UPDATE_TIME);
String statusString = row.getString(StoreDefinition.ProgramScheduleStore.STATUS);
try {
Preconditions.checkArgument(updatedTime != null, "Last-updated timestamp is null");
Preconditions.checkArgument(statusString != null, "schedule status is null");
ProgramScheduleStatus status = ProgramScheduleStatus.valueOf(statusString);
return new ProgramScheduleMeta(status, updatedTime);
} catch (IllegalArgumentException e) {
throw new IllegalStateException(String.format("Unexpected stored meta data for schedule %s: %s", scheduleId, e.getMessage()));
}
}
use of io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doGetSchedules.
private void doGetSchedules(HttpResponder responder, ApplicationId applicationId, @Nullable ProgramId programId, @Nullable String triggerTypeStr, @Nullable String statusStr) throws Exception {
ApplicationSpecification appSpec = store.getApplication(applicationId);
if (appSpec == null) {
throw new NotFoundException(applicationId);
}
ProgramScheduleStatus status;
try {
status = statusStr == null ? null : ProgramScheduleStatus.valueOf(statusStr);
} catch (IllegalArgumentException e) {
throw new BadRequestException(String.format("Invalid schedule status '%s'. Must be one of %s.", statusStr, Joiner.on(',').join(ProgramScheduleStatus.values())), e);
}
ProtoTrigger.Type triggerType;
try {
triggerType = triggerTypeStr == null ? null : ProtoTrigger.Type.valueOfCategoryName(triggerTypeStr);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
Predicate<ProgramScheduleRecord> predicate = record -> true;
if (status != null) {
predicate = predicate.and(record -> record.getMeta().getStatus().equals(status));
}
if (triggerType != null) {
predicate = predicate.and(record -> record.getSchedule().getTrigger().getType().equals(triggerType));
}
Collection<ProgramScheduleRecord> schedules;
if (programId != null) {
if (!appSpec.getProgramsByType(programId.getType().getApiProgramType()).contains(programId.getProgram())) {
throw new NotFoundException(programId);
}
schedules = programScheduleService.list(programId, predicate);
} else {
schedules = programScheduleService.list(applicationId, predicate);
}
List<ScheduleDetail> details = schedules.stream().map(ProgramScheduleRecord::toScheduleDetail).collect(Collectors.toList());
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(details, Schedulers.SCHEDULE_DETAILS_TYPE));
}
Aggregations