use of org.pentaho.platform.api.scheduler2.SimpleJobTrigger in project pentaho-platform by pentaho.
the class QuartzSchedulerIT method createQuartzTriggerSimpleTriggerTest.
@Test
public void createQuartzTriggerSimpleTriggerTest() throws Exception {
final Calendar calendar = Calendar.getInstance();
Date startTime = calendar.getTime();
calendar.add(Calendar.MONTH, 1);
Date endTime = calendar.getTime();
int repeatCount = 5;
long repeatIntervalSeconds = 10;
final SimpleJobTrigger simpleJobTrigger = new SimpleJobTrigger(startTime, endTime, repeatCount, repeatIntervalSeconds);
final Trigger quartzTrigger = QuartzScheduler.createQuartzTrigger(simpleJobTrigger, getJobKey());
assertNotNull(quartzTrigger);
assertTrue(quartzTrigger instanceof SimpleTrigger);
assertEquals(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT, quartzTrigger.getMisfireInstruction());
assertEquals(USER_NAME, quartzTrigger.getGroup());
SimpleTrigger simpleTrigger = (SimpleTrigger) quartzTrigger;
assertEquals(startTime, simpleTrigger.getStartTime());
assertEquals(endTime, simpleTrigger.getEndTime());
assertEquals(repeatCount, simpleTrigger.getRepeatCount());
assertEquals(repeatIntervalSeconds * 1000, simpleTrigger.getRepeatInterval());
}
use of org.pentaho.platform.api.scheduler2.SimpleJobTrigger in project pentaho-platform by pentaho.
the class QuartzSchedulerIT method testJobRunsAsSchedulingUser.
@Test
public void testJobRunsAsSchedulingUser() throws SchedulerException {
SimpleJobTrigger RUN_ONCE_IN_2_SECS = JobTrigger.ONCE_NOW;
RUN_ONCE_IN_2_SECS.setStartTime(new Date(System.currentTimeMillis() + 2000L));
scheduler.createJob("testName", TestAction.class, new HashMap<String, Serializable>(), RUN_ONCE_IN_2_SECS);
SecurityHelper.getInstance().becomeUser("Ima Wronguser");
sleep(3);
Assert.assertEquals("Job did not run the correct number of times", 1, TestAction.counter);
Assert.assertEquals("Job did not run as the correct user", TEST_USER, TestAction.executedAsUser);
List<Job> jobs = scheduler.getJobs(null);
Assert.assertEquals(0, jobs.size());
}
use of org.pentaho.platform.api.scheduler2.SimpleJobTrigger in project pentaho-platform by pentaho.
the class QuartzSchedulerIT method runTenTimesJobTest.
@Test
public void runTenTimesJobTest() throws SchedulerException {
SimpleJobTrigger repeater = new SimpleJobTrigger();
repeater.setStartTime(new Date());
repeater.setRepeatInterval(1);
repeater.setRepeatCount(9);
scheduler.createJob("testName", TestAction.class, new HashMap<String, Serializable>(), repeater);
sleep(5);
Assert.assertTrue("Job did not run the correct number of times", TestAction.counter > 3);
System.out.println("Counter = " + TestAction.counter);
Assert.assertTrue("Job did not run the correct number of times", TestAction.counter < 7);
sleep(10);
Assert.assertEquals("Job did not run the correct number of times", 10, TestAction.counter);
List<Job> jobs = scheduler.getJobs(null);
Assert.assertEquals(0, jobs.size());
}
use of org.pentaho.platform.api.scheduler2.SimpleJobTrigger 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)));
}
}
use of org.pentaho.platform.api.scheduler2.SimpleJobTrigger in project pentaho-platform by pentaho.
the class QuartzScheduler method setJobTrigger.
private void setJobTrigger(Scheduler scheduler, Job job, Trigger trigger) throws SchedulerException, org.quartz.SchedulerException {
QuartzJobKey jobKey = QuartzJobKey.parse(job.getJobId());
String groupName = jobKey.getUserName();
if (trigger instanceof SimpleTrigger) {
SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
SimpleJobTrigger simpleJobTrigger = new SimpleJobTrigger();
simpleJobTrigger.setStartTime(simpleTrigger.getStartTime());
simpleJobTrigger.setEndTime(simpleTrigger.getEndTime());
simpleJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
long interval = simpleTrigger.getRepeatInterval();
if (interval > 0) {
interval /= 1000;
}
simpleJobTrigger.setRepeatInterval(interval);
simpleJobTrigger.setRepeatCount(simpleTrigger.getRepeatCount());
job.setJobTrigger(simpleJobTrigger);
} else if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
ComplexJobTrigger complexJobTrigger = createComplexTrigger(cronTrigger.getCronExpression());
complexJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
job.setJobTrigger(complexJobTrigger);
if (trigger.getCalendarName() != null) {
Calendar calendar = scheduler.getCalendar(trigger.getCalendarName());
if (calendar instanceof QuartzSchedulerAvailability) {
QuartzSchedulerAvailability quartzSchedulerAvailability = (QuartzSchedulerAvailability) calendar;
complexJobTrigger.setStartTime(quartzSchedulerAvailability.getStartTime());
complexJobTrigger.setEndTime(quartzSchedulerAvailability.getEndTime());
}
}
complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
}
int triggerState = scheduler.getTriggerState(job.getJobId(), groupName);
switch(triggerState) {
case Trigger.STATE_NORMAL:
job.setState(JobState.NORMAL);
break;
case Trigger.STATE_BLOCKED:
job.setState(JobState.BLOCKED);
break;
case Trigger.STATE_COMPLETE:
job.setState(JobState.COMPLETE);
break;
case Trigger.STATE_ERROR:
job.setState(JobState.ERROR);
break;
case Trigger.STATE_PAUSED:
job.setState(JobState.PAUSED);
break;
default:
job.setState(JobState.UNKNOWN);
break;
}
job.setJobName(QuartzJobKey.parse(job.getJobId()).getJobName());
job.setNextRun(trigger.getNextFireTime());
job.setLastRun(trigger.getPreviousFireTime());
}
Aggregations