Search in sources :

Example 1 with WfTask

use of com.evolveum.midpoint.wf.impl.tasks.WfTask 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 WfTask

use of com.evolveum.midpoint.wf.impl.tasks.WfTask in project midpoint by Evolveum.

the class WfPropagateTaskObjectReferenceTaskHandler method run.

//endregion
//region Body
@Override
public TaskRunResult run(Task task) {
    TaskRunResult.TaskRunResultStatus status = TaskRunResult.TaskRunResultStatus.FINISHED;
    OperationResult result = task.getResult().createSubresult(WfPropagateTaskObjectReferenceTaskHandler.class + ".run");
    WfTask wfTask = wfTaskController.recreateWfTask(task);
    LOGGER.trace("WfPropagateTaskObjectReferenceTaskHandler starting... job = {}", wfTask);
    ModelContext modelContext;
    try {
        modelContext = wfTask.retrieveModelContext(result);
        if (modelContext == null) {
            throw new IllegalStateException("There's no model context in the task; job = " + wfTask);
        }
    } catch (SchemaException | ConfigurationException | ObjectNotFoundException | ExpressionEvaluationException e) {
        return reportException("Couldn't retrieve model context from job " + wfTask, task, result, e);
    } catch (CommunicationException e) {
        return reportException("Couldn't retrieve model context from job " + wfTask, task, result, TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR, e);
    }
    String oid = ((LensContext) modelContext).getFocusContext().getOid();
    if (oid == null) {
        LOGGER.warn("No object OID in job " + wfTask);
    } else {
        Class typeClass = ((LensContext) modelContext).getFocusContext().getObjectTypeClass();
        QName type = typeClass != null ? JAXBUtil.getTypeQName(typeClass) : null;
        if (type == null) {
            LOGGER.warn("Unknown type of object " + oid + " in task " + task);
        } else {
            ObjectReferenceType objectReferenceType = new ObjectReferenceType();
            objectReferenceType.setType(type);
            objectReferenceType.setOid(oid);
            if (task.getObjectRef() == null) {
                task.setObjectRef(objectReferenceType);
            } else {
                LOGGER.warn("object reference in task " + task + " is already set, although it shouldn't be");
            }
            List<WfTask> dependents;
            try {
                dependents = wfTask.listDependents(result);
                dependents.add(wfTask.getParentJob(result));
            } catch (SchemaException e) {
                return reportException("Couldn't get dependents from job " + wfTask, task, result, e);
            } catch (ObjectNotFoundException e) {
                return reportException("Couldn't get dependents from job " + wfTask, task, result, e);
            }
            for (WfTask dependent : dependents) {
                if (dependent.getTask().getObjectRef() == null) {
                    try {
                        dependent.getTask().setObjectRefImmediate(objectReferenceType, result);
                    } catch (ObjectNotFoundException e) {
                        // note we DO NOT return, because we want to set all references we can
                        reportException("Couldn't set object reference on job " + dependent, task, result, e);
                    } catch (SchemaException e) {
                        reportException("Couldn't set object reference on job " + dependent, task, result, e);
                    } catch (ObjectAlreadyExistsException e) {
                        reportException("Couldn't set object reference on job " + dependent, task, result, e);
                    }
                } else {
                    LOGGER.warn("object reference in job " + dependent + " is already set, although it shouldn't be");
                }
            }
        }
    }
    result.computeStatusIfUnknown();
    TaskRunResult runResult = new TaskRunResult();
    runResult.setRunResultStatus(status);
    runResult.setOperationResult(task.getResult());
    return runResult;
}
Also used : QName(javax.xml.namespace.QName) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask) ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)

Example 3 with WfTask

use of com.evolveum.midpoint.wf.impl.tasks.WfTask in project midpoint by Evolveum.

the class GeneralChangeProcessor method applyScenario.

private HookOperationMode applyScenario(GeneralChangeProcessorScenarioType scenarioType, GcpScenarioBean scenarioBean, ModelContext context, Task taskFromModel, WfConfigurationType wfConfigurationType, OperationResult result) {
    try {
        // ========== preparing root task ===========
        WfTaskCreationInstruction rootInstruction = baseModelInvocationProcessingHelper.createInstructionForRoot(this, context, taskFromModel, result);
        WfTask rootWfTask = baseModelInvocationProcessingHelper.submitRootTask(rootInstruction, taskFromModel, wfConfigurationType, result);
        // ========== preparing child task, starting WF process ===========
        WfTaskCreationInstruction instruction = scenarioBean.prepareJobCreationInstruction(scenarioType, (LensContext<?>) context, rootWfTask, taskFromModel, result);
        wfTaskController.submitWfTask(instruction, rootWfTask, wfConfigurationType, result);
        // ========== complete the action ===========
        baseModelInvocationProcessingHelper.logJobsBeforeStart(rootWfTask, result);
        rootWfTask.startWaitingForSubtasks(result);
        return HookOperationMode.BACKGROUND;
    } catch (SchemaException | ObjectNotFoundException | CommunicationException | ConfigurationException | ObjectAlreadyExistsException | ExpressionEvaluationException | RuntimeException | Error e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Workflow process(es) could not be started", e);
        result.recordFatalError("Workflow process(es) could not be started: " + e, e);
        return HookOperationMode.ERROR;
    // todo rollback - at least close open tasks, maybe stop workflow process instances
    }
}
Also used : WfTaskCreationInstruction(com.evolveum.midpoint.wf.impl.tasks.WfTaskCreationInstruction) WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask)

