use of org.apache.syncope.common.lib.types.PropagationTaskExecStatus in project syncope by apache.
the class PriorityPropagationTaskExecutor method doExecute.
@Override
protected void doExecute(final Collection<PropagationTaskTO> tasks, final PropagationReporter reporter, final boolean nullPriorityAsync) {
Map<PropagationTaskTO, ExternalResource> taskToResource = tasks.stream().collect(Collectors.toMap(Function.identity(), task -> resourceDAO.find(task.getResource())));
List<PropagationTaskTO> prioritizedTasks = tasks.stream().filter(task -> taskToResource.get(task).getPropagationPriority() != null).collect(Collectors.toList());
Collections.sort(prioritizedTasks, new PriorityComparator(taskToResource));
LOG.debug("Propagation tasks sorted by priority, for serial execution: {}", prioritizedTasks);
Collection<PropagationTaskTO> concurrentTasks = tasks.stream().filter(task -> !prioritizedTasks.contains(task)).collect(Collectors.toSet());
LOG.debug("Propagation tasks for concurrent execution: {}", concurrentTasks);
// first process priority resources sequentially and fail as soon as any propagation failure is reported
prioritizedTasks.forEach(task -> {
TaskExec execution = null;
PropagationTaskExecStatus execStatus;
try {
execution = newPropagationTaskCallable(task, reporter).call();
execStatus = PropagationTaskExecStatus.valueOf(execution.getStatus());
} catch (Exception e) {
LOG.error("Unexpected exception", e);
execStatus = PropagationTaskExecStatus.FAILURE;
}
if (execStatus != PropagationTaskExecStatus.SUCCESS) {
throw new PropagationException(task.getResource(), execution == null ? null : execution.getMessage());
}
});
// then process non-priority resources concurrently...
CompletionService<TaskExec> completionService = new ExecutorCompletionService<>(executor);
Map<PropagationTaskTO, Future<TaskExec>> nullPriority = new HashMap<>(concurrentTasks.size());
concurrentTasks.forEach(task -> {
try {
nullPriority.put(task, completionService.submit(newPropagationTaskCallable(task, reporter)));
} catch (Exception e) {
LOG.error("Unexpected exception", e);
}
});
// ...waiting for all callables to complete, if async processing was not required
if (!nullPriority.isEmpty()) {
if (nullPriorityAsync) {
nullPriority.forEach((task, exec) -> {
reporter.onSuccessOrNonPriorityResourceFailures(task, PropagationTaskExecStatus.CREATED, null, null, null);
});
} else {
final Set<Future<TaskExec>> nullPriorityFutures = new HashSet<>(nullPriority.values());
try {
executor.submit(() -> {
while (!nullPriorityFutures.isEmpty()) {
try {
nullPriorityFutures.remove(completionService.take());
} catch (Exception e) {
LOG.error("Unexpected exception", e);
}
}
}).get(60, TimeUnit.SECONDS);
} catch (Exception e) {
LOG.error("Unexpected exception", e);
} finally {
nullPriorityFutures.forEach(future -> {
future.cancel(true);
});
nullPriorityFutures.clear();
nullPriority.clear();
}
}
}
}
Aggregations