Search in sources :

Example 6 with TaskExec

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();
            }
        }
    }
}
Also used : PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) HashMap(java.util.HashMap) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Function(java.util.function.Function) PropagationTaskCallable(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskCallable) CompletionService(java.util.concurrent.CompletionService) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) Map(java.util.Map) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Collection(java.util.Collection) Resource(javax.annotation.Resource) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) Set(java.util.Set) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) Comparator(java.util.Comparator) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Collections(java.util.Collections) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) HashMap(java.util.HashMap) PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

Example 7 with TaskExec

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);
}
Also used : StandardEntitlement(org.apache.syncope.common.lib.types.StandardEntitlement) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) TaskTO(org.apache.syncope.common.lib.to.TaskTO) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayUtils(org.apache.commons.lang3.ArrayUtils) JobKey(org.quartz.JobKey) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) SchedTaskTO(org.apache.syncope.common.lib.to.SchedTaskTO) Pair(org.apache.commons.lang3.tuple.Pair) SchedulerException(org.quartz.SchedulerException) Map(java.util.Map) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) NotificationJobDelegate(org.apache.syncope.core.provisioning.api.notification.NotificationJobDelegate) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) JobTO(org.apache.syncope.common.lib.to.JobTO) TaskExecDAO(org.apache.syncope.core.persistence.api.dao.TaskExecDAO) Method(java.lang.reflect.Method) Triple(org.apache.commons.lang3.tuple.Triple) ExecTO(org.apache.syncope.common.lib.to.ExecTO) Task(org.apache.syncope.core.persistence.api.entity.task.Task) BulkActionResult(org.apache.syncope.common.lib.to.BulkActionResult) JobNamer(org.apache.syncope.core.provisioning.api.job.JobNamer) NotificationDAO(org.apache.syncope.core.persistence.api.dao.NotificationDAO) TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) Collectors(java.util.stream.Collectors) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) List(java.util.List) JobAction(org.apache.syncope.common.lib.types.JobAction) Component(org.springframework.stereotype.Component) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) JobDataMap(org.quartz.JobDataMap) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) JobType(org.apache.syncope.common.lib.types.JobType) TaskJob(org.apache.syncope.core.provisioning.java.job.TaskJob) ConfDAO(org.apache.syncope.core.persistence.api.dao.ConfDAO) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) TaskType(org.apache.syncope.common.lib.types.TaskType) Transactional(org.springframework.transaction.annotation.Transactional) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) Task(org.apache.syncope.core.persistence.api.entity.task.Task) ExecTO(org.apache.syncope.common.lib.to.ExecTO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with TaskExec

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());
}
Also used : PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 9 with TaskExec

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);
}
Also used : PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 10 with TaskExec

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;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) JobExecutionException(org.quartz.JobExecutionException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

TaskExec (org.apache.syncope.core.persistence.api.entity.task.TaskExec)19 Date (java.util.Date)9 PropagationTask (org.apache.syncope.core.persistence.api.entity.task.PropagationTask)7 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)7 ExecTO (org.apache.syncope.common.lib.to.ExecTO)5 Test (org.junit.jupiter.api.Test)5 Collectors (java.util.stream.Collectors)4 PropagationTaskTO (org.apache.syncope.common.lib.to.PropagationTaskTO)4 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)4 NotificationTask (org.apache.syncope.core.persistence.api.entity.task.NotificationTask)4 Transactional (org.springframework.transaction.annotation.Transactional)4 List (java.util.List)3 Map (java.util.Map)3 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3 ExternalResourceDAO (org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO)3 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)3 SchedTask (org.apache.syncope.core.persistence.api.entity.task.SchedTask)3 Task (org.apache.syncope.core.persistence.api.entity.task.Task)3 TaskUtils (org.apache.syncope.core.persistence.api.entity.task.TaskUtils)3 TaskUtilsFactory (org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory)3