use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.
the class DefaultSynchronizationService method init.
/**
* Cancel all previously ran synchronizations
*/
@Override
@Transactional
public void init() {
String instanceId = configurationService.getInstanceId();
LOG.info("Cancel unprocessed synchronizations - tasks was interrupt during instance [{}] restart", instanceId);
//
// find all running sync on all instances
IdmLongRunningTaskFilter lrtFilter = new IdmLongRunningTaskFilter();
lrtFilter.setRunning(Boolean.TRUE);
lrtFilter.setTaskType(this.getName());
List<IdmLongRunningTaskDto> allRunningSynchronizations = longRunningTaskService.find(lrtFilter, null).getContent();
// stop logs on the same instance id
SysSyncLogFilter logFilter = new SysSyncLogFilter();
logFilter.setRunning(Boolean.TRUE);
synchronizationLogService.find(logFilter, null).forEach(sync -> {
boolean runningOnOtherInstance = allRunningSynchronizations.stream().anyMatch(lrt -> {
return !lrt.getInstanceId().equals(instanceId) && sync.getSynchronizationConfig().equals(lrt.getTaskProperties().get(PARAMETER_SYNCHRONIZATION_ID));
});
if (!runningOnOtherInstance) {
String message = MessageFormat.format("Cancel unprocessed synchronization [{0}] - tasks was interrupt during instance [{1}] restart", sync.getId(), instanceId);
LOG.info(message);
sync.addToLog(message);
sync.setRunning(false);
synchronizationLogService.save(sync);
}
});
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.
the class AbstractSchedulableStatefulExecutorIntegrationTest method testExecute.
/**
* Tests the stateful execution method of the task by the following
* scenario:
* 1. find all identities to process
* 2. process retrieved identities
* 3. persist processing log
* 4. save processed identities into processed queue
*
* Second execution run:
* 1. find all identities to process - same as first run
* 2. call process retrieved identities - all have been processed already
* 3. check processing log - nothing new was processed, therefore must be empty
* 4. check processed items queue did not change
*
* Third run:
* 1. find all identities to process - returns empty list
* 2. check processing log - nothing was processed
* 3. check processed items queue - must be empty
* @throws Exception
*/
@Test
public void testExecute() throws Exception {
// manually prepare control entities - normally scheduler will take care of it itself
IdmScheduledTaskDto scheduledTask = createIdmScheduledTask(UUID.randomUUID().toString());
IdmLongRunningTaskDto longRunningTask = createIdmLongRunningTask(scheduledTask, TestIdenityIntegrationExecutor.class);
executor.setLongRunningTaskId(longRunningTask.getId());
// first run
List<IdmIdentityDto> itemsToProcess = getTestIdentities();
// set executor data
executor.dtos = itemsToProcess;
//
Boolean result = executor.process();
Page<IdmProcessedTaskItemDto> queueItems = itemService.findQueueItems(scheduledTask, null);
Page<IdmProcessedTaskItemDto> logItems = itemService.findLogItems(longRunningTask, null);
//
assertTrue(result);
assertEquals(longRunningTask.getScheduledTask(), scheduledTask.getId());
assertEquals(itemsToProcess.size(), queueItems.getTotalElements());
assertEquals(itemsToProcess.size(), logItems.getTotalElements());
assertEquals(Long.valueOf(itemsToProcess.size()), executor.getCount());
assertEquals(Long.valueOf(itemsToProcess.size()), executor.getCounter());
SchedulerTestUtils.checkLogItems(longRunningTask, IdmIdentityDto.class, logItems);
SchedulerTestUtils.checkQueueItems(scheduledTask, IdmIdentityDto.class, queueItems);
//
// second run
//
longRunningTask = createIdmLongRunningTask(scheduledTask, TestIdenityIntegrationExecutor.class);
executor.setLongRunningTaskId(longRunningTask.getId());
executor.dtos = itemsToProcess;
//
result = executor.process();
queueItems = itemService.findQueueItems(scheduledTask, null);
logItems = itemService.findLogItems(longRunningTask, null);
//
assertTrue(result);
assertEquals(itemsToProcess.size(), queueItems.getTotalElements());
assertEquals(0, logItems.getTotalElements());
assertEquals(Long.valueOf(0), executor.getCount());
assertEquals(Long.valueOf(0), executor.getCounter());
SchedulerTestUtils.checkQueueItems(scheduledTask, IdmIdentityDto.class, queueItems);
//
// third run
//
longRunningTask = createIdmLongRunningTask(scheduledTask, TestIdenityIntegrationExecutor.class);
executor.setLongRunningTaskId(longRunningTask.getId());
//
result = executor.process();
queueItems = itemService.findQueueItems(scheduledTask, null);
logItems = itemService.findLogItems(longRunningTask, null);
//
assertTrue(result);
assertEquals(0, queueItems.getTotalElements());
assertEquals(0, logItems.getTotalElements());
assertEquals(Long.valueOf(0), executor.getCount());
assertEquals(Long.valueOf(0), executor.getCounter());
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.
the class AbstractSchedulableStatefulExecutorUnitTest method testProcess.
@Test
public void testProcess() {
IdmScheduledTaskDto scheduledTask = new IdmScheduledTaskDto();
scheduledTask.setId(UUID.randomUUID());
IdmLongRunningTaskDto lrt = new IdmLongRunningTaskDto();
//
IdmIdentityDto dto1 = getTestIdentityDto();
IdmIdentityDto dto2 = getTestIdentityDto();
IdmIdentityDto dto3 = getTestIdentityDto();
// stubs
doReturn(Optional.of(new OperationResult.Builder(OperationState.EXECUTED).build())).when(executor).processItem(any(IdmIdentityDto.class));
doReturn(false).when(executor).isInProcessedQueue(any(IdmIdentityDto.class));
doNothing().when(itemService).deleteInternal(any(IdmProcessedTaskItemDto.class));
doReturn(Lists.newArrayList(UUID.randomUUID(), UUID.randomUUID())).when(executor).getProcessedItemRefsFromQueue();
doReturn(scheduledTask.getId()).when(executor).getScheduledTaskId();
// matchers
when(longRunningTaskService.get(any(UUID.class))).thenReturn(lrt);
when(scheduledTaskService.get(any(UUID.class))).thenReturn(scheduledTask);
when(executor.getItemsToProcess(any(Pageable.class))).thenReturn(new PageImpl<>(Lists.newArrayList(dto1, dto2), new PageRequest(0, 2), 6)).thenReturn(new PageImpl<>(Lists.newArrayList(dto3), new PageRequest(0, 2), 6)).thenReturn(new PageImpl<>(Lists.newArrayList()));
when(itemService.saveInternal(any(IdmProcessedTaskItemDto.class))).then(AdditionalAnswers.returnsFirstArg());
when(itemService.find(any(IdmProcessedTaskItemFilter.class), any(Pageable.class))).thenReturn(new PageImpl<>(Lists.newArrayList(new IdmProcessedTaskItemDto())));
//
//
Boolean processingResult = executor.process();
assertTrue(processingResult);
//
//
verify(scheduledTaskService, times(3)).get(any(UUID.class));
verify(longRunningTaskService, times(6)).get(any(UUID.class));
//
verify(executor, times(3)).getItemsToProcess(any(Pageable.class));
verify(executor, times(1)).isInProcessedQueue(dto1);
verify(executor, times(1)).isInProcessedQueue(dto2);
verify(executor, times(1)).isInProcessedQueue(dto3);
verify(executor, times(1)).getProcessedItemRefsFromQueue();
verify(executor, times(2)).removeFromProcessedQueue(any(UUID.class));
verify(executor, times(3)).addToProcessedQueue(any(IdmIdentityDto.class), any(OperationResult.class));
verify(executor, times(3)).logItemProcessed(any(IdmIdentityDto.class), any(OperationResult.class));
// 6x addToProcessQueue, 2x removeItemFromQueue, 2x stubbed
verify(executor, times(10)).getScheduledTaskId();
verify(executor, times(3)).processItem(any(IdmIdentityDto.class));
// 3x addToProcessQueue, 3x logItemProcessed
verify(itemService, times(2)).deleteInternal(any(IdmProcessedTaskItemDto.class));
verify(itemService, times(3)).createLogItem(any(AbstractDto.class), any(OperationResult.class), any(IdmLongRunningTaskDto.class));
verify(itemService, times(3)).createQueueItem(any(AbstractDto.class), any(OperationResult.class), any(IdmScheduledTaskDto.class));
// 2x from removeFromProcessedQueue, other invocations are stubbed
verify(itemService, times(2)).find(any(IdmProcessedTaskItemFilter.class), any(Pageable.class));
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmProcessedTaskItemDtoServiceTest method testDeleteLogItemIntegrity.
@Test
public void testDeleteLogItemIntegrity() {
IdmScheduledTaskDto d = helper.createSchedulableTask();
IdmLongRunningTaskDto lrt = this.createLongRunningTask(d);
IdmProcessedTaskItemDto item = service.saveInternal(helper.prepareProcessedItem(lrt));
//
longrunningService.deleteInternal(lrt);
//
assertNull(longrunningService.get(lrt.getId()));
assertNull(service.get(item.getId()));
assertNotNull(scheduledTaskService.get(d.getId()));
}
use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmProcessedTaskItemDtoServiceTest method getLongRunningTaskIdFilter.
@Test
public void getLongRunningTaskIdFilter() {
IdmScheduledTaskDto d = helper.createSchedulableTask();
IdmLongRunningTaskDto lrt = this.createLongRunningTask(d);
IdmLongRunningTaskDto lrt2 = this.createLongRunningTask(d);
//
IdmProcessedTaskItemDto item = service.saveInternal(helper.prepareProcessedItem(lrt));
IdmProcessedTaskItemDto item2 = service.saveInternal(helper.prepareProcessedItem(lrt));
IdmProcessedTaskItemDto item3 = service.saveInternal(helper.prepareProcessedItem(lrt2));
//
IdmProcessedTaskItemFilter filter = new IdmProcessedTaskItemFilter();
filter.setLongRunningTaskId(lrt.getId());
Page<IdmProcessedTaskItemDto> result = service.find(filter, null);
assertEquals("Wrong number of items!", 2, result.getTotalElements());
assertTrue(result.getContent().contains(item));
assertTrue(result.getContent().contains(item2));
assertFalse(result.getContent().contains(item3));
}
Aggregations