Search in sources :

Example 6 with Trigger

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

the class ProgramScheduleService method update.

/**
 * Update the given schedule
 *
 * @param scheduleId the schedule to update
 * @param scheduleDetail the schedule to update it to
 * @throws NotFoundException if the schedule could not be found
 * @throws BadRequestException if the update is invalid
 * @throws UnauthorizedException if the principal is not authorized as an admin operations on the schedule program
 * @throws Exception if any other errors occurred while performing the authorization enforcement check
 */
public void update(ScheduleId scheduleId, ScheduleDetail scheduleDetail) throws Exception {
    authorizationEnforcer.enforce(scheduleId.getParent(), authenticationContext.getPrincipal(), Action.ADMIN);
    ProgramSchedule existing = scheduler.getSchedule(scheduleId);
    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());
    ProgramSchedule updatedSchedule = new ProgramSchedule(existing.getName(), description, programId, properties, trigger, constraints, timeoutMillis);
    scheduler.updateSchedule(updatedSchedule);
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 7 with Trigger

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

the class AppWithFrequentScheduledWorkflows method configure.

@Override
public void configure() {
    setName(NAME);
    setDescription("Sample application with multiple Workflows");
    addWorkflow(new DummyWorkflow(SOME_WORKFLOW));
    addWorkflow(new DummyWorkflow(ANOTHER_WORKFLOW));
    addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_1));
    addWorkflow(new DummyWorkflow(SCHEDULED_WORKFLOW_2));
    addWorkflow(new DummyWorkflow(COMPOSITE_WORKFLOW));
    schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_1, ProgramType.WORKFLOW, SOME_WORKFLOW).triggerOnPartitions(DATASET_NAME1, 1));
    schedule(buildSchedule(DATASET_PARTITION_SCHEDULE_2, ProgramType.WORKFLOW, ANOTHER_WORKFLOW).triggerOnPartitions(DATASET_NAME2, 2));
    // Schedule the workflow to run in every ten seconds
    schedule(buildSchedule(TEN_SECOND_SCHEDULE_1, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1).triggerByTime("*/10 * * * * ?"));
    // Schedule the workflow to run in every ten seconds
    schedule(buildSchedule(TEN_SECOND_SCHEDULE_2, ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_2).triggerByTime("*/10 * * * * ?"));
    // OrTrigger with only PartitionTrigger to be triggered
    Trigger orTrigger1 = getTriggerFactory().or(getTriggerFactory().onPartitions(DATASET_NAME2, 3), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
    // OrTrigger with only TimeTrigger to be triggered
    Trigger orTrigger2 = getTriggerFactory().or(getTriggerFactory().byTime("*/5 * * * * ?"), getTriggerFactory().onProgramStatus(ProgramType.WORKFLOW, SCHEDULED_WORKFLOW_1, ProgramStatus.KILLED));
    schedule(buildSchedule(COMPOSITE_SCHEDULE, ProgramType.WORKFLOW, COMPOSITE_WORKFLOW).triggerOn(getTriggerFactory().and(orTrigger1, orTrigger2)));
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger)

Example 8 with Trigger

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

the class AbstractCompositeTriggerBuilder method getBuiltTriggers.

protected SatisfiableTrigger[] getBuiltTriggers(String namespace, String applicationName, String applicationVersion) {
    int numTriggers = triggers.length;
    SatisfiableTrigger[] builtTriggers = new SatisfiableTrigger[numTriggers];
    for (int i = 0; i < numTriggers; i++) {
        Trigger trigger = triggers[i];
        builtTriggers[i] = trigger instanceof TriggerBuilder ? ((TriggerBuilder) trigger).build(namespace, applicationName, applicationVersion) : (SatisfiableTrigger) trigger;
    }
    return builtTriggers;
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger)

Example 9 with Trigger

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

the class TriggerCodecTest method testSerDeserYieldsTrigger.

private void testSerDeserYieldsTrigger(ProtoTrigger proto, Trigger trigger) {
    String jsonOfTrigger = GSON.toJson(trigger);
    String jsonOfTriggerAsTrigger = GSON.toJson(trigger, Trigger.class);
    String jsonOfProto = GSON.toJson(proto);
    String jsonOfProtoAsTrigger = GSON.toJson(proto, Trigger.class);
    String jsonOfTriggerByProto = GSON_PROTO.toJson(trigger);
    String jsonOfTriggerAsTriggerByProto = GSON_PROTO.toJson(trigger, Trigger.class);
    String jsonOfProtoByProto = GSON_PROTO.toJson(proto);
    String jsonOfProtoAsTriggerByProto = GSON_PROTO.toJson(proto, Trigger.class);
    Assert.assertEquals(jsonOfTrigger, jsonOfTriggerAsTrigger);
    Assert.assertEquals(jsonOfTrigger, jsonOfProto);
    Assert.assertEquals(jsonOfTrigger, jsonOfProtoAsTrigger);
    Assert.assertEquals(jsonOfTrigger, jsonOfTriggerByProto);
    Assert.assertEquals(jsonOfTrigger, jsonOfTriggerAsTriggerByProto);
    Assert.assertEquals(jsonOfTrigger, jsonOfProtoByProto);
    Assert.assertEquals(jsonOfTrigger, jsonOfProtoAsTriggerByProto);
    Trigger deserialized = GSON.fromJson(jsonOfTrigger, Trigger.class);
    Trigger deserializedAsProto = GSON_PROTO.fromJson(jsonOfTrigger, Trigger.class);
    Assert.assertEquals(trigger, deserialized);
    Assert.assertEquals(proto, deserializedAsProto);
}
Also used : Trigger(co.cask.cdap.api.schedule.Trigger) ProtoTrigger(co.cask.cdap.proto.ProtoTrigger)

Aggregations

Trigger (co.cask.cdap.api.schedule.Trigger)9 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ProtoTrigger (co.cask.cdap.proto.ProtoTrigger)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 HashSet (java.util.HashSet)2 ProgramStatus (co.cask.cdap.api.ProgramStatus)1 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ProgramScheduleRecord (co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)1 AbstractSatisfiableCompositeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)1 ProgramStatusTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)1 SatisfiableTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)1 ScheduleId (co.cask.cdap.proto.id.ScheduleId)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1