use of org.apache.syncope.core.persistence.api.entity.task.TaskExec 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();
}
}
}
}
use of org.apache.syncope.core.persistence.api.entity.task.TaskExec in project syncope by apache.
the class TaskLogic method listExecutions.
@PreAuthorize("hasRole('" + StandardEntitlement.TASK_READ + "')")
@Override
public Pair<Integer, List<ExecTO>> listExecutions(final String key, final int page, final int size, final List<OrderByClause> orderByClauses) {
Task task = taskDAO.find(key);
if (task == null) {
throw new NotFoundException("Task " + key);
}
Integer count = taskExecDAO.count(key);
List<ExecTO> result = taskExecDAO.findAll(task, page, size, orderByClauses).stream().map(taskExec -> binder.getExecTO(taskExec)).collect(Collectors.toList());
return Pair.of(count, result);
}
use of org.apache.syncope.core.persistence.api.entity.task.TaskExec in project syncope by apache.
the class TaskExecTest method findLatestStarted.
@Test
public void findLatestStarted() {
PropagationTask task = taskDAO.find("1e697572-b896-484c-ae7f-0c8f63fcbc6c");
assertNotNull(task);
TaskExec latestStarted = taskExecDAO.findLatestStarted(task);
assertNotNull(latestStarted);
assertEquals("e58ca1c7-178a-4012-8a71-8aa14eaf0655", latestStarted.getKey());
}
use of org.apache.syncope.core.persistence.api.entity.task.TaskExec in project syncope by apache.
the class TaskTest method deleteTaskExecution.
@Test
public void deleteTaskExecution() {
TaskExec execution = taskExecDAO.find("e58ca1c7-178a-4012-8a71-8aa14eaf0655");
int executionNumber = execution.getTask().getExecs().size();
taskExecDAO.delete("e58ca1c7-178a-4012-8a71-8aa14eaf0655");
taskExecDAO.flush();
assertNull(taskExecDAO.find("e58ca1c7-178a-4012-8a71-8aa14eaf0655"));
PropagationTask task = taskDAO.find("1e697572-b896-484c-ae7f-0c8f63fcbc6c");
assertEquals(task.getExecs().size(), executionNumber - 1);
}
use of org.apache.syncope.core.persistence.api.entity.task.TaskExec in project syncope by apache.
the class DefaultNotificationJobDelegate method executeSingle.
@Transactional
@Override
public TaskExec executeSingle(final NotificationTask task) {
TaskExec execution = entityFactory.newEntity(TaskExec.class);
execution.setTask(task);
execution.setStart(new Date());
boolean retryPossible = true;
if (StringUtils.isBlank(task.getSubject()) || task.getRecipients().isEmpty() || StringUtils.isBlank(task.getHtmlBody()) || StringUtils.isBlank(task.getTextBody())) {
String message = "Could not fetch all required information for sending e-mails:\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + task.getTextBody();
LOG.error(message);
execution.setStatus(NotificationJob.Status.NOT_SENT.name());
retryPossible = false;
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(message);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("About to send e-mails:\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + task.getTextBody() + "\n");
}
status.set("Sending notifications to " + task.getRecipients());
for (String to : task.getRecipients()) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(task.getSender());
helper.setSubject(task.getSubject());
helper.setText(task.getTextBody(), task.getHtmlBody());
mailSender.send(message);
execution.setStatus(NotificationJob.Status.SENT.name());
StringBuilder report = new StringBuilder();
switch(task.getTraceLevel()) {
case ALL:
report.append("FROM: ").append(task.getSender()).append('\n').append("TO: ").append(to).append('\n').append("SUBJECT: ").append(task.getSubject()).append('\n').append('\n').append(task.getTextBody()).append('\n').append('\n').append(task.getHtmlBody()).append('\n');
break;
case SUMMARY:
report.append("E-mail sent to ").append(to).append('\n');
break;
case FAILURES:
case NONE:
default:
}
if (report.length() > 0) {
execution.setMessage(report.toString());
}
notificationManager.createTasks(AuditElements.EventCategoryType.TASK, "notification", null, "send", AuditElements.Result.SUCCESS, null, null, task, "Successfully sent notification to " + to);
} catch (Exception e) {
LOG.error("Could not send e-mail", e);
execution.setStatus(NotificationJob.Status.NOT_SENT.name());
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(ExceptionUtils2.getFullStackTrace(e));
}
notificationManager.createTasks(AuditElements.EventCategoryType.TASK, "notification", null, "send", AuditElements.Result.FAILURE, null, null, task, "Could not send notification to " + to, e);
}
execution.setEnd(new Date());
}
}
if (hasToBeRegistered(execution)) {
execution = notificationManager.storeExec(execution);
if (retryPossible && (NotificationJob.Status.valueOf(execution.getStatus()) == NotificationJob.Status.NOT_SENT)) {
handleRetries(execution);
}
} else {
notificationManager.setTaskExecuted(execution.getTask().getKey(), true);
}
return execution;
}
Aggregations