Search in sources :

Example 41 with IdmLongRunningTaskDto

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);
        }
    });
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) SysSyncLogFilter(eu.bcvsolutions.idm.acc.dto.filter.SysSyncLogFilter) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with IdmLongRunningTaskDto

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());
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 43 with IdmLongRunningTaskDto

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));
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmProcessedTaskItemFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Example 44 with IdmLongRunningTaskDto

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()));
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 45 with IdmLongRunningTaskDto

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));
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmProcessedTaskItemFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

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