Example 4 with WfTask

use of com.evolveum.midpoint.wf.impl.tasks.WfTask in project midpoint by Evolveum.

the class WfPrepareChildOperationTaskHandler method run.

//endregion
//region Body
@SuppressWarnings("unchecked")
@Override
public TaskRunResult run(Task task) {
    TaskRunResult.TaskRunResultStatus status = TaskRunResult.TaskRunResultStatus.FINISHED;
    LOGGER.trace("WfPrepareChildOperationTaskHandler starting... task = {}", task);
    try {
        WfTask wfTask = wfTaskController.recreateWfTask(task);
        OperationResult result = task.getResult();
        ModelContext<?> modelContext = wfTask.retrieveModelContext(result);
        if (modelContext == null) {
            throw new IllegalStateException("There's no model context in child task; task = " + task);
        }
        // prepare deltaOut to be used
        ObjectTreeDeltas deltasOut = wfTask.retrieveResultingDeltas();
        if (LOGGER.isTraceEnabled()) {
            dumpDeltaOut(deltasOut);
        }
        if (deltasOut == null || deltasOut.isEmpty()) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("There's no primary delta in focus context; we'll delete model operation context. Task = {}, model context:\n{}", task, modelContext.debugDump());
            }
            wfTask.deleteModelOperationContext();
        } else {
            // place deltaOut into model context
            modelContext.getFocusContext().setPrimaryDelta(deltasOut.getFocusChange());
            Set<Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>>> entries = deltasOut.getProjectionChangeMapEntries();
            for (Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>> entry : entries) {
                // TODO what if projection context does not exist?
                modelContext.findProjectionContext(entry.getKey()).setPrimaryDelta(entry.getValue());
            }
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Resulting model context to be stored into task {}:\n{}", task, modelContext.debugDump(0));
            }
            wfTask.storeModelContext(modelContext);
        }
        task.savePendingModifications(result);
    } catch (SchemaException | ObjectNotFoundException | ObjectAlreadyExistsException | ConfigurationException | ExpressionEvaluationException | RuntimeException | Error e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't prepare child model context", e);
        status = TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR;
    } catch (CommunicationException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't prepare child model context", e);
        status = TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR;
    }
    TaskRunResult runResult = new TaskRunResult();
    runResult.setRunResultStatus(status);
    return runResult;
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask) ResourceShadowDiscriminator(com.evolveum.midpoint.schema.ResourceShadowDiscriminator) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Map(java.util.Map) ObjectTreeDeltas(com.evolveum.midpoint.schema.ObjectTreeDeltas)

Example 5 with WfTask

use of com.evolveum.midpoint.wf.impl.tasks.WfTask in project midpoint by Evolveum.

the class BaseModelInvocationProcessingHelper method logJobsBeforeStart.

public void logJobsBeforeStart(WfTask rootWfTask, OperationResult result) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    if (!LOGGER.isTraceEnabled()) {
        return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append("===[ Situation just before root task starts waiting for subtasks ]===\n");
    sb.append("Root job = ").append(rootWfTask).append("; task = ").append(rootWfTask.getTask().debugDump()).append("\n");
    if (rootWfTask.hasModelContext()) {
        sb.append("Context in root task: \n").append(rootWfTask.retrieveModelContext(result).debugDump(1)).append("\n");
    }
    List<WfTask> children = rootWfTask.listChildren(result);
    for (int i = 0; i < children.size(); i++) {
        WfTask child = children.get(i);
        sb.append("Child job #").append(i).append(" = ").append(child).append(", its task:\n").append(child.getTask().debugDump(1));
        if (child.hasModelContext()) {
            sb.append("Context in child task:\n").append(child.retrieveModelContext(result).debugDump(2));
        }
    }
    LOGGER.trace("\n{}", sb.toString());
    LOGGER.trace("Now the root task starts waiting for child tasks");
}
Also used : WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask)

Aggregations

WfTask (com.evolveum.midpoint.wf.impl.tasks.WfTask)8 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)2 ObjectTreeDeltas (com.evolveum.midpoint.schema.ObjectTreeDeltas)2 ResourceShadowDiscriminator (com.evolveum.midpoint.schema.ResourceShadowDiscriminator)2 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)2 Map (java.util.Map)2 ModelContext (com.evolveum.midpoint.model.api.context.ModelContext)1 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 Task (com.evolveum.midpoint.task.api.Task)1 TaskRunResultStatus (com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus)1 ExecutionMode (com.evolveum.midpoint.wf.impl.processors.primary.PrimaryChangeProcessor.ExecutionMode)1 WfTaskCreationInstruction (com.evolveum.midpoint.wf.impl.tasks.WfTaskCreationInstruction)1 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)1 QName (javax.xml.namespace.QName)1