Search in sources :

Example 1 with ProgramStatusTriggerInfo

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

the class AppWithMultipleSchedules method getNewTokensFromScheduleInfo.

private static Map<String, String> getNewTokensFromScheduleInfo(TriggeringScheduleInfo scheduleInfo, Map<String, String> propertiesMap) {
    List<TriggerInfo> triggerInfoList = scheduleInfo.getTriggerInfos();
    List<ProgramStatusTriggerInfo> programStatusTriggerInfos = new ArrayList<>();
    for (TriggerInfo info : triggerInfoList) {
        if (info instanceof ProgramStatusTriggerInfo) {
            programStatusTriggerInfos.add((ProgramStatusTriggerInfo) info);
        }
    }
    Map<String, String> newRuntimeArgs = new HashMap<>();
    // If no ProgramStatusTriggerInfo, no need of override the existing runtimeArgs
    if (programStatusTriggerInfos.size() == 0) {
        return newRuntimeArgs;
    }
    // token#<user-token-key>:<node-name>
    for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
        String triggeringPropertyName = entry.getKey();
        String[] propertyParts = triggeringPropertyName.split("#");
        if ("runtime-arg".equals(propertyParts[0])) {
            addRuntimeArgs(programStatusTriggerInfos, newRuntimeArgs, propertyParts[1], entry.getValue());
        } else if ("token".equals(propertyParts[0])) {
            addTokens(programStatusTriggerInfos, newRuntimeArgs, propertyParts[1], entry.getValue());
        }
    }
    return newRuntimeArgs;
}
Also used : ProgramStatusTriggerInfo(co.cask.cdap.api.schedule.ProgramStatusTriggerInfo) HashMap(java.util.HashMap) TriggerInfo(co.cask.cdap.api.schedule.TriggerInfo) ProgramStatusTriggerInfo(co.cask.cdap.api.schedule.ProgramStatusTriggerInfo) ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ProgramStatusTriggerInfo

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

the class AppWithMultipleSchedules method addTokens.

private static void addTokens(List<ProgramStatusTriggerInfo> programStatusTriggerInfos, Map<String, String> runtimeArgs, String triggeringKeyNodePair, String key) {
    for (ProgramStatusTriggerInfo triggerInfo : programStatusTriggerInfos) {
        WorkflowToken token = triggerInfo.getWorkflowToken();
        if (token == null) {
            continue;
        }
        String[] keyNode = triggeringKeyNodePair.split(":");
        Value value = token.get(keyNode[0], keyNode[1]);
        if (value == null) {
            continue;
        }
        runtimeArgs.put(key, value.toString());
    }
}
Also used : ProgramStatusTriggerInfo(co.cask.cdap.api.schedule.ProgramStatusTriggerInfo) Value(co.cask.cdap.api.workflow.Value) WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken)

Example 3 with ProgramStatusTriggerInfo

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

the class SmartWorkflow method updateTokenWithTriggeringProperties.

