Search in sources :

Example 1 with TaskRunResultStatus

use of com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus in project midpoint by Evolveum.

the class WfPrepareRootOperationTaskHandler method run.

//endregion
//region run method
@Override
public TaskRunResult run(Task task) {
    TaskRunResultStatus status = TaskRunResultStatus.FINISHED;
    try {
        OperationResult result = task.getResult();
        WfTask rootWfTask = wfTaskController.recreateRootWfTask(task);
        List<WfTask> children = rootWfTask.listChildren(result);
        LensContext rootContext = (LensContext) rootWfTask.retrieveModelContext(result);
        boolean changed = false;
        for (WfTask child : children) {
            if (child.getTaskExecutionStatus() != TaskExecutionStatus.CLOSED) {
                throw new IllegalStateException("Child task " + child + " is not in CLOSED state; its state is " + child.getTaskExecutionStatus());
            }
            if (child.hasModelContext()) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Child job {} has model context present - skipping fetching deltas from it.", child);
                }
            } else {
                ObjectTreeDeltas deltas = child.retrieveResultingDeltas();
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Child job {} returned {} deltas", child, deltas != null ? deltas.getDeltaList().size() : 0);
                }
                if (deltas != null) {
                    LensFocusContext focusContext = rootContext.getFocusContext();
                    ObjectDelta focusDelta = deltas.getFocusChange();
                    if (focusDelta != null) {
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Adding delta from job {} to root model context; delta = {}", child, focusDelta.debugDump(0));
                        }
                        if (focusContext.getPrimaryDelta() != null && !focusContext.getPrimaryDelta().isEmpty()) {
                            focusContext.addPrimaryDelta(focusDelta);
                        } else {
                            focusContext.setPrimaryDelta(focusDelta);
                        }
                        changed = true;
                    }
                    Set<Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>>> entries = deltas.getProjectionChangeMapEntries();
                    for (Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>> entry : entries) {
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Adding projection delta from job {} to root model context; rsd = {}, delta = {}", child, entry.getKey(), entry.getValue().debugDump());
                        }
                        ModelProjectionContext projectionContext = rootContext.findProjectionContext(entry.getKey());
                        if (projectionContext == null) {
                            // TODO more liberal treatment?
                            throw new IllegalStateException("No projection context for " + entry.getKey());
                        }
                        if (projectionContext.getPrimaryDelta() != null && !projectionContext.getPrimaryDelta().isEmpty()) {
                            projectionContext.addPrimaryDelta(entry.getValue());
                        } else {
                            projectionContext.setPrimaryDelta(entry.getValue());
                        }
                        changed = true;
                    }
                }
            }
        }
        if (!rootContext.hasAnyPrimaryChange()) {
            // deletes the model context
            rootContext = null;
            // regardless of whether rootContext was changed or not
            changed = true;
        }
        if (changed) {
            rootWfTask.storeModelContext(rootContext);
            rootWfTask.commitChanges(result);
        }
    } catch (SchemaException | ObjectNotFoundException | ObjectAlreadyExistsException | ConfigurationException | ExpressionEvaluationException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't aggregate resulting deltas from child workflow-monitoring tasks due to schema exception", e);
        status = TaskRunResultStatus.PERMANENT_ERROR;
    } catch (CommunicationException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't aggregate resulting deltas from child workflow-monitoring tasks", e);
        status = TaskRunResultStatus.TEMPORARY_ERROR;
    }
    TaskRunResult runResult = new TaskRunResult();
    runResult.setRunResultStatus(status);
    return runResult;
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ObjectTreeDeltas(com.evolveum.midpoint.schema.ObjectTreeDeltas) TaskRunResultStatus(com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask) ResourceShadowDiscriminator(com.evolveum.midpoint.schema.ResourceShadowDiscriminator) LensFocusContext(com.evolveum.midpoint.model.impl.lens.LensFocusContext) Map(java.util.Map)

Example 2 with TaskRunResultStatus

use of com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus in project midpoint by Evolveum.

the class WfProcessInstanceShadowTaskHandler method run.

