Search in sources :

Example 1 with LongRunningTaskEvent

use of eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent in project CzechIdMng by bcvsolutions.

the class AbstractLongRunningTaskExecutor method end.

/**
 * TODO: save result into long running task - blob, text?
 *
 * @param result
 * @param ex
 * @return
 */
protected V end(V result, Exception ex) {
    Assert.notNull(longRunningTaskId);
    IdmLongRunningTaskDto task = longRunningTaskService.get(longRunningTaskId);
    Assert.notNull(task, "Long running task has to be prepared before task is ended");
    LOG.debug("Long running task ends [{}] with result [{}].", longRunningTaskId, result);
    // 
    setStateProperties(task);
    // 
    if (ex != null) {
        ResultModel resultModel;
        if (ex instanceof ResultCodeException) {
            resultModel = ((ResultCodeException) ex).getError().getError();
        } else {
            resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_FAILED, ImmutableMap.of("taskId", longRunningTaskId, "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
        }
        LOG.error(resultModel.toString(), ex);
        task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
    } else if (OperationState.isRunnable(task.getResultState())) {
        // executed standardly
        LOG.debug("Long running task ended [{}] standardly, previous state [{}], result [{}].", longRunningTaskId, task.getResultState(), result);
        task.setResult(new OperationResult.Builder(OperationState.EXECUTED).build());
    }
    // 
    // publish event - LRT ended
    // TODO: result is not persisted - propagate him in event?
    entityEventManager.publishEvent(new LongRunningTaskEvent(LongRunningTaskEventType.END, task));
    // 
    return result;
}
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) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent)

Example 2 with LongRunningTaskEvent

use of eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskNoExecutionAfterInitiatorFails.

@Test
public void testDependentTaskNoExecutionAfterInitiatorFails() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        LongRunningTaskExecuteDependentProcessor processor = context.getBean(LongRunningTaskExecuteDependentProcessor.class);
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createTask("dependent-task-initiator");
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId());
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        IdmLongRunningTaskDto lrt = createdLrts.get(0);
        lrt.setResult(new OperationResult(OperationState.EXCEPTION));
        // not executed
        EventResult<IdmLongRunningTaskDto> result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.BLOCKED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.NOT_EXECUTED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CANCELED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CREATED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.RUNNING));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        // 
        longRunningTaskManager.cancel(lrt.getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) LongRunningTaskExecuteDependentProcessor(eu.bcvsolutions.idm.core.scheduler.event.processor.LongRunningTaskExecuteDependentProcessor) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)2 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)2 LongRunningTaskEvent (eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent)2 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)1 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)1 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)1 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)1 LongRunningTaskExecuteDependentProcessor (eu.bcvsolutions.idm.core.scheduler.event.processor.LongRunningTaskExecuteDependentProcessor)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 FutureTask (java.util.concurrent.FutureTask)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1