Search in sources :

Example 1 with IActionInvokeStatus

use of org.pentaho.platform.api.action.IActionInvokeStatus in project pentaho-platform by pentaho.

the class DefaultActionInvoker method invokeActionImpl.

/**
 * Invokes the provided {@link IAction} as the provided {@code actionUser}.
 *
 * @param actionBean the {@link IAction} being invoked
 * @param actionUser The user invoking the {@link IAction}
 * @param params     the {@link Map} or parameters needed to invoke the {@link IAction}
 * @return the {@link IActionInvokeStatus} object containing information about the action invocation
 * @throws Exception when the {@code IAction} cannot be invoked for some reason.
 */
protected IActionInvokeStatus invokeActionImpl(final IAction actionBean, final String actionUser, final Map<String, Serializable> params) throws Exception {
    final String workItemUid = ActionUtil.extractUid(params);
    if (actionBean == null || params == null) {
        final String failureMessage = Messages.getInstance().getCantInvokeNullAction();
        WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.FAILED, failureMessage);
        throw new ActionInvocationException(failureMessage);
    }
    WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.IN_PROGRESS);
    if (logger.isDebugEnabled()) {
        logger.debug(Messages.getInstance().getRunningInBackgroundLocally(actionBean.getClass().getName(), params));
    }
    // set the locale, if not already set
    if (params.get(LocaleHelper.USER_LOCALE_PARAM) == null || StringUtils.isEmpty(params.get(LocaleHelper.USER_LOCALE_PARAM).toString())) {
        params.put(LocaleHelper.USER_LOCALE_PARAM, LocaleHelper.getLocale());
    }
    // remove the scheduling infrastructure properties
    ActionUtil.removeKeyFromMap(params, ActionUtil.INVOKER_ACTIONCLASS);
    ActionUtil.removeKeyFromMap(params, ActionUtil.INVOKER_ACTIONID);
    ActionUtil.removeKeyFromMap(params, ActionUtil.INVOKER_ACTIONUSER);
    // build the stream provider
    final IBackgroundExecutionStreamProvider streamProvider = getStreamProvider(params);
    ActionUtil.removeKeyFromMap(params, ActionUtil.INVOKER_STREAMPROVIDER);
    ActionUtil.removeKeyFromMap(params, ActionUtil.INVOKER_UIPASSPARAM);
    final ActionRunner actionBeanRunner = new ActionRunner(actionBean, actionUser, params, streamProvider);
    final IActionInvokeStatus status = new ActionInvokeStatus();
    status.setStreamProvider(streamProvider);
    boolean requiresUpdate = false;
    try {
        if ((StringUtil.isEmpty(actionUser)) || (actionUser.equals("system session"))) {
            // $NON-NLS-1$
            // For now, don't try to run quartz jobs as authenticated if the user
            // that created the job is a system user. See PPP-2350
            requiresUpdate = SecurityHelper.getInstance().runAsAnonymous(actionBeanRunner);
        } else {
            requiresUpdate = SecurityHelper.getInstance().runAsUser(actionUser, actionBeanRunner);
        }
    } catch (final Throwable t) {
        WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.FAILED, t.toString());
        status.setThrowable(t);
    }
    status.setRequiresUpdate(requiresUpdate);
    // Set the execution Status
    status.setExecutionStatus(actionBean.isExecutionSuccessful());
    return status;
}
Also used : IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) IActionInvokeStatus(org.pentaho.platform.api.action.IActionInvokeStatus) ActionInvokeStatus(org.pentaho.platform.action.ActionInvokeStatus) IActionInvokeStatus(org.pentaho.platform.api.action.IActionInvokeStatus) ActionInvocationException(org.pentaho.platform.api.action.ActionInvocationException)

Example 2 with IActionInvokeStatus

use of org.pentaho.platform.api.action.IActionInvokeStatus in project pentaho-platform by pentaho.

the class ActionAdapterQuartzJob method invokeAction.

/**
 * Invokes the {@link IAction} bean that is created from the provided {@code actionClassName} and {@code actionId} as
 * the provided {@code actionUser}. If the {@code IAction} execution fails as-is, the scheduler attempts to re-create
 * the job that will try to invoke the {@link IAction} again.
 *
 * @param actionClassName The class name of the {@link IAction} bean; used as a backup, if the {@code actionId} is not
 *                        available or vald
 * @param actionId        The bean id of the {@link IAction} requested to be invoked.
 * @param actionUser      The user invoking the {@link IAction}
 * @param context         the {@code JobExecutionContext}
 * @param params          the {@link Map} or parameters needed to invoke the {@link IAction}
 * @throws Exception when the {@code IAction} cannot be invoked for some reason.
 */
