Search in sources :

Example 1 with CustomActionContext

use of co.cask.cdap.api.customaction.CustomActionContext in project cdap by caskdata.

the class CustomActionExecutor method executeCustomAction.

private void executeCustomAction() throws Exception {
    try {
        customActionContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
        // AbstractCustomAction implements final initialize(context) and requires subclass to
        // implement initialize(), whereas programs that directly implement CustomAction can
        // override initialize(context)
        TransactionControl txControl = customAction instanceof AbstractCustomAction ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, AbstractCustomAction.class, customAction, "initialize") : Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "initialize", CustomActionContext.class);
        customActionContext.initializeProgram(customAction, customActionContext, txControl, false);
        customActionContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
        customActionContext.executeChecked(new AbstractContext.ThrowingRunnable() {

            @Override
            public void run() throws Exception {
                customAction.run();
            }
        });
        customActionContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
    } catch (Throwable t) {
        customActionContext.setState(new ProgramState(ProgramStatus.FAILED, Throwables.getRootCause(t).getMessage()));
        Throwables.propagateIfPossible(t, Exception.class);
        throw Throwables.propagate(t);
    } finally {
        TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "destroy");
        customActionContext.destroyProgram(customAction, customActionContext, txControl, false);
    }
}
Also used : CustomAction(co.cask.cdap.api.customaction.CustomAction) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) CustomActionContext(co.cask.cdap.api.customaction.CustomActionContext) BasicCustomActionContext(co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext) ProgramState(co.cask.cdap.api.ProgramState) AbstractContext(co.cask.cdap.internal.app.runtime.AbstractContext)

Example 2 with CustomActionContext

use of co.cask.cdap.api.customaction.CustomActionContext in project cdap by caskdata.

the class PipelineAction method run.

@Override
public void run() throws Exception {
    CustomActionContext context = getContext();
    Map<String, String> properties = context.getSpecification().getProperties();
    BatchPhaseSpec phaseSpec = GSON.fromJson(properties.get(Constants.PIPELINEID), BatchPhaseSpec.class);
    PipelinePhase phase = phaseSpec.getPhase();
    StageInfo stageInfo = phase.iterator().next();
    PluginContext pluginContext = new PipelinePluginContext(context, metrics, phaseSpec.isStageLoggingEnabled(), phaseSpec.isProcessTimingEnabled());
    Action action = pluginContext.newPluginInstance(stageInfo.getName(), new DefaultMacroEvaluator(context.getWorkflowToken(), context.getRuntimeArguments(), context.getLogicalStartTime(), context, context.getNamespace()));
    BasicArguments arguments = new BasicArguments(context);
    ActionContext actionContext = new BasicActionContext(context, metrics, stageInfo.getName(), arguments);
    if (!context.getDataTracer(stageInfo.getName()).isEnabled()) {
        action.run(actionContext);
    }
    WorkflowToken token = context.getWorkflowToken();
    if (token == null) {
        throw new IllegalStateException("WorkflowToken cannot be null when action is executed through Workflow.");
    }
    for (Map.Entry<String, String> entry : arguments.getAddedArguments().entrySet()) {
        token.put(entry.getKey(), entry.getValue());
    }
}
Also used : Action(co.cask.cdap.etl.api.action.Action) CustomAction(co.cask.cdap.api.customaction.CustomAction) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) PipelinePluginContext(co.cask.cdap.etl.common.plugin.PipelinePluginContext) PluginContext(co.cask.cdap.api.plugin.PluginContext) StageInfo(co.cask.cdap.etl.planner.StageInfo) WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken) CustomActionContext(co.cask.cdap.api.customaction.CustomActionContext) ActionContext(co.cask.cdap.etl.api.action.ActionContext) PipelinePhase(co.cask.cdap.etl.common.PipelinePhase) DefaultMacroEvaluator(co.cask.cdap.etl.common.DefaultMacroEvaluator) CustomActionContext(co.cask.cdap.api.customaction.CustomActionContext) BatchPhaseSpec(co.cask.cdap.etl.batch.BatchPhaseSpec) BasicArguments(co.cask.cdap.etl.common.BasicArguments) HashMap(java.util.HashMap) Map(java.util.Map) PipelinePluginContext(co.cask.cdap.etl.common.plugin.PipelinePluginContext)

Aggregations

AbstractCustomAction (co.cask.cdap.api.customaction.AbstractCustomAction)2 CustomAction (co.cask.cdap.api.customaction.CustomAction)2 CustomActionContext (co.cask.cdap.api.customaction.CustomActionContext)2 ProgramState (co.cask.cdap.api.ProgramState)1 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)1 PluginContext (co.cask.cdap.api.plugin.PluginContext)1 WorkflowToken (co.cask.cdap.api.workflow.WorkflowToken)1 Action (co.cask.cdap.etl.api.action.Action)1 ActionContext (co.cask.cdap.etl.api.action.ActionContext)1 BatchPhaseSpec (co.cask.cdap.etl.batch.BatchPhaseSpec)1 BasicArguments (co.cask.cdap.etl.common.BasicArguments)1 DefaultMacroEvaluator (co.cask.cdap.etl.common.DefaultMacroEvaluator)1 PipelinePhase (co.cask.cdap.etl.common.PipelinePhase)1 PipelinePluginContext (co.cask.cdap.etl.common.plugin.PipelinePluginContext)1 StageInfo (co.cask.cdap.etl.planner.StageInfo)1 AbstractContext (co.cask.cdap.internal.app.runtime.AbstractContext)1 BasicCustomActionContext (co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1