Search in sources :

Example 6 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testTimeSchedule.

@Test
public void testTimeSchedule() throws Exception {
    TimeSchedule timeSchedule = (TimeSchedule) Schedules.builder("foo").setDescription("bar").createTimeSchedule("cronEntry");
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, "testWorkflow");
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(timeSchedule, programInfo, properties);
    String jsonStr = GSON.toJson(specification);
    ScheduleSpecification deserialized = GSON.fromJson(jsonStr, ScheduleSpecification.class);
    Assert.assertEquals(specification, deserialized);
}
Also used : TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Test(org.junit.Test)

Example 7 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testStreamSizeSchedule.

@Test
public void testStreamSizeSchedule() throws Exception {
    Schedule dataSchedule = Schedules.builder("foo").setDescription("bar").createDataSchedule(Schedules.Source.STREAM, "stream", 10);
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, "testWorkflow");
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(dataSchedule, programInfo, properties);
    String jsonStr = GSON.toJson(specification);
    ScheduleSpecification deserialized = GSON.fromJson(jsonStr, ScheduleSpecification.class);
    Assert.assertEquals(specification, deserialized);
}
Also used : Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification) Test(org.junit.Test)

Example 8 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testAddSchedule.

private void testAddSchedule(String scheduleName) throws Exception {
    TimeSchedule timeSchedule = (TimeSchedule) Schedules.builder(scheduleName).setDescription("Something").createTimeSchedule("0 * * * ?");
    TimeTrigger timeTrigger = new TimeTrigger("0 * * * ?");
    ScheduleProgramInfo programInfo = new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, AppWithSchedule.WORKFLOW_NAME);
    ImmutableMap<String, String> properties = ImmutableMap.of("a", "b", "c", "d");
    ScheduleSpecification specification = new ScheduleSpecification(timeSchedule, programInfo, properties);
    ScheduleDetail detail = new ScheduleDetail(scheduleName, specification.getSchedule().getDescription(), specification.getProgram(), specification.getProperties(), new TimeTrigger(timeSchedule.getCronEntry()), Collections.<Constraint>emptyList(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
    // trying to add the schedule with different name in path param than schedule spec should fail
    HttpResponse response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "differentName", detail);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    // adding a schedule to a non-existing app should fail
    response = addSchedule(TEST_NAMESPACE1, "nonExistingApp", null, scheduleName, specification);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.getCode(), response.getStatusLine().getStatusCode());
    // adding a schedule to invalid type of program type should fail
    ScheduleDetail invalidScheduleDetail = new ScheduleDetail(scheduleName, "Something", new ScheduleProgramInfo(SchedulableProgramType.MAPREDUCE, AppWithSchedule.MAPREDUCE), properties, timeTrigger, ImmutableList.<Constraint>of(), TimeUnit.MINUTES.toMillis(1));
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, invalidScheduleDetail);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    // adding a schedule for a program that does not exist
    ScheduleSpecification nonExistingSpecification = new ScheduleSpecification(timeSchedule, new ScheduleProgramInfo(SchedulableProgramType.MAPREDUCE, "nope"), properties);
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, nonExistingSpecification);
    Assert.assertEquals(HttpResponseStatus.NOT_FOUND.getCode(), response.getStatusLine().getStatusCode());
    // adding a schedule with invalid schedule details should fail
    TimeSchedule invalidTimeSchedule = (TimeSchedule) Schedules.builder("invalidTimeSchedule").setDescription("Something").createTimeSchedule(// invalid cron expression
    "0 * ? * ?");
    // Intentionally keep this ScheduleSpecification to test backward compatibility
    ScheduleSpecification invalidSpecification = new ScheduleSpecification(invalidTimeSchedule, programInfo, properties);
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, "invalidTimeSchedule", invalidSpecification);
    Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.getCode(), response.getStatusLine().getStatusCode());
    // test adding a schedule
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, specification);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(2, schedules.size());
    Assert.assertEquals(detail, schedules.get(1));
    List<ScheduleDetail> schedulesForApp = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
    Assert.assertEquals(schedules, schedulesForApp);
    // trying to add ScheduleDetail of the same schedule again should fail with AlreadyExistsException
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, null, scheduleName, detail);
    Assert.assertEquals(HttpResponseStatus.CONFLICT.getCode(), response.getStatusLine().getStatusCode());
    // although we should be able to add schedule to a different version of the app
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, scheduleName, detail);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    // this should not have affected the schedules of the default version
    List<ScheduleDetail> scheds = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(schedules, scheds);
    // there should be two schedules now for version 2
    List<ScheduleDetail> schedules2 = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, AppWithSchedule.WORKFLOW_NAME);
    Assert.assertEquals(2, schedules2.size());
    Assert.assertEquals(detail, schedules2.get(1));
    List<ScheduleDetail> schedulesForApp2 = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2);
    Assert.assertEquals(schedules2, schedulesForApp2);
    // Add a schedule with no schedule name in spec
    ScheduleDetail detail2 = new ScheduleDetail(null, "Something 2", programInfo, properties, new TimeTrigger("0 * * * ?"), Collections.<Constraint>emptyList(), TimeUnit.HOURS.toMillis(6));
    response = addSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, "schedule-100", detail2);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    ScheduleDetail detail100 = getSchedule(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, "schedule-100");
    Assert.assertEquals("schedule-100", detail100.getName());
    Assert.assertEquals(detail2.getTimeoutMillis(), detail100.getTimeoutMillis());
    // test backward-compatible api
    ScheduleSpecification spec100 = getScheduleSpec(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, "schedule-100");
    Assert.assertEquals(detail100.toScheduleSpec(), spec100);
}
Also used : TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) HttpResponse(org.apache.http.HttpResponse) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 9 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doGetSchedule.

