Search in sources :

Example 1 with TaskQuartzImpl

use of com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl in project midpoint by Evolveum.

the class TaskSynchronizer method synchronizeJobStores.

/**
     * Checks for consistency between Quartz job store and midPoint repository.
     * In case of conflict, the latter is taken as authoritative source.
     *
     * (For RAM Job Store, running this method at startup effectively means that tasks from midPoint repo are imported into Quartz job store.)
     *
     */
boolean synchronizeJobStores(OperationResult parentResult) {
    OperationResult result = parentResult.createSubresult(this.getClass().getName() + ".synchronizeJobStores");
    Scheduler scheduler = taskManager.getExecutionManager().getQuartzScheduler();
    LOGGER.info("Synchronizing Quartz job store with midPoint repository.");
    List<PrismObject<TaskType>> tasks;
    try {
        tasks = getRepositoryService().searchObjects(TaskType.class, new ObjectQuery(), null, result);
    } catch (SchemaException | RuntimeException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Synchronization cannot be done, because tasks cannot be listed from the repository.", e);
        return false;
    }
    LOGGER.trace("There are {} task(s) in repository", tasks.size());
    // check consistency of tasks present in repo
    Set<String> oidsInRepo = new HashSet<>();
    int processed = 0;
    int changed = 0;
    int errors = 0;
    for (PrismObject<TaskType> taskPrism : tasks) {
        if (taskPrism.getOid() == null) {
            LOGGER.error("Skipping task with no OID: {}", taskPrism);
            errors++;
            continue;
        }
        oidsInRepo.add(taskPrism.getOid());
        TaskQuartzImpl task;
        try {
            // in order for the task to be "fresh"
            task = (TaskQuartzImpl) taskManager.getTask(taskPrism.getOid(), result);
            if (synchronizeTask(task, result)) {
                // todo are we sure that we increment this counter only for successfully processed tasks? we hope so :)
                changed++;
            }
        } catch (SchemaException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Task Manager cannot synchronize task {} due to schema exception.", e, taskPrism.getOid());
        } catch (ObjectNotFoundException e) {
            LoggingUtils.logException(LOGGER, "Task Manager cannot synchronize task {} because it does not exist", e, taskPrism.getOid());
        }
        if (result.getLastSubresultStatus() == OperationResultStatus.SUCCESS) {
            processed++;
        } else {
            errors++;
        }
    }
    // remove non-existing tasks
    int removed = 0;
    Set<JobKey> jobs = null;
    try {
        jobs = new HashSet<>(scheduler.getJobKeys(jobGroupEquals(JobKey.DEFAULT_GROUP)));
    } catch (SchedulerException e) {
        String message = "Cannot list jobs from Quartz scheduler, skipping second part of synchronization procedure.";
        LoggingUtils.logUnexpectedException(LOGGER, message, e);
        result.recordPartialError(message, e);
    }
    if (jobs != null) {
        LOGGER.trace("There are {} job(s) in Quartz job store", jobs.size());
        for (JobKey job : jobs) {
            if (!oidsInRepo.contains(job.getName()) && !RemoteNodesManager.STARTER_JOB_KEY.equals(job)) {
                LOGGER.info("Task " + job.getName() + " is not in repository, removing from Quartz job store.");
                try {
                    scheduler.deleteJob(job);
                    removed++;
                } catch (SchedulerException e) {
                    String message = "Cannot remove job " + job.getName() + " from Quartz job store";
                    LoggingUtils.logUnexpectedException(LOGGER, message, e);
                    result.createSubresult("deleteQuartzJob").recordPartialError(message, e);
                    errors++;
                }
            }
        }
    }
    String resultMessage = "Synchronization of midpoint and Quartz task store finished. " + processed + " task(s) existing in midPoint repository successfully processed, resulting in " + changed + " updated Quartz job(s). " + removed + " task(s) removed from Quartz job store. Processing of " + errors + " task(s) failed.";
    LOGGER.info(resultMessage);
    if (result.isUnknown()) {
        result.recordStatus(OperationResultStatus.SUCCESS, resultMessage);
    }
    return true;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) PrismObject(com.evolveum.midpoint.prism.PrismObject) JobKey(org.quartz.JobKey) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) HashSet(java.util.HashSet)

Example 2 with TaskQuartzImpl

use of com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl in project midpoint by Evolveum.

the class TaskSynchronizer method synchronizeJobStores.

/**
 * Checks for consistency between Quartz job store and midPoint repository.
 * In case of conflict, the latter is taken as authoritative source.
 *
 * (For RAM Job Store, running this method at startup effectively means that tasks from midPoint repo are imported into Quartz job store.)
 */
