use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method executeEvent.
@Override
@Transactional
public void executeEvent(IdmEntityEventDto event) {
Assert.notNull(event);
Assert.notNull(event.getOwnerId());
if (!eventConfiguration.isAsynchronous()) {
// synchronous processing
// we don't persist events and their states
process(new CoreEvent<>(EntityEventType.EXECUTE, event));
return;
}
if (event.getPriority() == PriorityType.IMMEDIATE) {
// synchronous processing
// we don't persist events and their states
// TODO: what about running event with the same owner? And events in queue for the same owner
process(new CoreEvent<>(EntityEventType.EXECUTE, event));
return;
}
//
if (runningOwnerEvents.putIfAbsent(event.getOwnerId(), event.getId()) != null) {
LOG.debug("Previous event [{}] for owner with id [{}] is currently processed.", runningOwnerEvents.get(event.getOwnerId()), event.getOwnerId());
// event will be processed in another scheduling
return;
}
// check super owner is not processed
UUID superOwnerId = event.getProperties().getUuid(EntityEventManager.EVENT_PROPERTY_SUPER_OWNER_ID);
if (superOwnerId != null && !superOwnerId.equals(event.getOwnerId())) {
if (runningOwnerEvents.putIfAbsent(superOwnerId, event.getId()) != null) {
LOG.debug("Previous event [{}] for super owner with id [{}] is currently processed.", runningOwnerEvents.get(superOwnerId), superOwnerId);
runningOwnerEvents.remove(event.getOwnerId());
// event will be processed in another scheduling
return;
}
}
// execute event in new thread asynchronously
try {
eventConfiguration.getExecutor().execute(new Runnable() {
@Override
public void run() {
try {
process(new CoreEvent<>(EntityEventType.EXECUTE, event));
} catch (Exception ex) {
// exception handling only ... all processor should persist their own entity state (see AbstractEntityEventProcessor)
ResultModel resultModel;
if (ex instanceof ResultCodeException) {
resultModel = ((ResultCodeException) ex).getError().getError();
} else {
resultModel = new DefaultResultModel(CoreResultCode.EVENT_EXECUTE_FAILED, ImmutableMap.of("eventId", event.getId(), "eventType", String.valueOf(event.getEventType()), "ownerId", String.valueOf(event.getOwnerId()), "instanceId", String.valueOf(event.getInstanceId())));
}
context.getBean(DefaultEntityEventManager.this.getClass()).saveResult(event.getId(), new OperationResultDto.Builder(OperationState.EXCEPTION).setCause(ex).setModel(resultModel).build());
LOG.error(resultModel.toString(), ex);
} finally {
LOG.trace("Event [{}] ends for owner with id [{}].", event.getId(), event.getOwnerId());
removeRunningEvent(event);
}
}
});
//
LOG.trace("Running event [{}] for owner with id [{}].", event.getId(), event.getOwnerId());
} catch (RejectedExecutionException ex) {
// thread pool is full - wait for another try
// TODO: Thread.wait(300) ?
removeRunningEvent(event);
}
}
use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.
the class DefaultLongRunningTaskManager method createTaskExecutor.
/**
* Create new LongRunningTaskExecutor from given LRT.
* Handles exceptions, when task already processed, task type is removed or task initialization failed
*
* @param task
* @return
*/
private LongRunningTaskExecutor<?> createTaskExecutor(IdmLongRunningTaskDto task) {
Assert.notNull(task, "Long running task instance is required!");
if (!OperationState.isRunnable(task.getResultState())) {
throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_IS_PROCESSED, ImmutableMap.of("taskId", task.getId()));
}
//
LongRunningTaskExecutor<?> taskExecutor = null;
ResultModel resultModel = null;
Exception ex = null;
try {
taskExecutor = (LongRunningTaskExecutor<?>) AutowireHelper.createBean(Class.forName(task.getTaskType()));
taskExecutor.setLongRunningTaskId(task.getId());
taskExecutor.init((Map<String, Object>) task.getTaskProperties());
} catch (ClassNotFoundException e) {
ex = e;
resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_NOT_FOUND, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
} catch (Exception e) {
ex = e;
resultModel = new DefaultResultModel(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", task.getId(), "taskType", task.getTaskType(), "instanceId", task.getInstanceId()));
}
if (ex != null) {
LOG.error(resultModel.toString(), ex);
task.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setModel(resultModel).setCause(ex).build());
service.save(task);
return null;
} else {
return taskExecutor;
}
}
use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.
the class DisabledSystemProcessor method process.
@Override
public EventResult<SysProvisioningOperationDto> process(EntityEvent<SysProvisioningOperationDto> event) {
SysProvisioningOperationDto provisioningOperation = event.getContent();
SysSystemDto system = systemService.get(provisioningOperation.getSystem());
String uid = provisioningOperationService.getByProvisioningOperation(provisioningOperation).getUid();
boolean closed = false;
if (system.isDisabled()) {
ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_SYSTEM_DISABLED, ImmutableMap.of("name", uid, "system", system.getName()));
provisioningOperation.setResult(new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build());
//
provisioningOperation = provisioningOperationService.save(provisioningOperation);
//
LOG.info(resultModel.toString());
notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
//
closed = true;
}
// set back to event content
event.setContent(provisioningOperation);
return new DefaultEventResult<>(event, this, closed);
}
use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.
the class ReadonlySystemProcessor method process.
@Override
public EventResult<SysProvisioningOperationDto> process(EntityEvent<SysProvisioningOperationDto> event) {
SysProvisioningOperationDto provisioningOperation = event.getContent();
SysSystemDto system = systemService.get(provisioningOperation.getSystem());
boolean closed = false;
if (system.isReadonly()) {
String uid = provisioningOperationService.getByProvisioningOperation(provisioningOperation).getUid();
ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_SYSTEM_READONLY, ImmutableMap.of("name", uid, "system", system.getName()));
provisioningOperation.setResult(new OperationResult.Builder(OperationState.NOT_EXECUTED).setModel(resultModel).build());
//
provisioningOperation = provisioningOperationService.save(provisioningOperation);
//
LOG.info(resultModel.toString());
notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
//
closed = true;
}
// set back to event content
event.setContent(provisioningOperation);
return new DefaultEventResult<>(event, this, closed);
}
use of eu.bcvsolutions.idm.core.api.dto.ResultModel in project CzechIdMng by bcvsolutions.
the class DefaultSysProvisioningOperationService method handleFailed.
@Override
@Transactional
public SysProvisioningOperationDto handleFailed(SysProvisioningOperationDto operation, Exception ex) {
SysSystemDto system = systemService.get(operation.getSystem());
String uid = this.getByProvisioningOperation(operation).getUid();
ResultModel resultModel = new DefaultResultModel(AccResultCode.PROVISIONING_FAILED, ImmutableMap.of("name", uid, "system", system.getName(), "operationType", operation.getOperationType(), "objectClass", operation.getProvisioningContext().getConnectorObject().getObjectClass().getType()));
LOG.error(resultModel.toString(), ex);
//
operation.increaseAttempt();
// TODO: from configuration
operation.setMaxAttempts(6);
operation.setResult(new OperationResult.Builder(OperationState.EXCEPTION).setCode(resultModel.getStatusEnum()).setModel(resultModel).setCause(ex).build());
//
operation = save(operation);
//
// calculate next attempt
SysProvisioningOperationDto firstOperation = getFirstOperationByBatchId(operation.getBatch());
if (firstOperation.equals(operation)) {
SysProvisioningBatchDto batch = batchService.get(operation.getBatch());
batch.setNextAttempt(batchService.calculateNextAttempt(operation));
batch = batchService.save(batch);
}
//
if (securityService.getCurrentId() != null) {
// TODO: check account owner
notificationManager.send(AccModuleDescriptor.TOPIC_PROVISIONING, new IdmMessageDto.Builder().setModel(resultModel).build());
}
return operation;
}
Aggregations