use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManager method init.
/**
* Cancel all previously ran tasks
*/
@Override
@Transactional
public void init() {
LOG.info("Cancel unprocessed long running task - tasks was interrupt during instance restart");
//
// task prepared for run - they can be in running state, but process physically doesn't started yet (running flag is still set to false)
String instanceId = configurationService.getInstanceId();
service.findAllByInstance(instanceId, OperationState.RUNNING).forEach(this::cancelTaskByRestart);
//
// running tasks - they can be marked as canceled, but they were not killed before server was restarted
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setInstanceId(instanceId);
filter.setRunning(Boolean.TRUE);
service.find(filter, null).forEach(this::cancelTaskByRestart);
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManager method switchInstanceId.
@Override
@Transactional
public int switchInstanceId(String previousInstanceId, String newInstanceId) {
Assert.hasLength(previousInstanceId, "Previous asynchronous instance is required.");
boolean stopProcessing = configurationService.getBooleanValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_STOP_PROCESSING, false);
//
try {
String asynchronousInstanceId = configurationService.getInstanceId();
// resolve default
if (StringUtils.isEmpty(newInstanceId)) {
newInstanceId = asynchronousInstanceId;
}
if (previousInstanceId.equals(newInstanceId)) {
LOG.info("Previous instance is same as newly used for asynchronous task processing [{}].", newInstanceId);
// return currently used
return 0;
}
// stop asynchronous task processing
configurationService.setBooleanValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_STOP_PROCESSING, true);
// find all created tasks with old instance and move them to new instance
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setInstanceId(previousInstanceId);
filter.setOperationState(OperationState.CREATED);
List<IdmLongRunningTaskDto> tasks = findLongRunningTasks(filter, null).getContent();
for (IdmLongRunningTaskDto task : tasks) {
task.setInstanceId(newInstanceId);
Map<String, Object> taskProperties = task.getTaskProperties();
if (taskProperties.containsKey(LongRunningTaskExecutor.PARAMETER_INSTANCE_ID)) {
taskProperties.put(LongRunningTaskExecutor.PARAMETER_INSTANCE_ID, newInstanceId);
}
saveLongRunningTask(task);
}
//
return tasks.size();
} finally {
// start asynchronous task processing
configurationService.setBooleanValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_STOP_PROCESSING, stopProcessing);
}
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleAttributeServiceIntegrationTest method testRemoveLastRuleRecount.
@Test
public void testRemoveLastRuleRecount() {
// start transaction
TransactionContextHolder.setContext(TransactionContextHolder.createEmptyContext());
UUID transactionId = TransactionContextHolder.getContext().getTransactionId();
//
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmRoleDto role = getHelper().createRole();
//
IdmAutomaticRoleAttributeDto automaticRole = getHelper().createAutomaticRole(role.getId());
//
IdmAutomaticRoleAttributeRuleDto automaticRoleRule = getHelper().createAutomaticRoleRule(automaticRole.getId(), AutomaticRoleAttributeRuleComparison.EQUALS, AutomaticRoleAttributeRuleType.IDENTITY, IdmIdentity_.username.getName(), null, identity.getUsername());
this.recalculateSync(automaticRole.getId());
//
List<IdmIdentityRoleDto> identityRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, identityRoles.size());
Assert.assertEquals(transactionId, identityRoles.get(0).getTransactionId());
//
automaticRoleAttributeRuleService.delete(automaticRoleRule);
identityRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(identityRoles.isEmpty());
//
this.recalculateSync(automaticRole.getId());
identityRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(identityRoles.isEmpty());
//
// check all LRT ended successfully
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setTransactionId(transactionId);
List<IdmLongRunningTaskDto> lrts = longRunningTaskManager.findLongRunningTasks(filter, null).getContent();
Assert.assertFalse(lrts.isEmpty());
Assert.assertTrue(lrts.stream().allMatch(lrt -> lrt.getResultState() == OperationState.EXECUTED));
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter in project CzechIdMng by bcvsolutions.
the class DeleteLongRunningTaskExecutorIntegrationTest method testDeleteOldLongRunningTasks.
@Test
public void testDeleteOldLongRunningTasks() {
// prepare provisioning operations
ZonedDateTime createdOne = ZonedDateTime.now().minusDays(2);
UUID ownerId = UUID.randomUUID();
IdmLongRunningTaskDto operationOne = createDto(ownerId, createdOne, OperationState.EXECUTED);
// all other variants for not removal
createDto(ownerId, LocalDate.now().atStartOfDay(ZoneId.systemDefault()).plusMinutes(1), OperationState.EXECUTED);
createDto(ownerId, LocalDate.now().atStartOfDay(ZoneId.systemDefault()).plusMinutes(1), OperationState.CREATED);
createDto(ownerId, LocalDate.now().atStartOfDay(ZoneId.systemDefault()).plusMinutes(1), OperationState.EXECUTED);
createDto(ownerId, ZonedDateTime.now().minusDays(2), OperationState.EXCEPTION);
createDto(ownerId, LocalDate.now().atStartOfDay(ZoneId.systemDefault()).minusHours(22), OperationState.EXECUTED);
//
Assert.assertEquals(createdOne, operationOne.getCreated());
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setInstanceId(ownerId.toString());
List<IdmLongRunningTaskDto> lrts = service.find(filter, null).getContent();
Assert.assertEquals(6, lrts.size());
//
DeleteLongRunningTaskExecutor taskExecutor = new DeleteLongRunningTaskExecutor();
Map<String, Object> properties = new HashMap<>();
properties.put(DeleteLongRunningTaskExecutor.PARAMETER_NUMBER_OF_DAYS, 1);
properties.put(DeleteLongRunningTaskExecutor.PARAMETER_OPERATION_STATE, OperationState.EXECUTED);
AutowireHelper.autowire(taskExecutor);
taskExecutor.init(properties);
//
longRunningTaskManager.execute(taskExecutor);
//
lrts = service.find(filter, null).getContent();
Assert.assertEquals(5, lrts.size());
Assert.assertTrue(lrts.stream().allMatch(a -> !a.getId().equals(operationOne.getId())));
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter 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()));
}
});
}
}
Aggregations