public boolean synchronizeJobStores(OperationResult parentResult) {
    OperationResult result = parentResult.createSubresult(this.getClass().getName() + ".synchronizeJobStores");
    Scheduler scheduler = localScheduler.getQuartzScheduler();
    LOGGER.info("Synchronizing Quartz job store with midPoint repository.");
    List<PrismObject<TaskType>> tasks;
    try {
        tasks = repositoryService.searchObjects(TaskType.class, null, null, result);
    } catch (SchemaException | RuntimeException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Synchronization cannot be done, because tasks cannot be listed from the repository.", e);
        return false;
    }
    LOGGER.trace("There are {} task(s) in repository", tasks.size());
    // check consistency of tasks present in repo
    Set<String> oidsInRepo = new HashSet<>();
    int processed = 0;
    int changed = 0;
    int errors = 0;
    for (PrismObject<TaskType> taskPrism : tasks) {
        if (taskPrism.getOid() == null) {
            LOGGER.error("Skipping task with no OID: {}", taskPrism);
            errors++;
            continue;
        }
        oidsInRepo.add(taskPrism.getOid());
        TaskQuartzImpl task;
        try {
            // in order for the task to be "fresh"
            task = taskManager.getTaskPlain(taskPrism.getOid(), result);
            if (synchronizeTask(task, result)) {
                // todo are we sure that we increment this counter only for successfully processed tasks? we hope so :)
                changed++;
            }
        } catch (SchemaException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Task Manager cannot synchronize task {} due to schema exception.", e, taskPrism.getOid());
        } catch (ObjectNotFoundException e) {
            LoggingUtils.logException(LOGGER, "Task Manager cannot synchronize task {} because it does not exist", e, taskPrism.getOid());
        }
        if (result.getLastSubresultStatus() == OperationResultStatus.SUCCESS) {
            processed++;
        } else {
            errors++;
        }
    }
    // remove non-existing tasks
    int removed = 0;
    Set<JobKey> jobs = null;
    try {
        jobs = new HashSet<>(scheduler.getJobKeys(jobGroupEquals(JobKey.DEFAULT_GROUP)));
    } catch (SchedulerException e) {
        String message = "Cannot list jobs from Quartz scheduler, skipping second part of synchronization procedure.";
        LoggingUtils.logUnexpectedException(LOGGER, message, e);
        result.recordPartialError(message, e);
    }
    if (jobs != null) {
        LOGGER.trace("There are {} job(s) in Quartz job store", jobs.size());
        for (JobKey job : jobs) {
            if (!oidsInRepo.contains(job.getName()) && !RemoteSchedulers.STARTER_JOB_KEY.equals(job)) {
                LOGGER.info("Task " + job.getName() + " is not in repository, removing from Quartz job store.");
                try {
                    scheduler.deleteJob(job);
                    removed++;
                } catch (SchedulerException e) {
                    String message = "Cannot remove job " + job.getName() + " from Quartz job store";
                    LoggingUtils.logUnexpectedException(LOGGER, message, e);
                    result.createSubresult("deleteQuartzJob").recordPartialError(message, e);
                    errors++;
                }
            }
        }
    }
    String resultMessage = "Synchronization of midpoint and Quartz task store finished. " + processed + " task(s) existing in midPoint repository successfully processed, resulting in " + changed + " updated Quartz job(s). " + removed + " task(s) removed from Quartz job store. Processing of " + errors + " task(s) failed.";
    LOGGER.info(resultMessage);
    if (result.isUnknown()) {
        result.recordStatus(OperationResultStatus.SUCCESS, resultMessage);
    }
    return true;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismObject(com.evolveum.midpoint.prism.PrismObject) JobKey(org.quartz.JobKey) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) HashSet(java.util.HashSet)

Example 3 with TaskQuartzImpl

use of com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl in project midpoint by Evolveum.

the class ScheduleNowHelper method scheduleCoordinatorAndWorkersNow.

public void scheduleCoordinatorAndWorkersNow(String coordinatorOid, OperationResult result) throws SchemaException, ObjectNotFoundException {
    TaskQuartzImpl coordinatorTask = taskRetriever.getTaskPlain(coordinatorOid, result);
    TaskSchedulingStateType state = coordinatorTask.getSchedulingState();
    switch(state) {
        case CLOSED:
        case READY:
            // hoping that the task handler will do what is needed (i.e. recreate or restart workers)
            scheduleTaskNow(coordinatorTask, result);
            break;
        case WAITING:
            // this means that workers are either busy (runnable) or are suspended; administrator should do something with that
            String msg1 = "Coordinator " + coordinatorTask + " cannot be run now, because it is in WAITING scheduling state. " + "Please check and resolve state of its worker tasks.";
            LOGGER.error(msg1);
            result.recordFatalError(msg1);
            break;
        case SUSPENDED:
            String msg2 = "Coordinator " + coordinatorTask + " cannot be run now, because it is in SUSPENDED state. " + "Please use appropriate method to schedule its execution.";
            LOGGER.error(msg2);
            result.recordFatalError(msg2);
            break;
        default:
            throw new IllegalStateException("Coordinator " + coordinatorTask + " is in unsupported state: " + state);
    }
}
Also used : TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) TaskSchedulingStateType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskSchedulingStateType)

