Search in sources :

Example 1 with ConcurrentExecutionException

use of eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException in project CzechIdMng by bcvsolutions.

the class AbstractLongRunningTaskExecutor method validate.

/**
 * Validates task before start e.q. if task already running or to prevent run task concurrently.
 *
 * Look out: override this method additively
 *
 * @param task persisted task to validate
 */
@Override
public void validate(IdmLongRunningTaskDto task) {
    Assert.notNull(task, "Long running task has to be prepared before task is started");
    // 
    if (task.isRunning()) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_RUNNING, ImmutableMap.of("taskId", task.getId()));
    }
    if (!OperationState.isRunnable(task.getResultState())) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_PROCESSED, ImmutableMap.of("taskId", task.getId()));
    }
    // 
    // check concurrent task is not running (or waiting => operation state is used)
    String taskType = task.getTaskType();
    if (this.getClass().isAnnotationPresent(DisallowConcurrentExecution.class)) {
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setTaskType(taskType);
        filter.setOperationState(OperationState.RUNNING);
        // ignore waiting tasks
        filter.setRunning(Boolean.TRUE);
        List<UUID> runningTasks = longRunningTaskService.findIds(filter, null).getContent().stream().filter(t -> {
            // not self
            return !t.equals(task.getId());
        }).collect(Collectors.toList());
        if (!runningTasks.isEmpty()) {
            throw new ConcurrentExecutionException(CoreResultCode.LONG_RUNNING_TASK_IS_RUNNING, ImmutableMap.of("taskId", getName()));
        }
    }
    if (this.getClass().isAnnotationPresent(IdmCheckConcurrentExecution.class)) {
        List<String> disallowConcurrentTaskTypes = new ArrayList<>();
        IdmCheckConcurrentExecution disallowConcurrentExecution = this.getClass().getAnnotation(IdmCheckConcurrentExecution.class);
        Class<? extends LongRunningTaskExecutor<?>>[] taskTypes = disallowConcurrentExecution.taskTypes();
        if (taskTypes.length == 0) {
            disallowConcurrentTaskTypes.add(taskType);
        } else {
            disallowConcurrentTaskTypes.addAll(// TODO: move to utils somewhere - DRY manager
            Arrays.asList(taskTypes).stream().map(Class::getCanonicalName).collect(Collectors.toList()));
        }
        // TODO: filter.setTaskTypes(...)
        disallowConcurrentTaskTypes.forEach(concurrentTaskType -> {
            IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
            filter.setTaskType(concurrentTaskType);
            filter.setOperationState(OperationState.RUNNING);
            // ignore waiting tasks
            filter.setRunning(Boolean.TRUE);
            List<UUID> runningTasks = longRunningTaskService.findIds(filter, null).getContent().stream().filter(t -> {
                // not self
                return !t.equals(task.getId());
            }).collect(Collectors.toList());
            if (!runningTasks.isEmpty()) {
                throw new AcceptedException(CoreResultCode.LONG_RUNNING_TASK_ACCEPTED, ImmutableMap.of("taskId", getName()));
            }
        });
    }
}
Also used : Arrays(java.util.Arrays) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmEntityEventDto(eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) TransactionContext(eu.bcvsolutions.idm.core.api.domain.TransactionContext) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) ArrayList(java.util.ArrayList) AutowireHelper(eu.bcvsolutions.idm.core.api.utils.AutowireHelper) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Propagation(org.springframework.transaction.annotation.Propagation) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) Map(java.util.Map) DisallowConcurrentExecution(org.quartz.DisallowConcurrentExecution) TransactionContextHolder(eu.bcvsolutions.idm.core.api.domain.TransactionContextHolder) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) LongRunningTaskEventType(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent.LongRunningTaskEventType) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) BeanNameAware(org.springframework.beans.factory.BeanNameAware) IdmCheckConcurrentExecution(eu.bcvsolutions.idm.core.scheduler.api.domain.IdmCheckConcurrentExecution) EntityEventLock(eu.bcvsolutions.idm.core.api.event.EntityEventLock) ImmutableMap(com.google.common.collect.ImmutableMap) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) EnabledEvaluator(eu.bcvsolutions.idm.core.security.api.service.EnabledEvaluator) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ParameterConverter(eu.bcvsolutions.idm.core.api.utils.ParameterConverter) List(java.util.List) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) Optional(java.util.Optional) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ArrayList(java.util.ArrayList) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) IdmCheckConcurrentExecution(eu.bcvsolutions.idm.core.scheduler.api.domain.IdmCheckConcurrentExecution) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) UUID(java.util.UUID)

Example 2 with ConcurrentExecutionException

use of eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method execute.

@Override
@Transactional(noRollbackFor = AcceptedException.class)
public synchronized <V> LongRunningFutureTask<V> execute(LongRunningTaskExecutor<V> taskExecutor) {
    if (!isAsynchronous()) {
        V result = executeSync(taskExecutor);
        // construct simple "sync" task
        return new LongRunningFutureTask<>(taskExecutor, new FutureTask<V>(() -> {
            return result;
        }) {

            @Override
            public V get() {
                return result;
            }
        });
    }
    // 
    // autowire task properties
    AutowireHelper.autowire(taskExecutor);
    // persist LRT as running => prevent to scheduler process the created tasks
    IdmLongRunningTaskDto persistTask = resolveLongRunningTask(taskExecutor, null, OperationState.RUNNING);
    // 
    try {
        taskExecutor.validate(persistTask);
    } catch (ConcurrentExecutionException ex) {
        // task can be executed later, e.g. after previous task ends
        markTaskAsCreated(persistTask);
        // 
        throw ex;
    } catch (ResultCodeException ex) {
        // task can be executed later, e.g. after previous task ends
        if (ex.getStatus() == HttpStatus.ACCEPTED) {
            markTaskAsCreated(persistTask);
        } else {
            // mark task as failed => prevent to stuck task type execution from queue
            persistTask.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setException(ex).build());
            service.save(persistTask);
        }
        // 
        throw ex;
    }
    // 
    LongRunningFutureTask<V> longRunnigFutureTask = new LongRunningFutureTask<>(taskExecutor, new FutureTask<>(taskExecutor));
    // execute - after original transaction is commited
    entityEventManager.publishEvent(longRunnigFutureTask);
    // 
    return longRunnigFutureTask;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ConcurrentExecutionException(eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)2 ConcurrentExecutionException (eu.bcvsolutions.idm.core.scheduler.api.exception.ConcurrentExecutionException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)1 TransactionContext (eu.bcvsolutions.idm.core.api.domain.TransactionContext)1 TransactionContextHolder (eu.bcvsolutions.idm.core.api.domain.TransactionContextHolder)1 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)1 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)1 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)1 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)1 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)1 EntityEventLock (eu.bcvsolutions.idm.core.api.event.EntityEventLock)1 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)1 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)1 EntityEventManager (eu.bcvsolutions.idm.core.api.service.EntityEventManager)1 LookupService (eu.bcvsolutions.idm.core.api.service.LookupService)1 AutowireHelper (eu.bcvsolutions.idm.core.api.utils.AutowireHelper)1