use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doAddSchedule.
private void doAddSchedule(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String scheduleName) throws Exception {
final ApplicationId applicationId = new ApplicationId(namespace, appName, appVersion);
ScheduleDetail scheduleFromRequest = readScheduleDetailBody(request, scheduleName, false, new Function<JsonElement, ScheduleDetail>() {
@Override
public ScheduleDetail apply(@Nullable JsonElement input) {
ScheduleSpecification scheduleSpec = GSON.fromJson(input, ScheduleSpecification.class);
return toScheduleDetail(applicationId, scheduleSpec);
}
});
if (scheduleFromRequest.getProgram() == null) {
throw new BadRequestException("No program was specified for the schedule");
}
if (scheduleFromRequest.getProgram().getProgramType() == null) {
throw new BadRequestException("No program type was specified for the schedule");
}
if (scheduleFromRequest.getProgram().getProgramName() == null) {
throw new BadRequestException("No program name was specified for the schedule");
}
if (scheduleFromRequest.getTrigger() == null) {
throw new BadRequestException("No trigger was specified for the schedule");
}
ProgramType programType = ProgramType.valueOfSchedulableType(scheduleFromRequest.getProgram().getProgramType());
String programName = scheduleFromRequest.getProgram().getProgramName();
ProgramId programId = applicationId.program(programType, programName);
if (lifecycleService.getProgramSpecification(programId) == null) {
throw new NotFoundException(programId);
}
String description = Objects.firstNonNull(scheduleFromRequest.getDescription(), "");
Map<String, String> properties = Objects.firstNonNull(scheduleFromRequest.getProperties(), EMPTY_PROPERTIES);
List<? extends Constraint> constraints = Objects.firstNonNull(scheduleFromRequest.getConstraints(), NO_CONSTRAINTS);
long timeoutMillis = Objects.firstNonNull(scheduleFromRequest.getTimeoutMillis(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
ProgramSchedule schedule = new ProgramSchedule(scheduleName, description, programId, properties, scheduleFromRequest.getTrigger(), constraints, timeoutMillis);
programScheduler.addSchedule(schedule);
responder.sendStatus(HttpResponseStatus.OK);
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method combineForUpdate.
private ProgramSchedule combineForUpdate(ScheduleDetail scheduleDetail, ProgramSchedule existing) throws BadRequestException {
String description = Objects.firstNonNull(scheduleDetail.getDescription(), existing.getDescription());
ProgramId programId = scheduleDetail.getProgram() == null ? existing.getProgramId() : existing.getProgramId().getParent().program(scheduleDetail.getProgram().getProgramType() == null ? existing.getProgramId().getType() : ProgramType.valueOfSchedulableType(scheduleDetail.getProgram().getProgramType()), Objects.firstNonNull(scheduleDetail.getProgram().getProgramName(), existing.getProgramId().getProgram()));
if (!programId.equals(existing.getProgramId())) {
throw new BadRequestException(String.format("Must update the schedule '%s' with the same program as '%s'. " + "To change the program in a schedule, please delete the schedule and create a new one.", existing.getName(), existing.getProgramId().toString()));
}
Map<String, String> properties = Objects.firstNonNull(scheduleDetail.getProperties(), existing.getProperties());
Trigger trigger = Objects.firstNonNull(scheduleDetail.getTrigger(), existing.getTrigger());
List<? extends Constraint> constraints = Objects.firstNonNull(scheduleDetail.getConstraints(), existing.getConstraints());
Long timeoutMillis = Objects.firstNonNull(scheduleDetail.getTimeoutMillis(), existing.getTimeoutMillis());
return new ProgramSchedule(existing.getName(), description, programId, properties, trigger, constraints, timeoutMillis);
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method doGetSchedules.
protected void doGetSchedules(HttpResponder responder, String namespace, String app, String version, @Nullable String workflow, @Nullable String format) throws NotFoundException, BadRequestException {
boolean asScheduleSpec = returnScheduleAsSpec(format);
ApplicationId applicationId = new ApplicationId(namespace, app, version);
ApplicationSpecification appSpec = store.getApplication(applicationId);
if (appSpec == null) {
throw new NotFoundException(applicationId);
}
List<ProgramSchedule> schedules;
if (workflow != null) {
WorkflowId workflowId = applicationId.workflow(workflow);
if (appSpec.getWorkflows().get(workflow) == null) {
throw new NotFoundException(workflowId);
}
schedules = programScheduler.listSchedules(workflowId);
} else {
schedules = programScheduler.listSchedules(applicationId);
}
List<ScheduleDetail> details = Schedulers.toScheduleDetails(schedules);
if (asScheduleSpec) {
List<ScheduleSpecification> specs = ScheduleDetail.toScheduleSpecs(details);
responder.sendJson(HttpResponseStatus.OK, specs, Schedulers.SCHEDULE_SPECS_TYPE, GSON_FOR_SCHEDULES);
} else {
responder.sendJson(HttpResponseStatus.OK, details, Schedulers.SCHEDULE_DETAILS_TYPE, GSON_FOR_SCHEDULES);
}
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class DeleteAndCreateSchedulesStage method process.
@Override
public void process(final ApplicationWithPrograms input) throws Exception {
if (!input.canUpdateSchedules()) {
// if we cant update schedules, emit and return
emit(input);
return;
}
ApplicationId appId = input.getApplicationId();
// Get a set of new schedules from the app spec
Set<ProgramSchedule> newSchedules = getProgramScheduleSet(appId, input.getSpecification());
for (ProgramSchedule schedule : programScheduler.listSchedules(appId)) {
if (newSchedules.contains(schedule)) {
// Remove the existing schedule from the newSchedules
newSchedules.remove(schedule);
continue;
}
// Delete the existing schedule if it is not present in newSchedules
programScheduler.deleteSchedule(schedule.getScheduleId());
}
// Add new schedules
for (ProgramSchedule schedule : newSchedules) {
addSchedule(schedule);
}
// Emit the input to next stage.
emit(input);
}
use of co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule in project cdap by caskdata.
the class DeleteAndCreateSchedulesStage method toProgramSchedule.
private ProgramSchedule toProgramSchedule(ApplicationId appId, ScheduleCreationSpec scheduleCreationSpec) {
ProgramId programId = appId.workflow(scheduleCreationSpec.getProgramName());
Trigger trigger = scheduleCreationSpec.getTrigger();
return new ProgramSchedule(scheduleCreationSpec.getName(), scheduleCreationSpec.getDescription(), programId, scheduleCreationSpec.getProperties(), trigger, scheduleCreationSpec.getConstraints(), scheduleCreationSpec.getTimeoutMillis());
}
Aggregations