Example 4 with TaskQuartzImpl

use of com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl in project midpoint by Evolveum.

the class TaskRetriever method getTask.

/**
 * Connects to the remote node if needed.
 */
@NotNull
public TaskQuartzImpl getTask(String oid, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) throws SchemaException, ObjectNotFoundException {
    try {
        // returns null if noFetch is set
        ClusterStatusInformation csi = clusterStatusInformationRetriever.getClusterStatusInformation(options, TaskType.class, true, result);
        PrismObject<TaskType> taskPrism = getTaskFromRemoteNode(oid, options, csi, result);
        if (taskPrism == null) {
            taskPrism = repositoryService.getObject(TaskType.class, oid, options, result);
        }
        TaskQuartzImpl task = taskInstantiator.createTaskInstance(taskPrism, result);
        addTransientTaskInformation(task, csi, SelectorOptions.hasToLoadPath(TaskType.F_NEXT_RUN_START_TIMESTAMP, options), SelectorOptions.hasToLoadPath(TaskType.F_NEXT_RETRY_TIMESTAMP, options), SelectorOptions.hasToLoadPath(TaskType.F_NODE_AS_OBSERVED, options), result);
        if (SelectorOptions.hasToLoadPath(TaskType.F_SUBTASK_REF, options)) {
            fillInSubtasks(task, csi, options, result);
        }
        updateFromTaskInMemory(task);
        return task;
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) ClusterStatusInformation(com.evolveum.midpoint.task.quartzimpl.cluster.ClusterStatusInformation) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TaskQuartzImpl

use of com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl in project midpoint by Evolveum.

the class TaskRetriever method addTransientTaskInformation.

// task is Task or PrismObject<TaskType>
private void addTransientTaskInformation(Object task, ClusterStatusInformation csi, boolean retrieveNextRunStartTime, boolean retrieveRetryTime, boolean retrieveNodeAsObserved, OperationResult result) {
    if (!isPersistent(task)) {
        throw new IllegalStateException("Task " + task + " is not persistent");
    }
    if (task instanceof RunningTask) {
        throw new UnsupportedOperationException("addTransientTaskInformation is not available for running tasks");
    }
    TaskType taskBean;
    if (task instanceof TaskQuartzImpl) {
        taskBean = ((TaskQuartzImpl) task).getRawTaskObject().asObjectable();
    } else if (task instanceof PrismObject<?>) {
        // noinspection unchecked
        taskBean = ((PrismObject<TaskType>) task).asObjectable();
    } else {
        throw new IllegalArgumentException("task: " + task);
    }
    if (csi != null && retrieveNodeAsObserved) {
        NodeType runsAt = csi.findNodeInfoForTask(taskBean.getOid());
        if (runsAt != null) {
            taskBean.setNodeAsObserved(runsAt.getNodeIdentifier());
        }
    }
    if (retrieveNextRunStartTime || retrieveRetryTime) {
        NextStartTimes times = localScheduler.getNextStartTimes(taskBean.getOid(), retrieveNextRunStartTime, retrieveRetryTime, result);
        if (retrieveNextRunStartTime && times.getNextScheduledRun() != null) {
            taskBean.setNextRunStartTimestamp(XmlTypeConverter.createXMLGregorianCalendar(times.getNextScheduledRun()));
        }
        if (retrieveRetryTime && times.getNextRetry() != null) {
            taskBean.setNextRetryTimestamp(XmlTypeConverter.createXMLGregorianCalendar(times.getNextRetry()));
        }
    }
    Long stalledSince = stalledTasksWatcher.getStalledSinceForTask(taskBean);
    if (stalledSince != null) {
        taskBean.setStalledSince(XmlTypeConverter.createXMLGregorianCalendar(stalledSince));
    }
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) NextStartTimes(com.evolveum.midpoint.task.quartzimpl.quartz.NextStartTimes)

Aggregations

TaskQuartzImpl (com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl)24 ArrayList (java.util.ArrayList)5 PrismObject (com.evolveum.midpoint.prism.PrismObject)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 Task (com.evolveum.midpoint.task.api.Task)4 NotNull (org.jetbrains.annotations.NotNull)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 TaskType (com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType)3 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 HashSet (java.util.HashSet)2 JobKey (org.quartz.JobKey)2 Scheduler (org.quartz.Scheduler)2 SchedulerException (org.quartz.SchedulerException)2 PreconditionViolationException (com.evolveum.midpoint.repo.api.PreconditionViolationException)1 IterationItemInformation (com.evolveum.midpoint.schema.statistics.IterationItemInformation)1 IterativeOperationStartInfo (com.evolveum.midpoint.schema.statistics.IterativeOperationStartInfo)1 Operation (com.evolveum.midpoint.schema.statistics.Operation)1 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)1 RunningTask (com.evolveum.midpoint.task.api.RunningTask)1