protected void invokeAction(final String actionClassName, final String actionId, final String actionUser, final JobExecutionContext context, final Map<String, Serializable> params) throws Exception {
    final String workItemUid = ActionUtil.extractUid(params);
    WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.SUBMITTED);
    // creates an instance of IActionInvoker, which knows how to invoke this IAction - if the IActionInvoker bean is
    // not defined through spring, fall back on the default action invoker
    final IActionInvoker actionInvoker = Optional.ofNullable(PentahoSystem.get(IActionInvoker.class)).orElse(getActionInvoker());
    // Instantiate the requested IAction bean
    final IAction actionBean = (IAction) ActionUtil.createActionBean(actionClassName, actionId);
    if (actionInvoker == null || actionBean == null) {
        final String failureMessage = Messages.getInstance().getErrorString(// $NON-NLS-1$
        "ActionAdapterQuartzJob.ERROR_0002_FAILED_TO_CREATE_ACTION", getActionIdentifier(null, actionClassName, actionId), StringUtil.getMapAsPrettyString(params));
        WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.FAILED, failureMessage);
        throw new LoggingJobExecutionException(failureMessage);
    }
    if (actionBean instanceof BlockoutAction) {
        params.put(IBlockoutManager.SCHEDULED_FIRE_TIME, context.getScheduledFireTime());
    }
    // Invoke the action and get the status of the invocation
    final IActionInvokeStatus status = actionInvoker.invokeAction(actionBean, actionUser, getSerializableMap(params));
    // Status may not be available for remote execution, which is expected
    if (status == null) {
        if (log.isWarnEnabled()) {
            log.warn(Messages.getInstance().getErrorString(// $NON-NLS-1$
            "ActionAdapterQuartzJob.WARN_0002_NO_STATUS", getActionIdentifier(actionBean, actionClassName, actionId), StringUtil.getMapAsPrettyString(params)));
        }
        return;
    }
    // exception
    if (!status.isExecutionSuccessful()) {
        // throw job exception
        throw new JobExecutionException(Messages.getInstance().getActionFailedToExecute(// $NON-NLS-1$
        actionBean.getClass().getName()));
    }
    final boolean requiresUpdate = status.requiresUpdate();
    final Throwable throwable = status.getThrowable();
    Object objsp = status.getStreamProvider();
    IBackgroundExecutionStreamProvider sp = null;
    if (objsp != null && IBackgroundExecutionStreamProvider.class.isAssignableFrom(objsp.getClass())) {
        sp = (IBackgroundExecutionStreamProvider) objsp;
    }
    final IBackgroundExecutionStreamProvider streamProvider = sp;
    // shallow copy
    final Map<String, Serializable> jobParams = new HashMap<String, Serializable>(params);
    final IScheduler scheduler = PentahoSystem.getObjectFactory().get(IScheduler.class, "IScheduler2", null);
    if (throwable != null) {
        Object restartFlag = jobParams.get(QuartzScheduler.RESERVEDMAPKEY_RESTART_FLAG);
        if (restartFlag == null) {
            final SimpleJobTrigger trigger = new SimpleJobTrigger(new Date(), null, 0, 0);
            final Class<IAction> iaction = (Class<IAction>) actionBean.getClass();
            // recreate the job in the context of the original creator
            SecurityHelper.getInstance().runAsUser(actionUser, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    if (streamProvider != null) {
                        // remove generated content
                        streamProvider.setStreamingAction(null);
                    }
                    QuartzJobKey jobKey = QuartzJobKey.parse(context.getJobDetail().getName());
                    String jobName = jobKey.getJobName();
                    jobParams.put(QuartzScheduler.RESERVEDMAPKEY_RESTART_FLAG, Boolean.TRUE);
                    WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.RESTARTED);
                    scheduler.createJob(jobName, iaction, jobParams, trigger, streamProvider);
                    log.warn("New RunOnce job created for " + jobName + " -> possible startup synchronization error");
                    return null;
                }
            });
        } else {
            log.warn("RunOnce already created, skipping");
        }
        throw new JobExecutionException(throwable);
    }
    scheduler.fireJobCompleted(actionBean, actionUser, params, streamProvider);
    if (requiresUpdate) {
        log.warn("Output path for job: " + context.getJobDetail().getName() + " has changed. Job requires update");
        try {
            final IJobTrigger trigger = scheduler.getJob(context.getJobDetail().getName()).getJobTrigger();
            final Class<IAction> iaction = (Class<IAction>) actionBean.getClass();
            // remove job with outdated/invalid output path
            scheduler.removeJob(context.getJobDetail().getName());
            // recreate the job in the context of the original creator
            SecurityHelper.getInstance().runAsUser(actionUser, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    // remove generated content
                    streamProvider.setStreamingAction(null);
                    QuartzJobKey jobKey = QuartzJobKey.parse(context.getJobDetail().getName());
                    String jobName = jobKey.getJobName();
                    WorkItemLifecycleEventUtil.publish(workItemUid, params, WorkItemLifecyclePhase.RESTARTED);
                    org.pentaho.platform.api.scheduler2.Job j = scheduler.createJob(jobName, iaction, jobParams, trigger, streamProvider);
                    log.warn("New Job: " + j.getJobId() + " created");
                    return null;
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(MessageFormat.format("Scheduling system successfully invoked action {0} as user {1} with params [ {2} ]", // $NON-NLS-1$
        actionBean.getClass().getName(), actionUser, QuartzScheduler.prettyPrintMap(params)));
    }
}
Also used : IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) HashMap(java.util.HashMap) BlockoutAction(org.pentaho.platform.scheduler2.blockout.BlockoutAction) JobExecutionException(org.quartz.JobExecutionException) Job(org.quartz.Job) IAction(org.pentaho.platform.api.action.IAction) IActionInvoker(org.pentaho.platform.api.action.IActionInvoker) Date(java.util.Date) JobExecutionException(org.quartz.JobExecutionException) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) IActionInvokeStatus(org.pentaho.platform.api.action.IActionInvokeStatus) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler)

Aggregations

IActionInvokeStatus (org.pentaho.platform.api.action.IActionInvokeStatus)2 IBackgroundExecutionStreamProvider (org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider)2 Serializable (java.io.Serializable)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ActionInvokeStatus (org.pentaho.platform.action.ActionInvokeStatus)1 ActionInvocationException (org.pentaho.platform.api.action.ActionInvocationException)1 IAction (org.pentaho.platform.api.action.IAction)1 IActionInvoker (org.pentaho.platform.api.action.IActionInvoker)1 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)1 IScheduler (org.pentaho.platform.api.scheduler2.IScheduler)1 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)1 BlockoutAction (org.pentaho.platform.scheduler2.blockout.BlockoutAction)1 Job (org.quartz.Job)1 JobExecutionException (org.quartz.JobExecutionException)1