Search in sources :

Example 6 with IScheduler

use of org.pentaho.platform.api.scheduler2.IScheduler in project pentaho-platform by pentaho.

the class DefaultSchedulerService method updateJob.

private void updateJob(String jobId, Map<String, ParamValue> jobParams, JobTrigger trigger) throws SchedulerException {
    // $NON-NLS-1$
    logger.debug("Creating job with schedule " + trigger.toString());
    try {
        // $NON-NLS-1$
        IScheduler scheduler = PentahoSystem.get(IScheduler.class, "IScheduler2", null);
        Map<String, Serializable> properJobParams = toProperMap(jobParams);
        scheduler.updateJob(jobId, properJobParams, trigger);
    } catch (SchedulerException e) {
        // temporary error logging.. this needs to become an aspect
        logger.error(e.getMessage(), e);
        throw e;
    }
}
Also used : Serializable(java.io.Serializable) SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler)

Example 7 with IScheduler

use of org.pentaho.platform.api.scheduler2.IScheduler in project pentaho-platform by pentaho.

the class DefaultSchedulerService method removeJob.

/**
 * {@inheritDoc}
 */
public void removeJob(String jobId) throws SchedulerException {
    // $NON-NLS-1$
    IScheduler scheduler = PentahoSystem.get(IScheduler.class, "IScheduler2", null);
    scheduler.removeJob(jobId);
}
Also used : IScheduler(org.pentaho.platform.api.scheduler2.IScheduler)

Example 8 with IScheduler

use of org.pentaho.platform.api.scheduler2.IScheduler in project pentaho-platform by pentaho.

the class SolutionImportHandlerIT method init.

@Before
public void init() throws PlatformInitializationException, SchedulerException {
    MicroPlatform mp = new MicroPlatform();
    mp.define("IScheduler", TestQuartzScheduler.class);
    mp.define(IUserRoleListService.class, StubUserRoleListService.class);
    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    mp.defineInstance(IAuthorizationPolicy.class, policy);
    mp.start();
    scheduler = PentahoSystem.get(IScheduler.class);
    scheduler.start();
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) IScheduler(org.pentaho.platform.api.scheduler2.IScheduler) Before(org.junit.Before)

Example 9 with IScheduler

use of org.pentaho.platform.api.scheduler2.IScheduler in project pentaho-platform by pentaho.

the class BlockoutManagerUtil method getFireTimes.

public static List<Date> getFireTimes(IJobTrigger jobTrigger, IScheduler scheduler) {
    // Determines the maximum amount of fire times allowed to be calculated
    int n = 1000;
    Date startDate = new Date(System.currentTimeMillis());
    Date endDate = new Date(startDate.getTime() + 4 * TIME.YEAR.time);
    // Quartz Triggers
    if (scheduler instanceof QuartzScheduler) {
        try {
            List<Date> dates = new ArrayList<Date>();
            boolean endDateIsNull = jobTrigger.getEndTime() == null;
            // $NON-NLS-1$ //$NON-NLS-2$
            Trigger trigger = QuartzScheduler.createQuartzTrigger(jobTrigger, new QuartzJobKey("test", "test"));
            // add previous trigger (it might be currently active)
            // $NON-NLS-1$;
            IBlockoutManager manager = PentahoSystem.get(IBlockoutManager.class, "IBlockoutManager", null);
            if (manager != null) {
                List<Job> blockouts = manager.getBlockOutJobs();
                for (Job blockout : blockouts) {
                    if (blockout.getLastRun() != null) {
                        dates.add(blockout.getLastRun());
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                Date nextFireTime = trigger.getFireTimeAfter(startDate);
                if ((nextFireTime == null) || (nextFireTime.after(endDate) || (!endDateIsNull && nextFireTime.after(jobTrigger.getEndTime())))) {
                    break;
                }
                dates.add(nextFireTime);
                startDate = nextFireTime;
            }
            return dates;
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
    throw new RuntimeException(// $NON-NLS-1$
    "Can not calculate fire times for unsupported Scheduler Type: " + scheduler.getClass().getSimpleName());
}
Also used : SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) QuartzJobKey(org.pentaho.platform.scheduler2.quartz.QuartzJobKey) ArrayList(java.util.ArrayList) QuartzScheduler(org.pentaho.platform.scheduler2.quartz.QuartzScheduler) Date(java.util.Date) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) Trigger(org.quartz.Trigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Job(org.pentaho.platform.api.scheduler2.Job) IBlockoutManager(org.pentaho.platform.api.scheduler2.IBlockoutManager)

Example 10 with IScheduler

use of org.pentaho.platform.api.scheduler2.IScheduler 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

IScheduler (org.pentaho.platform.api.scheduler2.IScheduler)14 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)8 Job (org.pentaho.platform.api.scheduler2.Job)7 Serializable (java.io.Serializable)6 Date (java.util.Date)6 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)5 JobTrigger (org.pentaho.platform.api.scheduler2.JobTrigger)5 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)4 SchedulerException (org.pentaho.platform.api.scheduler2.SchedulerException)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 IBlockoutManager (org.pentaho.platform.api.scheduler2.IBlockoutManager)2 IJobFilter (org.pentaho.platform.api.scheduler2.IJobFilter)2 QuartzScheduler (org.pentaho.platform.scheduler2.quartz.QuartzScheduler)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Map (java.util.Map)1 WebService (javax.jws.WebService)1