Search in sources :

Example 16 with TaskQuartzImpl

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

the class TaskPersister method addTask.

public String addTask(PrismObject<TaskType> taskPrism, RepoAddOptions options, OperationResult result) throws ObjectAlreadyExistsException, SchemaException {
    if (taskPrism.asObjectable().getOwnerRef() == null) {
        try {
            MidPointPrincipal principal = SecurityUtil.getPrincipal();
            if (principal != null) {
                ObjectReferenceType newOwnerRef = ObjectTypeUtil.createObjectRef(principal.getFocus(), prismContext);
                taskPrism.asObjectable().setOwnerRef(newOwnerRef);
            }
        } catch (SecurityViolationException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't determine logged-in user. Task owner was not set.", e);
        }
    }
    // perhaps redundant, but it's more convenient to work with Task than with Task prism
    TaskQuartzImpl task = taskInstantiator.createTaskInstance(taskPrism, result);
    if (task.getTaskIdentifier() == null) {
        task.setTaskIdentifier(generateTaskIdentifier().toString());
    }
    return addTaskToRepositoryAndQuartz(task, options, result);
}
Also used : ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 17 with TaskQuartzImpl

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

the class TaskStateManager method clearTaskOperationResult.

// endregion
// region Misc
static void clearTaskOperationResult(Task task, OperationResult result) throws SchemaException, ObjectNotFoundException {
    OperationResult emptyTaskResult = new OperationResult("run");
    emptyTaskResult.setStatus(OperationResultStatus.IN_PROGRESS);
    ((TaskQuartzImpl) task).setResultImmediate(emptyTaskResult, result);
}
Also used : TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 18 with TaskQuartzImpl

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

the class TaskStateManager method scheduleTaskNow.

// endregion
// region Schedule now!
public void scheduleTaskNow(String taskOid, OperationResult result) throws SchemaException, ObjectNotFoundException {
    TaskQuartzImpl task = taskRetriever.getTaskPlain(taskOid, result);
    scheduleTaskNow(task, result);
}
Also used : TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl)

Example 19 with TaskQuartzImpl

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

the class TaskCleaner method cleanupTasks.

