Search in sources :

Example 6 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskLogic method updateSchedTask.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_UPDATE + "')")
public <T extends SchedTaskTO> T updateSchedTask(final TaskType type, final SchedTaskTO taskTO) {
    SchedTask task = taskDAO.find(taskTO.getKey());
    if (task == null) {
        throw new NotFoundException("Task " + taskTO.getKey());
    }
    TaskUtils taskUtils = taskUtilsFactory.getInstance(task);
    if (taskUtils.getType() != type) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        sce.getElements().add("Found " + type + ", expected " + taskUtils.getType());
        throw sce;
    }
    binder.updateSchedTask(task, taskTO, taskUtils);
    task = taskDAO.save(task);
    try {
        jobManager.register(task, task.getStartAt(), confDAO.find("tasks.interruptMaxRetries", 1L));
    } catch (Exception e) {
        LOG.error("While registering quartz job for task " + task.getKey(), e);
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    return binder.getTaskTO(task, taskUtils, false);
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskLogic method execute.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_EXECUTE + "')")
@Override
public ExecTO execute(final String key, final Date startAt, final boolean dryRun) {
    Task task = taskDAO.find(key);
    if (task == null) {
        throw new NotFoundException("Task " + key);
    }
    if (startAt != null && startAt.before(new Date())) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add("Cannot schedule in the past");
        throw sce;
    }
    TaskUtils taskUtil = taskUtilsFactory.getInstance(task);
    ExecTO result = null;
    switch(taskUtil.getType()) {
        case PROPAGATION:
            TaskExec propExec = taskExecutor.execute((PropagationTaskTO) binder.getTaskTO(task, taskUtil, false));
            result = binder.getExecTO(propExec);
            break;
        case NOTIFICATION:
            TaskExec notExec = notificationJobDelegate.executeSingle((NotificationTask) task);
            result = binder.getExecTO(notExec);
            break;
        case SCHEDULED:
        case PULL:
        case PUSH:
            if (!((SchedTask) task).isActive()) {
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add("Task " + key + " is not active");
                throw sce;
            }
            try {
                Map<String, Object> jobDataMap = jobManager.register((SchedTask) task, startAt, confDAO.find("tasks.interruptMaxRetries", 1L));
                jobDataMap.put(TaskJob.DRY_RUN_JOBDETAIL_KEY, dryRun);
                if (startAt == null) {
                    scheduler.getScheduler().triggerJob(JobNamer.getJobKey(task), new JobDataMap(jobDataMap));
                }
            } catch (Exception e) {
                LOG.error("While executing task {}", task, e);
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add(e.getMessage());
                throw sce;
            }
            result = new ExecTO();
            result.setJobType(JobType.TASK);
            result.setRefKey(task.getKey());
            result.setRefDesc(binder.buildRefDesc(task));
            result.setStart(new Date());
            result.setStatus("JOB_FIRED");
            result.setMessage("Job fired; waiting for results...");
            break;
        default:
    }
    return result;
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) 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) JobDataMap(org.quartz.JobDataMap) ExecTO(org.apache.syncope.common.lib.to.ExecTO) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Date(java.util.Date) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskLogic method createSchedTask.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_CREATE + "')")
public <T extends SchedTaskTO> T createSchedTask(final TaskType type, final T taskTO) {
    TaskUtils taskUtils = taskUtilsFactory.getInstance(taskTO);
    if (taskUtils.getType() != type) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        sce.getElements().add("Found " + type + ", expected " + taskUtils.getType());
        throw sce;
    }
    SchedTask task = binder.createSchedTask(taskTO, taskUtils);
    task = taskDAO.save(task);
    try {
        jobManager.register(task, task.getStartAt(), confDAO.find("tasks.interruptMaxRetries", 1L));
    } catch (Exception e) {
        LOG.error("While registering quartz job for task " + task.getKey(), e);
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    return binder.getTaskTO(task, taskUtils, false);
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 9 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class GroupLogic method bulkMembersAction.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_CREATE + "') " + "and hasRole('" + StandardEntitlement.TASK_EXECUTE + "')")
@Transactional
public ExecTO bulkMembersAction(final String key, final BulkMembersActionType actionType) {
    Group group = groupDAO.find(key);
    if (group == null) {
        throw new NotFoundException("Group " + key);
    }
    Implementation jobDelegate = implementationDAO.find(ImplementationType.TASKJOB_DELEGATE).stream().filter(impl -> GroupMemberProvisionTaskJobDelegate.class.getName().equals(impl.getBody())).findFirst().orElse(null);
    if (jobDelegate == null) {
        jobDelegate = entityFactory.newEntity(Implementation.class);
        jobDelegate.setKey(GroupMemberProvisionTaskJobDelegate.class.getSimpleName());
        jobDelegate.setEngine(ImplementationEngine.JAVA);
        jobDelegate.setType(ImplementationType.TASKJOB_DELEGATE);
        jobDelegate.setBody(GroupMemberProvisionTaskJobDelegate.class.getName());
        jobDelegate = implementationDAO.save(jobDelegate);
    }
    SchedTask task = entityFactory.newEntity(SchedTask.class);
    task.setName("Bulk member provision for group " + group.getName());
    task.setActive(true);
    task.setJobDelegate(jobDelegate);
    task = taskDAO.save(task);
    try {
        Map<String, Object> jobDataMap = jobManager.register(task, null, confDAO.find("tasks.interruptMaxRetries", 1L));
        jobDataMap.put(TaskJob.DRY_RUN_JOBDETAIL_KEY, false);
        jobDataMap.put(GroupMemberProvisionTaskJobDelegate.GROUP_KEY_JOBDETAIL_KEY, key);
        jobDataMap.put(GroupMemberProvisionTaskJobDelegate.ACTION_TYPE_JOBDETAIL_KEY, actionType);
        scheduler.getScheduler().triggerJob(JobNamer.getJobKey(task), new JobDataMap(jobDataMap));
    } catch (Exception e) {
        LOG.error("While executing task {}", task, e);
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    ExecTO result = new ExecTO();
    result.setJobType(JobType.TASK);
    result.setRefKey(task.getKey());
    result.setRefDesc(taskDataBinder.buildRefDesc(task));
    result.setStart(new Date());
    result.setStatus("JOB_FIRED");
    result.setMessage("Job fired; waiting for results...");
    return result;
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) JobDataMap(org.quartz.JobDataMap) ExecTO(org.apache.syncope.common.lib.to.ExecTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) GroupMemberProvisionTaskJobDelegate(org.apache.syncope.core.provisioning.java.job.GroupMemberProvisionTaskJobDelegate) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) Date(java.util.Date) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SchedTask (org.apache.syncope.core.persistence.api.entity.task.SchedTask)9 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)8 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)5 SchedulerException (org.quartz.SchedulerException)5 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)4 Task (org.apache.syncope.core.persistence.api.entity.task.Task)4 TaskUtils (org.apache.syncope.core.persistence.api.entity.task.TaskUtils)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Date (java.util.Date)3 ExecTO (org.apache.syncope.common.lib.to.ExecTO)3 JobDataMap (org.quartz.JobDataMap)3 Transactional (org.springframework.transaction.annotation.Transactional)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 StringUtils (org.apache.commons.lang3.StringUtils)2 ProvisioningTaskTO (org.apache.syncope.common.lib.to.ProvisioningTaskTO)2 ImplementationType (org.apache.syncope.common.lib.types.ImplementationType)2 TaskType (org.apache.syncope.common.lib.types.TaskType)2 ImplementationDAO (org.apache.syncope.core.persistence.api.dao.ImplementationDAO)2