Search in sources :

Example 31 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method interrupt.

@Override
@Transactional
public boolean interrupt(UUID longRunningTaskId) {
    Assert.notNull(longRunningTaskId);
    IdmLongRunningTaskDto task = service.get(longRunningTaskId);
    Assert.notNull(longRunningTaskId);
    String instanceId = configurationService.getInstanceId();
    if (!task.getInstanceId().equals(instanceId)) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_DIFFERENT_INSTANCE, ImmutableMap.of("taskId", longRunningTaskId, "taskInstanceId", task.getInstanceId(), "currentInstanceId", instanceId));
    }
    if (OperationState.RUNNING != task.getResult().getState()) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_NOT_RUNNING, ImmutableMap.of("taskId", longRunningTaskId, "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    }
    // 
    // interrupt thread
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    for (Thread thread : threadSet) {
        if (thread.getId() == task.getThreadId()) {
            ResultModel resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_INTERRUPT, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
            Exception ex = null;
            try {
                thread.interrupt();
            } catch (Exception e) {
                ex = e;
                LOG.error(resultModel.toString(), e);
            }
            task.setRunning(false);
            // 
            if (ex == null) {
                LOG.info("Long running task with id: [{}], was interrupted.", task.getId());
                task.setResult(new OperationResult.Builder(OperationState.CANCELED).setModel(resultModel).build());
            } else {
                LOG.info("Long running task with id: [{}], has some exception during interrupt.", task.getId());
                task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
            }
            service.save(task);
            return true;
        }
    }
    LOG.warn("For long running task with id: [{}], has not found running thread.", task.getId());
    return false;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 32 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method cancel.

@Override
@Transactional
public void cancel(UUID longRunningTaskId) {
    Assert.notNull(longRunningTaskId);
    IdmLongRunningTaskDto task = service.get(longRunningTaskId);
    Assert.notNull(longRunningTaskId);
    // 
    if (!OperationState.isRunnable(task.getResult().getState())) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_NOT_RUNNING, ImmutableMap.of("taskId", longRunningTaskId, "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
    }
    // 
    task.setResult(new OperationResult.Builder(OperationState.CANCELED).build());
    LOG.info("Long running task with id: [{}] was canceled.", task.getId());
    // running to false will be set by task himself
    service.save(task);
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 33 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method processCreated.

@Override
@Transactional
public LongRunningFutureTask<?> processCreated(UUID longRunningTaskId) {
    LOG.debug("Processing created task [{}] from long running task queue", longRunningTaskId);
    IdmLongRunningTaskDto task = service.get(longRunningTaskId);
    // task cannot be started twice
    if (task.isRunning() || OperationState.RUNNING == task.getResultState()) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_RUNNING, ImmutableMap.of("taskId", task.getId()));
    }
    if (OperationState.CREATED != task.getResultState()) {
        throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_PROCESSED, ImmutableMap.of("taskId", task.getId()));
    }
    // 
    LongRunningTaskExecutor<?> taskExecutor = createTaskExecutor(task);
    if (taskExecutor == null) {
        return null;
    }
    return execute(taskExecutor);
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManager method persistTask.

/**
 * Prepares executor's LRT
 *
 * @param taskExecutor
 * @return
 */
private IdmLongRunningTaskDto persistTask(LongRunningTaskExecutor<?> taskExecutor) {
    // prepare task
    IdmLongRunningTaskDto task;
    if (taskExecutor.getLongRunningTaskId() == null) {
        task = new IdmLongRunningTaskDto();
        task.setTaskType(taskExecutor.getName());
        task.setTaskProperties(taskExecutor.getProperties());
        task.setTaskDescription(taskExecutor.getDescription());
        task.setInstanceId(configurationService.getInstanceId());
        task.setResult(new OperationResult.Builder(OperationState.CREATED).build());
        task = service.save(task);
        taskExecutor.setLongRunningTaskId(task.getId());
    } else {
        task = service.get(taskExecutor.getLongRunningTaskId());
    }
    return task;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)

Example 35 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class RemoveAutomaticRoleTaskExecutor method validate.

/**
 * Automatic role removal can be start, if previously LRT ended.
 */
@Override
public void validate(IdmLongRunningTaskDto task) {
    super.validate(task);
    // 
    AbstractIdmAutomaticRoleDto automaticRole = roleTreeNodeService.get(getAutomaticRoleId());
    if (automaticRole == null) {
        // get from automatic role attribute service
        automaticRole = automaticRoleAttributeService.get(getAutomaticRoleId());
    }
    // 
    IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
    filter.setTaskType(this.getClass().getCanonicalName());
    filter.setRunning(Boolean.TRUE);
    // 
    for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
        if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_REMOVE_TASK_RUN_CONCURRENTLY, ImmutableMap.of("roleTreeNode", automaticRole.getId().toString(), "taskId", longRunningTask.getId().toString()));
        }
    }
    // 
    filter.setTaskType(AddNewAutomaticRoleTaskExecutor.class.getCanonicalName());
    for (IdmLongRunningTaskDto longRunningTask : getLongRunningTaskService().find(filter, null)) {
        if (longRunningTask.getTaskProperties().get(AbstractAutomaticRoleTaskExecutor.PARAMETER_ROLE_TREE_NODE).equals(automaticRole.getId())) {
            throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_REMOVE_TASK_ADD_RUNNING, ImmutableMap.of("roleTreeNode", automaticRole.getId().toString(), "taskId", longRunningTask.getId().toString()));
        }
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractIdmAutomaticRoleDto(eu.bcvsolutions.idm.core.api.dto.AbstractIdmAutomaticRoleDto) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)

Aggregations

IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)56 Test (org.junit.Test)32 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)29 IdmScheduledTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto)14 IdmProcessedTaskItemDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto)13 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)13 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)11 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)9 Transactional (org.springframework.transaction.annotation.Transactional)7 LocalDate (org.joda.time.LocalDate)6 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)5 IdmPasswordDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto)4 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)4 UUID (java.util.UUID)4 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)3 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)3 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)3 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)3 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)3