/*
     * There are two kinds of wf process-watching tasks: passive and active.
     * 
     * *Passive tasks* are used when wf processes are sophisticated enough to send events
     * about their state changes (using e.g. listeners or custom java tasks). In that case
     * we simply use midpoint tasks as holders of information coming within these events.
     * 
     * In the future, we will implement the original idea that when the workflow process
     * instance finishes, the task status will be changed to RUNNABLE, and then this task 
     * will be picked up by TaskManager to be run. This handler will be then called.
     * However, as for now, all processing (including post-processing after wf process
     * finish) is done within WorkflowHook.activiti2midpoint method.
     * 
     * As for *active tasks*, these are used to monitor simple wf processes, which do
     * not send any information to midpoint by themselves. These tasks are recurrent,
     * so their run() method is periodically executed. This method simply asks the
     * WfMS for the information about the particular process id. The response is asynchronous,
     * and is processed within WorkflowHook.activiti2midpoint method.
     *  
     */
@Override
public TaskRunResult run(Task task) {
    if (wfConfiguration.isEnabled()) {
        // todo: fixme this is a bit weird
        if (task.getExecutionStatus() == TaskExecutionStatus.CLOSED) {
            LOGGER.info("Task {} has been flagged as closed; exiting the run() method.", task);
        } else {
            String id = wfTaskUtil.getProcessId(task);
            if (id != null) {
                LOGGER.debug("Task {}: requesting status for wf process id {}", task, id);
                queryProcessInstance(id, task, task.getResult());
            }
        }
    } else {
        LOGGER.info("Workflow management is not currently enabled, skipping the task run.");
    }
    TaskRunResult result = new TaskRunResult();
    TaskRunResultStatus runResultStatus;
    if (wfTaskUtil.isProcessInstanceFinished(task)) {
        runResultStatus = TaskRunResultStatus.FINISHED_HANDLER;
    } else {
        // finished means this run has finished, not the whole task
        runResultStatus = TaskRunResultStatus.FINISHED;
    }
    // todo fix this
    result.setOperationResult(task.getResult());
    result.setRunResultStatus(runResultStatus);
    return result;
}
Also used : TaskRunResultStatus(com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus)

Example 3 with TaskRunResultStatus

use of com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus in project midpoint by Evolveum.

the class WaitForSubtasksByPollingTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    OperationResult opResult = new OperationResult(WaitForSubtasksByPollingTaskHandler.class.getName() + ".run");
    TaskRunResult runResult = new TaskRunResult();
    LOGGER.info("WaitForSubtasksByPollingTaskHandler run starting; in task " + task.getName());
    List<PrismObject<TaskType>> subtasks = null;
    try {
        subtasks = ((TaskQuartzImpl) task).listSubtasksRaw(opResult);
    } catch (SchemaException e) {
        throw new SystemException("Couldn't list subtasks of " + task + " due to schema exception", e);
    }
    LOGGER.info("Number of subtasks found: " + subtasks.size() + "; task = {}", task);
    boolean allClosed = true;
    for (PrismObject<TaskType> t : subtasks) {
        if (t.asObjectable().getExecutionStatus() != TaskExecutionStatusType.CLOSED) {
            LOGGER.info("Subtask " + t.getOid() + "/" + t.asObjectable().getName() + " is not closed, it is " + t.asObjectable().getExecutionStatus() + ", for task {}", task);
            allClosed = false;
            break;
        }
    }
    TaskRunResultStatus status;
    if (allClosed) {
        LOGGER.info("All subtasks are closed, finishing waiting for them; task = {}", task);
        status = TaskRunResultStatus.FINISHED_HANDLER;
    } else {
        status = TaskRunResultStatus.FINISHED;
    }
    // not to overwrite task's result
    runResult.setOperationResult(null);
    // not to overwrite task's progress
    runResult.setProgress(task.getProgress());
    runResult.setRunResultStatus(status);
    LOGGER.info("WaitForSubtasksByPollingTaskHandler run finishing; in task " + task.getName());
    return runResult;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) SystemException(com.evolveum.midpoint.util.exception.SystemException) TaskRunResultStatus(com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Aggregations

TaskRunResultStatus (com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 ModelProjectionContext (com.evolveum.midpoint.model.api.context.ModelProjectionContext)1 LensContext (com.evolveum.midpoint.model.impl.lens.LensContext)1 LensFocusContext (com.evolveum.midpoint.model.impl.lens.LensFocusContext)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)1 ObjectTreeDeltas (com.evolveum.midpoint.schema.ObjectTreeDeltas)1 ResourceShadowDiscriminator (com.evolveum.midpoint.schema.ResourceShadowDiscriminator)1 TaskRunResult (com.evolveum.midpoint.task.api.TaskRunResult)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 WfTask (com.evolveum.midpoint.wf.impl.tasks.WfTask)1 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)1 TaskType (com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType)1 Map (java.util.Map)1