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;
}
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);
}
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);
}
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;
}
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()));
}
}
}
Aggregations