Search in sources :

Example 1 with LongRunningTaskExecutor

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());
}
Also used : TestTaskExecutor(eu.bcvsolutions.idm.core.scheduler.task.impl.TestTaskExecutor) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) Function(java.util.function.Function) DefaultLongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.service.impl.DefaultLongRunningTaskManager) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) TestHelper(eu.bcvsolutions.idm.test.api.TestHelper) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) AbstractLongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor) IdmLongRunningTaskService(eu.bcvsolutions.idm.core.scheduler.api.service.IdmLongRunningTaskService) Before(org.junit.Before) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) LongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Assert.assertTrue(org.junit.Assert.assertTrue) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) Test(org.junit.Test) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) ApplicationContext(org.springframework.context.ApplicationContext) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 2 with LongRunningTaskExecutor

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;
    }
}
Also used : IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 3 with LongRunningTaskExecutor

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;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) ArrayList(java.util.ArrayList) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmCheckConcurrentExecution(eu.bcvsolutions.idm.core.scheduler.api.domain.IdmCheckConcurrentExecution) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) UUID(java.util.UUID) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with LongRunningTaskExecutor

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());
}
Also used : IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) TestTaskExecutor(eu.bcvsolutions.idm.core.scheduler.task.impl.TestTaskExecutor) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) Function(java.util.function.Function) IdmProcessedTaskItemService(eu.bcvsolutions.idm.core.scheduler.api.service.IdmProcessedTaskItemService) ArrayList(java.util.ArrayList) AutowireHelper(eu.bcvsolutions.idm.core.api.utils.AutowireHelper) IdentityDeleteBulkAction(eu.bcvsolutions.idm.core.bulk.action.impl.IdentityDeleteBulkAction) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) After(org.junit.After) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) AbstractLongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor) IdmLongRunningTaskService(eu.bcvsolutions.idm.core.scheduler.api.service.IdmLongRunningTaskService) Before(org.junit.Before) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) SchedulerConfiguration(eu.bcvsolutions.idm.core.scheduler.api.config.SchedulerConfiguration) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Assert.assertNotNull(org.junit.Assert.assertNotNull) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) Test(org.junit.Test) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) TaskNotRecoverableException(eu.bcvsolutions.idm.core.scheduler.exception.TaskNotRecoverableException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ExecutionException(java.util.concurrent.ExecutionException) IdmProcessedTaskItemFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) Assert.assertFalse(org.junit.Assert.assertFalse) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) Test(org.junit.Test)

Example 5 with LongRunningTaskExecutor

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());
}
Also used : TestTaskExecutor(eu.bcvsolutions.idm.core.scheduler.task.impl.TestTaskExecutor) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) Function(java.util.function.Function) DefaultLongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.service.impl.DefaultLongRunningTaskManager) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) TestHelper(eu.bcvsolutions.idm.test.api.TestHelper) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) AbstractLongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor) IdmLongRunningTaskService(eu.bcvsolutions.idm.core.scheduler.api.service.IdmLongRunningTaskService) Before(org.junit.Before) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) LongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Assert.assertTrue(org.junit.Assert.assertTrue) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) Test(org.junit.Test) LongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor) ApplicationContext(org.springframework.context.ApplicationContext) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

LongRunningTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskExecutor)6 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)5 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)5 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)4 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)4 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)4 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)4 ConcurrentExecutionException (eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException)4 AbstractLongRunningTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor)4 IdmLongRunningTaskService (eu.bcvsolutions.idm.core.scheduler.api.service.IdmLongRunningTaskService)4 TestTaskExecutor (eu.bcvsolutions.idm.core.scheduler.task.impl.TestTaskExecutor)4 ExecutionException (java.util.concurrent.ExecutionException)4 Function (java.util.function.Function)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertFalse (org.junit.Assert.assertFalse)4 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)4 Assert.assertNotNull (org.junit.Assert.assertNotNull)4 Assert.assertNull (org.junit.Assert.assertNull)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Before (org.junit.Before)4