use of eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManagerIntegrationTest method testCancelRunningTask.
@Test
public void testCancelRunningTask() throws InterruptedException, ExecutionException {
String result = "TEST_SUCCESS_04";
Long count = 100L;
LongRunningTaskExecutor<String> taskExecutor = new TestStopableLongRunningTaskExecutor(result, count);
//
LongRunningFutureTask<String> futureTask = manager.execute(taskExecutor);
//
Function<String, Boolean> continueFunction = res -> {
return !manager.getLongRunningTask(futureTask).isRunning();
};
helper.waitForResult(continueFunction);
//
manager.cancel(taskExecutor.getLongRunningTaskId());
//
assertEquals(result, futureTask.getFutureTask().get());
//
IdmLongRunningTaskDto longRunningTask = service.get(taskExecutor.getLongRunningTaskId());
assertEquals(OperationState.CANCELED, longRunningTask.getResult().getState());
assertEquals(count, longRunningTask.getCount());
assertNotEquals(count, longRunningTask.getCounter());
}
use of eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManager method getTaskFormInstance.
@Override
public IdmFormInstanceDto getTaskFormInstance(IdmLongRunningTaskDto longRunningTask) {
String taskType = longRunningTask.getTaskType();
//
try {
LongRunningTaskExecutor<?> executor = (LongRunningTaskExecutor<?>) AutowireHelper.createBean(Class.forName(taskType));
if (executor.isDisabled() || !enabledEvaluator.isEnabled(executor)) {
LOG.info("Task executor [{}] is disabled - form instance will not be created.", taskType);
return null;
}
IdmFormInstanceDto formInstance = executor.getFormInstance(new ConfigurationMap(longRunningTask.getTaskProperties()));
if (formInstance == null) {
return null;
}
//
formInstance.setOwnerId(longRunningTask.getId());
formInstance.setOwnerType(longRunningTask.getClass());
//
return formInstance;
} catch (ClassNotFoundException | NoSuchBeanDefinitionException ex) {
// disable or removed evaluator classes
LOG.warn("Task executor [{}] not found.", taskType);
return null;
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManager method processCreated.
/**
* Executes long running task on this instance
*/
@Override
@Transactional
public synchronized List<LongRunningFutureTask<?>> processCreated() {
String instanceId = configurationService.getInstanceId();
LOG.debug("Processing created tasks from long running task queue on instance id [{}]", instanceId);
// Prevent to mess up logged identity authentication => tasks can be started manually => will be visible in audit
if (!securityService.isAuthenticated()) {
securityService.setSystemAuthentication();
}
//
Set<String> processedTaskTypes = Sets.newHashSet();
List<LongRunningFutureTask<?>> taskList = new ArrayList<LongRunningFutureTask<?>>();
for (IdmLongRunningTaskDto task : service.findAllByInstance(instanceId, OperationState.CREATED)) {
String taskType = task.getTaskType();
UUID taskId = task.getId();
//
if (processedTaskTypes.contains(taskType)) {
LOG.debug("Task type [{}] is already processed.", taskType);
continue;
}
// check concurrent tasks before task is executed
try {
Class<?> executorClass = Class.forName(taskType);
if (executorClass.isAnnotationPresent(IdmCheckConcurrentExecution.class)) {
IdmCheckConcurrentExecution disallowConcurrentExecution = executorClass.getAnnotation(IdmCheckConcurrentExecution.class);
Class<? extends LongRunningTaskExecutor<?>>[] disallowConcurrentTaskTypes = disallowConcurrentExecution.taskTypes();
if (disallowConcurrentTaskTypes.length > 0 && CollectionUtils.containsAny(processedTaskTypes, Arrays.asList(disallowConcurrentTaskTypes).stream().map(Class::getCanonicalName).collect(Collectors.toList()))) {
LOG.debug("Concurrent task for task type [{}] is already processed.", taskType);
processedTaskTypes.add(taskType);
continue;
}
}
} catch (ClassNotFoundException ex) {
LOG.warn("Class [{}] for long running task [{}] not found. Task execution will be skipped.", taskType, task.getId());
}
//
try {
LongRunningFutureTask<?> futureTask = processCreated(taskId);
if (futureTask != null) {
taskList.add(futureTask);
// prevent to persisted task starts twice
Class<?> executorClass = AutowireHelper.getTargetClass(futureTask.getExecutor());
if (executorClass.isAnnotationPresent(DisallowConcurrentExecution.class)) {
processedTaskTypes.add(taskType);
}
if (executorClass.isAnnotationPresent(IdmCheckConcurrentExecution.class)) {
IdmCheckConcurrentExecution disallowConcurrentExecution = executorClass.getAnnotation(IdmCheckConcurrentExecution.class);
Class<? extends LongRunningTaskExecutor<?>>[] disallowConcurrentTaskTypes = disallowConcurrentExecution.taskTypes();
if (disallowConcurrentTaskTypes.length == 0) {
processedTaskTypes.add(taskType);
}
}
// task is processed => remove from failed tasks to log exception again, when task fails in future
failedLoggedTask.remove(taskId);
}
} catch (ResultCodeException ex) {
// we want to process other task, if some task fails and log just once
processedTaskTypes.add(taskType);
if (!failedLoggedTask.contains(taskId)) {
// we want to know in log, some scheduled task is not complete before next execution attempt
ExceptionUtils.log(LOG, ex);
failedLoggedTask.add(taskId);
}
}
}
return taskList;
}
use of eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManagerIntegrationTest method testInterruptRunningTask.
@Test
public void testInterruptRunningTask() throws InterruptedException, ExecutionException {
String result = "TEST_SUCCESS_05";
Long count = 100L;
LongRunningTaskExecutor<String> taskExecutor = new TestStopableLongRunningTaskExecutor(result, count);
//
manager.execute(taskExecutor);
IdmLongRunningTaskDto longRunningTask = manager.getLongRunningTask(taskExecutor);
// task has to be marked as running immediately
Assert.assertEquals(OperationState.RUNNING, longRunningTask.getResult().getState());
//
Function<String, Boolean> continueFunction = res -> {
return !manager.getLongRunningTask(taskExecutor).isRunning();
};
getHelper().waitForResult(continueFunction);
//
longRunningTask = service.get(taskExecutor.getLongRunningTaskId());
assertEquals(OperationState.RUNNING, longRunningTask.getResult().getState());
assertEquals(count, longRunningTask.getCount());
assertTrue(longRunningTask.isRunning());
//
assertTrue(manager.interrupt(taskExecutor.getLongRunningTaskId()));
//
longRunningTask = service.get(taskExecutor.getLongRunningTaskId());
assertNotEquals(OperationState.RUNNING, longRunningTask.getResult().getState());
assertEquals(count, longRunningTask.getCount());
assertNotEquals(count, longRunningTask.getCounter());
assertFalse(longRunningTask.isRunning());
}
use of eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManagerIntegrationTest method testInterruptRunningTask.
@Test
public void testInterruptRunningTask() throws InterruptedException, ExecutionException {
String result = "TEST_SUCCESS_05";
Long count = 100L;
LongRunningTaskExecutor<String> taskExecutor = new TestStopableLongRunningTaskExecutor(result, count);
//
manager.execute(taskExecutor);
//
Function<String, Boolean> continueFunction = res -> {
return !manager.getLongRunningTask(taskExecutor).isRunning();
};
helper.waitForResult(continueFunction);
//
IdmLongRunningTaskDto longRunningTask = service.get(taskExecutor.getLongRunningTaskId());
assertEquals(OperationState.RUNNING, longRunningTask.getResult().getState());
assertEquals(count, longRunningTask.getCount());
assertTrue(longRunningTask.isRunning());
//
assertTrue(manager.interrupt(taskExecutor.getLongRunningTaskId()));
//
longRunningTask = service.get(taskExecutor.getLongRunningTaskId());
assertNotEquals(OperationState.RUNNING, longRunningTask.getResult().getState());
assertEquals(count, longRunningTask.getCount());
assertNotEquals(count, longRunningTask.getCounter());
assertFalse(longRunningTask.isRunning());
}
Aggregations