Search in sources :

Example 1 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class ScheduledRunTimeTest method testGetNextRun.

@Test
public void testGetNextRun() throws Exception {
    ApplicationId appId = NamespaceId.DEFAULT.app("test");
    deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
    String scheduleName = "schedule1";
    // Add a schedule. Use a constraint to make it not going to be executed
    ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
    Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
    ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
    addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
    long now = System.currentTimeMillis();
    HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
    Assert.assertEquals(200, response.getResponseCode());
    // Get the next run time
    List<ScheduledRuntime> scheduledRunTimes = getScheduledRunTimes(programId, true);
    Assert.assertEquals(1, scheduledRunTimes.size());
    long nextTime = scheduledRunTimes.get(0).getTime();
    Assert.assertTrue(nextTime >= now);
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 2 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class ScheduledRunTimeTest method testBatchGetNextRun.

@Test
public void testBatchGetNextRun() throws Exception {
    // deploys 5 apps and create schedules for each of them
    long now = System.currentTimeMillis();
    List<ProgramId> programIds = new ArrayList<>();
    // Use a constraint to make it not going to be executed
    Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
    for (int i = 0; i < 5; i++) {
        ApplicationId appId = NamespaceId.DEFAULT.app("test" + i);
        deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
        String scheduleName = "schedule" + i;
        // Add a schedule
        ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
        programIds.add(programId);
        ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
        addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
        HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
        Assert.assertEquals(200, response.getResponseCode());
    }
    // Add programs that the app or the program doesn't exist
    programIds.add(NamespaceId.DEFAULT.app("not-exist").workflow("not-exist"));
    programIds.add(NamespaceId.DEFAULT.app("test1").workflow("not-exist"));
    List<BatchProgramSchedule> schedules = getScheduledRunTimes(NamespaceId.DEFAULT.getNamespace(), programIds, true);
    Assert.assertEquals(programIds.size(), schedules.size());
    // For the first 5 programs, they should have a next run
    for (int i = 0; i < 5; i++) {
        BatchProgramSchedule schedule = schedules.get(i);
        Assert.assertEquals(200, schedule.getStatusCode());
        List<ScheduledRuntime> nextRuns = schedule.getSchedules();
        Assert.assertNotNull(nextRuns);
        Assert.assertEquals(1, nextRuns.size());
        long nextTime = nextRuns.get(0).getTime();
        Assert.assertTrue(nextTime >= now);
    }
    // The last two should be a not found
    Assert.assertEquals(404, schedules.get(5).getStatusCode());
    Assert.assertEquals(404, schedules.get(6).getStatusCode());
}
Also used : BatchProgramSchedule(io.cdap.cdap.proto.BatchProgramSchedule) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 3 with ScheduledRuntime

use of io.cdap.cdap.proto.ScheduledRuntime in project cdap by caskdata.

the class TimeSchedulerTest method testNextSchedule.

@Test
public void testNextSchedule() throws Exception {
    // a schedule to be triggered every 5 minutes
    ProgramSchedule sched = new ProgramSchedule("tsched11", "two times schedule", PROG1_ID, ImmutableMap.of("prop2", "xx"), new TimeTrigger("*/5 * * * *"), Collections.emptyList());
    timeScheduler.addProgramSchedule(sched);
    // schedule is by default SUSPENDED after being added, resume it to enable the schedule
    timeScheduler.resumeProgramSchedule(sched);
    long currentTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    long startTimeInSeconds = currentTimeInSeconds + TimeUnit.HOURS.toSeconds(1);
    long endTimeInSeconds = currentTimeInSeconds + TimeUnit.HOURS.toSeconds(3);
    List<ScheduledRuntime> nextRuntimes = timeScheduler.getAllScheduledRunTimes(PROG1_ID, SchedulableProgramType.WORKFLOW, startTimeInSeconds, endTimeInSeconds);
    // for a scan range of 1pm to 3pm. since start time is inclusive, from 1pm tp 2pm we will have 13 schedules
    // and from 2:05 pm to 2:55pm will have 11 schedules as end time is exclusive. in total we expect 24 schedules.
    Assert.assertEquals(24, nextRuntimes.size());
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 4 with ScheduledRuntime

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());
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime)

Example 5 with ScheduledRuntime

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
    }
}
Also used : ProtoTrigger(io.cdap.cdap.proto.ProtoTrigger) NotFoundException(io.cdap.cdap.common.NotFoundException) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Aggregations

ScheduledRuntime (io.cdap.cdap.proto.ScheduledRuntime)9 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)5 HttpResponse (io.cdap.common.http.HttpResponse)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)3 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)3 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)3 ProgramId (io.cdap.cdap.proto.id.ProgramId)3 ScheduleProgramInfo (io.cdap.cdap.api.workflow.ScheduleProgramInfo)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 DelayConstraint (io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint)2 AbstractSatisfiableCompositeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)2 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)2 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2 List (java.util.List)2 Trigger (org.quartz.Trigger)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 TypeToken (com.google.common.reflect.TypeToken)1