public void cleanupTasks(@NotNull CleanupPolicyType policy, @NotNull Predicate<TaskType> selector, @NotNull RunningTask executionTask, @NotNull OperationResult result) throws SchemaException, ObjectNotFoundException {
    if (policy.getMaxAge() == null) {
        return;
    }
    TimeBoundary timeBoundary = TimeBoundary.compute(policy.getMaxAge());
    XMLGregorianCalendar deleteTasksClosedUpTo = timeBoundary.getBoundary();
    LOGGER.info("Starting cleanup for closed tasks deleting up to {} (duration '{}').", deleteTasksClosedUpTo, timeBoundary.getPositiveDuration());
    ObjectQuery obsoleteTasksQuery = prismContext.queryFor(TaskType.class).item(TaskType.F_EXECUTION_STATE).eq(TaskExecutionStateType.CLOSED).and().item(TaskType.F_COMPLETION_TIMESTAMP).le(deleteTasksClosedUpTo).and().item(TaskType.F_PARENT).isNull().build();
    List<PrismObject<TaskType>> obsoleteTasks = repositoryService.searchObjects(TaskType.class, obsoleteTasksQuery, null, result);
    LOGGER.debug("Found {} task tree(s) to be cleaned up", obsoleteTasks.size());
    boolean interrupted = false;
    int deleted = 0;
    int problems = 0;
    int subtasksProblems = 0;
    root: for (PrismObject<TaskType> rootTaskPrism : obsoleteTasks) {
        if (!executionTask.canRun()) {
            result.recordWarning("Interrupted");
            LOGGER.warn("Task cleanup was interrupted.");
            interrupted = true;
            break;
        }
        IterativeOperationStartInfo iterativeOperationStartInfo = new IterativeOperationStartInfo(new IterationItemInformation(rootTaskPrism));
        iterativeOperationStartInfo.setSimpleCaller(true);
        Operation op = executionTask.recordIterativeOperationStart(iterativeOperationStartInfo);
        try {
            // get whole tree
            TaskQuartzImpl rootTask = taskInstantiator.createTaskInstance(rootTaskPrism, result);
            if (rootTask.isIndestructible()) {
                LOGGER.trace("Not deleting {} as it is indestructible", rootTaskPrism);
                op.skipped();
                continue;
            }
            if (!selector.test(rootTaskPrism.asObjectable())) {
                LOGGER.debug("Not deleting {} because it was rejected by the selector", rootTaskPrism);
                op.skipped();
                continue;
            }
            List<TaskQuartzImpl> taskTreeMembers = rootTask.listSubtasksDeeply(true, result);
            for (TaskQuartzImpl child : taskTreeMembers) {
                if (child.isIndestructible()) {
                    LOGGER.trace("Not deleting {} as it has an indestructible child: {}", rootTask, child);
                    op.skipped();
                    continue root;
                }
                if (!selector.test(child.getRawTaskObject().asObjectable())) {
                    LOGGER.debug("Not deleting {} because the user has no authorization to delete one of the children: {}", rootTask, child);
                    op.skipped();
                    continue root;
                }
            }
            taskTreeMembers.add(rootTask);
            LOGGER.trace("Removing task {} along with its {} children.", rootTask, taskTreeMembers.size() - 1);
            Throwable lastProblem = null;
            for (Task task : taskTreeMembers) {
                try {
                    // TODO use repository service only - the task should be closed now
                    taskStateManager.deleteTask(task.getOid(), result);
                    deleted++;
                } catch (SchemaException | ObjectNotFoundException | RuntimeException e) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Couldn't delete obsolete task {}", e, task);
                    lastProblem = e;
                    problems++;
                    if (!task.getTaskIdentifier().equals(rootTask.getTaskIdentifier())) {
                        subtasksProblems++;
                    }
                }
            }
            // approximate solution (as the problem might be connected to a subtask)
            if (lastProblem != null) {
                op.failed(lastProblem);
            } else {
                op.succeeded();
            }
        } catch (Throwable t) {
            op.failed(t);
            throw t;
        }
        // structured progress is incremented with iterative operation reporting
        executionTask.incrementLegacyProgressAndStoreStatisticsIfTimePassed(result);
    }
    LOGGER.info("Task cleanup procedure " + (interrupted ? "was interrupted" : "finished") + ". Successfully deleted {} tasks; there were problems with deleting {} tasks.", deleted, problems);
    if (subtasksProblems > 0) {
        LOGGER.error("{} subtask(s) couldn't be deleted. Inspect that manually, otherwise they might reside in repo forever.", subtasksProblems);
    }
    String suffix = interrupted ? " Interrupted." : "";
    if (problems == 0) {
        result.createSubresult(OP_STATISTICS).recordStatus(SUCCESS, "Successfully deleted " + deleted + " task(s)." + suffix);
    } else {
        result.createSubresult(OP_STATISTICS).recordPartialError("Successfully deleted " + deleted + " task(s), " + "there was problems with deleting " + problems + " tasks." + suffix + (subtasksProblems > 0 ? (" " + subtasksProblems + " subtask(s) couldn't be deleted, please see the log.") : ""));
    }
}
Also used : RunningTask(com.evolveum.midpoint.task.api.RunningTask) Task(com.evolveum.midpoint.task.api.Task) Operation(com.evolveum.midpoint.schema.statistics.Operation) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PrismObject(com.evolveum.midpoint.prism.PrismObject) TimeBoundary(com.evolveum.midpoint.task.quartzimpl.util.TimeBoundary) IterativeOperationStartInfo(com.evolveum.midpoint.schema.statistics.IterativeOperationStartInfo) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) List(java.util.List) IterationItemInformation(com.evolveum.midpoint.schema.statistics.IterationItemInformation)

Example 20 with TaskQuartzImpl

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

the class TaskRetriever method resolveTaskOids.

// if there are problems with retrieving a task, we just log exception and put into operation result
List<TaskQuartzImpl> resolveTaskOids(Collection<String> oids, OperationResult result) {
    List<TaskQuartzImpl> tasks = new ArrayList<>();
    for (String oid : oids) {
        try {
            TaskQuartzImpl task = getTaskPlain(oid, result);
            tasks.add(task);
        } catch (ObjectNotFoundException e) {
            LoggingUtils.logException(LOGGER, "Couldn't retrieve task with OID {}", e, oid);
        } catch (SchemaException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't retrieve task with OID {}", e, oid);
        }
    }
    return tasks;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ArrayList(java.util.ArrayList)

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