private void updateTokenWithTriggeringProperties(TriggeringScheduleInfo scheduleInfo, TriggeringPropertyMapping propertiesMapping, WorkflowToken token) {
    List<ProgramStatusTriggerInfo> programStatusTriggerInfos = new ArrayList<>();
    for (TriggerInfo info : scheduleInfo.getTriggerInfos()) {
        if (info instanceof ProgramStatusTriggerInfo) {
            programStatusTriggerInfos.add((ProgramStatusTriggerInfo) info);
        }
    }
    // If no ProgramStatusTriggerInfo, no need of override the existing runtimeArgs
    if (programStatusTriggerInfos.isEmpty()) {
        return;
    }
    // Currently only expecting one trigger in a schedule
    ProgramStatusTriggerInfo triggerInfo = programStatusTriggerInfos.get(0);
    BasicArguments triggeringArguments = new BasicArguments(triggerInfo.getWorkflowToken(), triggerInfo.getRuntimeArguments());
    // Get the value of every triggering pipeline arguments specified in the propertiesMapping and update newRuntimeArgs
    List<ArgumentMapping> argumentMappings = propertiesMapping.getArguments();
    for (ArgumentMapping mapping : argumentMappings) {
        String sourceKey = mapping.getSource();
        if (sourceKey == null) {
            LOG.warn("The name of argument from the triggering pipeline cannot be null, " + "skip this argument mapping: '{}'.", mapping);
            continue;
        }
        String value = triggeringArguments.get(sourceKey);
        if (value == null) {
            LOG.warn("Runtime argument '{}' is not found in run '{}' of the triggering pipeline '{}' " + "in namespace '{}' ", sourceKey, triggerInfo.getRunId(), triggerInfo.getApplicationSpecification().getName(), triggerInfo.getNamespace());
            continue;
        }
        // Use the argument name in the triggering pipeline if target is not specified
        String targetKey = mapping.getTarget() == null ? sourceKey : mapping.getTarget();
        token.put(targetKey, value);
    }
    // Get the resolved plugin properties map from triggering pipeline's workflow token in triggeringArguments
    Map<String, Map<String, String>> resolvedProperties = GSON.fromJson(triggeringArguments.get(RESOLVED_PLUGIN_PROPERTIES_MAP), STAGE_PROPERTIES_MAP);
    for (PluginPropertyMapping mapping : propertiesMapping.getPluginProperties()) {
        String stageName = mapping.getStageName();
        if (stageName == null) {
            LOG.warn("The name of the stage cannot be null in plugin property mapping, skip this mapping: '{}'.", mapping);
            continue;
        }
        Map<String, String> pluginProperties = resolvedProperties.get(stageName);
        if (pluginProperties == null) {
            LOG.warn("No plugin properties can be found with stage name '{}' in triggering pipeline '{}' " + "in namespace '{}' ", mapping.getStageName(), triggerInfo.getApplicationSpecification().getName(), triggerInfo.getNamespace());
            continue;
        }
        String sourceKey = mapping.getSource();
        if (sourceKey == null) {
            LOG.warn("The name of argument from the triggering pipeline cannot be null, " + "skip this argument mapping: '{}'.", mapping);
            continue;
        }
        String value = pluginProperties.get(sourceKey);
        if (value == null) {
            LOG.warn("No property with name '{}' can be found in plugin '{}' of the triggering pipeline '{}' " + "in namespace '{}' ", sourceKey, stageName, triggerInfo.getApplicationSpecification().getName(), triggerInfo.getNamespace());
            continue;
        }
        // Use the argument name in the triggering pipeline if target is not specified
        String targetKey = mapping.getTarget() == null ? sourceKey : mapping.getTarget();
        token.put(targetKey, value);
    }
    return;
}
Also used : ArgumentMapping(co.cask.cdap.etl.proto.v2.ArgumentMapping) ProgramStatusTriggerInfo(co.cask.cdap.api.schedule.ProgramStatusTriggerInfo) ArrayList(java.util.ArrayList) ProgramStatusTriggerInfo(co.cask.cdap.api.schedule.ProgramStatusTriggerInfo) TriggerInfo(co.cask.cdap.api.schedule.TriggerInfo) PluginPropertyMapping(co.cask.cdap.etl.proto.v2.PluginPropertyMapping) BasicArguments(co.cask.cdap.etl.common.BasicArguments) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ProgramStatusTriggerInfo (co.cask.cdap.api.schedule.ProgramStatusTriggerInfo)3 TriggerInfo (co.cask.cdap.api.schedule.TriggerInfo)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Value (co.cask.cdap.api.workflow.Value)1 WorkflowToken (co.cask.cdap.api.workflow.WorkflowToken)1 BasicArguments (co.cask.cdap.etl.common.BasicArguments)1 ArgumentMapping (co.cask.cdap.etl.proto.v2.ArgumentMapping)1 PluginPropertyMapping (co.cask.cdap.etl.proto.v2.PluginPropertyMapping)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 LinkedHashMap (java.util.LinkedHashMap)1