use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManager method createTrigger.
@Override
public AbstractTaskTrigger createTrigger(String taskId, AbstractTaskTrigger trigger, boolean dryRun) {
Assert.notNull(taskId);
Assert.notNull(trigger);
//
// task has to support dry run mode
Task task = getTask(taskId);
Assert.notNull(task);
if (dryRun && !task.isSupportsDryRun()) {
throw new DryRunNotSupportedException(task.getTaskType().getCanonicalName());
}
//
String triggerId = Key.createUniqueName(taskId);
trigger.setId(triggerId);
trigger.setTaskId(taskId);
// TODO use of visitor pattern may be good
if (trigger instanceof CronTaskTrigger) {
createTriggerInternal(taskId, (CronTaskTrigger) trigger, dryRun);
} else if (trigger instanceof SimpleTaskTrigger) {
createTriggerInternal(taskId, (SimpleTaskTrigger) trigger, dryRun);
} else if (trigger instanceof DependentTaskTrigger) {
createTriggerInternal(taskId, (DependentTaskTrigger) trigger);
} else {
throw new SchedulerException(CoreResultCode.SCHEDULER_UNSUPPORTED_TASK_TRIGGER, ImmutableMap.of("trigger", trigger.getClass()));
}
return trigger;
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testDependentTaskExecution.
@Test
@SuppressWarnings("unchecked")
public void testDependentTaskExecution() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
String resultValue = "dependent-task-initiator";
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.CREATED);
filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
filter.setFrom(new DateTime());
List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(0L, createdLrts.size());
//
Task task = createTask("dependent-task-initiator");
DependentTaskTrigger trigger = new DependentTaskTrigger();
trigger.setInitiatorTaskId(task.getId());
//
// initiator = dependent task => circular execution
manager.createTrigger(task.getId(), trigger);
manager.runTask(task.getId());
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, createdLrts.size());
// execute first task
LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
//
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
//
longRunningTaskManager.cancel(createdLrts.get(0).getId());
//
longRunningTaskService.find(filter, null).getContent();
// cancel - clean up
Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testDependentTaskInDryModeExecution.
@Test
@SuppressWarnings("unchecked")
public void testDependentTaskInDryModeExecution() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
String resultValue = "dependent-task-initiator-dry-run";
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.CREATED);
filter.setTaskType(TestSchedulableDryRunTask.class.getCanonicalName());
filter.setFrom(new DateTime());
List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(0L, createdLrts.size());
//
Task task = createDryRunTask(resultValue);
DependentTaskTrigger trigger = new DependentTaskTrigger();
trigger.setInitiatorTaskId(task.getId());
//
// initiator = dependent task => circular execution
manager.createTrigger(task.getId(), trigger);
manager.runTask(task.getId(), true);
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, createdLrts.size());
Assert.assertTrue(createdLrts.get(0).isDryRun());
UUID firstTaskId = createdLrts.get(0).getId();
// execute first task
LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
//
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
Assert.assertTrue(createdLrts.get(0).isDryRun());
Assert.assertNotEquals(firstTaskId, createdLrts.get(0).getId());
//
longRunningTaskManager.cancel(createdLrts.get(0).getId());
//
longRunningTaskService.find(filter, null).getContent();
// cancel - clean up
Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManager method getTask.
/**
* Returns task by given key
*
* @param jobKey
* @return
*/
@SuppressWarnings("unchecked")
private Task getTask(JobKey jobKey) {
try {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail == null) {
// job does not exists
return null;
}
Task task = new Task();
// task setting
task.setId(jobKey.getName());
// AutowireHelper is not needed here
SchedulableTaskExecutor<?> taskExecutor = (SchedulableTaskExecutor<?>) jobDetail.getJobClass().newInstance();
task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) taskExecutor.getClass());
task.setSupportsDryRun(taskExecutor.supportsDryRun());
task.setDescription(jobDetail.getDescription());
task.setInstanceId(jobDetail.getJobDataMap().getString(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID));
task.setTriggers(new ArrayList<>());
// task properties
for (Entry<String, Object> entry : jobDetail.getJobDataMap().entrySet()) {
task.getParameters().put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
}
// scheduled triggers - native
for (Trigger trigger : scheduler.getTriggersOfJob(jobKey)) {
TriggerState state = scheduler.getTriggerState(trigger.getKey());
if (trigger instanceof CronTrigger) {
task.getTriggers().add(new CronTaskTrigger(task.getId(), (CronTrigger) trigger, TaskTriggerState.convert(state)));
} else if (trigger instanceof SimpleTrigger) {
task.getTriggers().add(new SimpleTaskTrigger(task.getId(), (SimpleTrigger) trigger, TaskTriggerState.convert(state)));
} else {
LOG.warn("Job '{}' ({}) has registered trigger of unsupported type {}", jobKey, jobDetail.getJobClass(), trigger);
}
}
// dependent tasks
dependentTaskTriggerRepository.findByDependentTaskId(jobKey.getName()).forEach(dependentTask -> {
task.getTriggers().add(new DependentTaskTrigger(task.getId(), dependentTask.getId(), dependentTask.getInitiatorTaskId()));
});
return task;
} catch (org.quartz.SchedulerException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
deleteTask(jobKey.getName());
LOG.warn("Job [{}] inicialization failed, job class was removed, scheduled task is removed.", jobKey, ex);
return null;
}
throw new CoreException(ex);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException ex) {
deleteTask(jobKey.getName());
LOG.warn("Job [{}] inicialization failed, scheduled task is removed", jobKey, ex);
return null;
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testDependentTaskNoExecutionAfterInitiatorFails.
@Test
public void testDependentTaskNoExecutionAfterInitiatorFails() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
LongRunningTaskExecuteDependentProcessor processor = context.getBean(LongRunningTaskExecuteDependentProcessor.class);
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.CREATED);
filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
filter.setFrom(new DateTime());
List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(0L, createdLrts.size());
//
Task task = createTask("dependent-task-initiator");
DependentTaskTrigger trigger = new DependentTaskTrigger();
trigger.setInitiatorTaskId(task.getId());
//
// initiator = dependent task => circular execution
manager.createTrigger(task.getId(), trigger);
manager.runTask(task.getId());
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, createdLrts.size());
IdmLongRunningTaskDto lrt = createdLrts.get(0);
lrt.setResult(new OperationResult(OperationState.EXCEPTION));
// not executed
EventResult<IdmLongRunningTaskDto> result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
lrt.setResult(new OperationResult(OperationState.BLOCKED));
result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
lrt.setResult(new OperationResult(OperationState.NOT_EXECUTED));
result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
lrt.setResult(new OperationResult(OperationState.CANCELED));
result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
lrt.setResult(new OperationResult(OperationState.CREATED));
result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
lrt.setResult(new OperationResult(OperationState.RUNNING));
result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
//
longRunningTaskManager.cancel(lrt.getId());
//
longRunningTaskService.find(filter, null).getContent();
// cancel - clean up
Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
Aggregations