private void doGetSchedule(HttpResponder responder, String namespace, String app, String version, String scheduleName, String format) throws NotFoundException, BadRequestException {
    boolean asScheduleSpec = returnScheduleAsSpec(format);
    ScheduleId scheduleId = new ApplicationId(namespace, app, version).schedule(scheduleName);
    ProgramSchedule schedule = programScheduler.getSchedule(scheduleId);
    ScheduleDetail detail = schedule.toScheduleDetail();
    if (asScheduleSpec) {
        ScheduleSpecification spec = detail.toScheduleSpec();
        if (spec == null) {
            // then this application must be using new APIs and the schedule can't be returned in old form.
            throw new NotFoundException(scheduleId);
        }
        responder.sendJson(HttpResponseStatus.OK, spec, ScheduleSpecification.class, GSON);
    } else {
        responder.sendJson(HttpResponseStatus.OK, detail, ScheduleDetail.class, GSON);
    }
}
Also used : ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ScheduleDetail(co.cask.cdap.proto.ScheduleDetail) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 10 with ScheduleSpecification

use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.

the class AbstractSchedulerService method getSchedulerForSchedule.

private Scheduler getSchedulerForSchedule(ProgramId program, String scheduleName) throws NotFoundException {
    ApplicationSpecification appSpec = store.getApplication(program.getParent());
    if (appSpec == null) {
        throw new ApplicationNotFoundException(program.getParent());
    }
    Map<String, ScheduleSpecification> schedules = appSpec.getSchedules();
    if (schedules == null || !schedules.containsKey(scheduleName)) {
        throw new ScheduleNotFoundException(program.getParent().schedule(scheduleName));
    }
    ScheduleSpecification scheduleSpec = schedules.get(scheduleName);
    Schedule schedule = scheduleSpec.getSchedule();
    return getScheduler(schedule);
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) Schedule(co.cask.cdap.api.schedule.Schedule) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Aggregations

ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)18 TimeSchedule (co.cask.cdap.internal.schedule.TimeSchedule)8 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)8 Test (org.junit.Test)7 ScheduleProgramInfo (co.cask.cdap.api.workflow.ScheduleProgramInfo)6 StreamSizeSchedule (co.cask.cdap.internal.schedule.StreamSizeSchedule)6 ApplicationId (co.cask.cdap.proto.id.ApplicationId)6 Schedule (co.cask.cdap.api.schedule.Schedule)5 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 NotFoundException (co.cask.cdap.common.NotFoundException)4 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)3 Id (co.cask.cdap.proto.Id)3 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)2 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)2 ScheduleCreationSpec (co.cask.cdap.internal.schedule.ScheduleCreationSpec)2 ScheduledRuntime (co.cask.cdap.proto.ScheduledRuntime)2 AppRequest (co.cask.cdap.proto.artifact.AppRequest)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2