Search in sources :

Example 11 with TaskRunResult

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

the class MockCycleTaskHandler method run.

/* (non-Javadoc)
      * @see com.evolveum.midpoint.task.api.TaskHandler#run(com.evolveum.midpoint.task.api.Task)
      */
@Override
public TaskRunResult run(Task task) {
    LOGGER.info("MockCycle.run starting");
    long progress = task.getProgress();
    OperationResult opResult = new OperationResult(MockCycleTaskHandler.class.getName() + ".run");
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    // TODO
    progress++;
    opResult.recordSuccess();
    // This "run" is finished. But the task goes on ... (if finishTheHandler == false)
    runResult.setRunResultStatus(finishTheHandler ? TaskRunResultStatus.FINISHED_HANDLER : TaskRunResultStatus.FINISHED);
    runResult.setProgress(progress);
    LOGGER.info("MockCycle.run stopping");
    return runResult;
}
Also used : TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 12 with TaskRunResult

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

the class LiveSyncTaskHandler method runInternal.

private TaskRunResult runInternal(Task task) {
    LOGGER.trace("LiveSyncTaskHandler.run starting");
    long progress = task.getProgress();
    OperationResult opResult = new OperationResult(OperationConstants.LIVE_SYNC);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    String resourceOid = task.getObjectOid();
    if (resourceOid == null) {
        LOGGER.error("Live Sync: No resource OID specified in the task");
        opResult.recordFatalError("No resource OID specified in the task");
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    ResourceType resource = null;
    try {
        resource = provisioningService.getObject(ResourceType.class, resourceOid, null, task, opResult).asObjectable();
    } catch (ObjectNotFoundException ex) {
        LOGGER.error("Live Sync: Resource {} not found: {}", new Object[] { resourceOid, ex.getMessage(), ex });
        // This is bad. The resource does not exist. Permanent problem.
        opResult.recordFatalError("Resource not found " + resourceOid, ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (SchemaException ex) {
        LOGGER.error("Live Sync: Error dealing with schema: {}", ex.getMessage(), ex);
        // Not sure about this. But most likely it is a misconfigured resource or connector
        // It may be worth to retry. Error is fatal, but may not be permanent.
        opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (RuntimeException ex) {
        LOGGER.error("Live Sync: Internal Error: {}", ex.getMessage(), ex);
        // Can be anything ... but we can't recover from that.
        // It is most likely a programming error. Does not make much sense to retry.
        opResult.recordFatalError("Internal Error: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (CommunicationException ex) {
        LOGGER.error("Live Sync: Error getting resource {}: {}", new Object[] { resourceOid, ex.getMessage(), ex });
        opResult.recordFatalError("Error getting resource " + resourceOid + ": " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (ConfigurationException ex) {
        LOGGER.error("Live Sync: Error getting resource {}: {}", new Object[] { resourceOid, ex.getMessage(), ex });
        opResult.recordFatalError("Error getting resource " + resourceOid + ": " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (SecurityViolationException ex) {
        LOGGER.error("Live Sync: Error getting resource {}: {}", new Object[] { resourceOid, ex.getMessage(), ex });
        opResult.recordFatalError("Error getting resource " + resourceOid + ": " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (ExpressionEvaluationException ex) {
        LOGGER.error("Live Sync: Error getting resource {}: {}", new Object[] { resourceOid, ex.getMessage(), ex });
        opResult.recordFatalError("Error getting resource " + resourceOid + ": " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    if (resource == null) {
        LOGGER.error("Live Sync: No resource specified");
        opResult.recordFatalError("No resource specified");
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    RefinedResourceSchema refinedSchema;
    try {
        refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resource, LayerType.MODEL, prismContext);
    } catch (SchemaException e) {
        LOGGER.error("Live Sync: Schema error during processing account definition: {}", e.getMessage());
        opResult.recordFatalError("Schema error during processing account definition: " + e.getMessage(), e);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    if (refinedSchema == null) {
        opResult.recordFatalError("No refined schema defined. Probably some configuration problem.");
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        LOGGER.error("Live Sync: No refined schema defined. Probably some configuration problem.");
        return runResult;
    }
    ObjectClassComplexTypeDefinition objectClass;
    try {
        objectClass = Utils.determineObjectClass(refinedSchema, task);
    } catch (SchemaException e) {
        LOGGER.error("Live Sync: schema error: {}", e.getMessage());
        opResult.recordFatalError(e);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    if (objectClass == null) {
        LOGGER.debug("Syncing all object classes");
    }
    ResourceShadowDiscriminator coords = new ResourceShadowDiscriminator(resourceOid, objectClass == null ? null : objectClass.getTypeName());
    int changesProcessed;
    try {
        // MAIN PART
        // Calling synchronize(..) in provisioning.
        // This will detect the changes and notify model about them.
        // It will use extension of task to store synchronization state
        Utils.clearRequestee(task);
        changesProcessed = provisioningService.synchronize(coords, task, opResult);
        progress += changesProcessed;
    } catch (ObjectNotFoundException ex) {
        LOGGER.error("Live Sync: A required object does not exist, OID: {}", ex.getOid());
        LOGGER.error("Exception stack trace", ex);
        // This is bad. The resource or task or something like that does not exist. Permanent problem.
        opResult.recordFatalError("A required object does not exist, OID: " + ex.getOid(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (CommunicationException ex) {
        LOGGER.error("Live Sync: Communication error:", ex);
        // Error, but not critical. Just try later.
        opResult.recordPartialError("Communication error: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (SchemaException ex) {
        LOGGER.error("Live Sync: Error dealing with schema:", ex);
        // Not sure about this. But most likely it is a misconfigured resource or connector
        // It may be worth to retry. Error is fatal, but may not be permanent.
        opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (RuntimeException ex) {
        LOGGER.error("Live Sync: Internal Error:", ex);
        // Can be anything ... but we can't recover from that.
        // It is most likely a programming error. Does not make much sense to retry.
        opResult.recordFatalError("Internal Error: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (ConfigurationException ex) {
        LOGGER.error("Live Sync: Configuration error:", ex);
        // Not sure about this. But most likely it is a misconfigured resource or connector
        // It may be worth to retry. Error is fatal, but may not be permanent.
        opResult.recordFatalError("Configuration error: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (SecurityViolationException ex) {
        LOGGER.error("Recompute: Security violation: {}", ex.getMessage(), ex);
        opResult.recordFatalError("Security violation: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } catch (ExpressionEvaluationException ex) {
        LOGGER.error("Recompute: Expression error: {}", ex.getMessage(), ex);
        opResult.recordFatalError("Expression error: " + ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(progress);
        return runResult;
    }
    opResult.computeStatus("Live sync run has failed");
    opResult.createSubresult(OperationConstants.LIVE_SYNC_STATISTICS).recordStatus(OperationResultStatus.SUCCESS, "Changes processed: " + changesProcessed);
    // This "run" is finished. But the task goes on ...
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    // Might collide with increasing progress in provisioning module, e.g. when an exception is thrown. But that's OK for now.
    runResult.setProgress(progress);
    LOGGER.trace("LiveSyncTaskHandler.run stopping (resource {})", resourceOid);
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ObjectClassComplexTypeDefinition(com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ResourceShadowDiscriminator(com.evolveum.midpoint.schema.ResourceShadowDiscriminator) RefinedResourceSchema(com.evolveum.midpoint.common.refinery.RefinedResourceSchema)

Example 13 with TaskRunResult

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

the class AuditReindexTaskHandler method run.

@Override
public TaskRunResult run(Task coordinatorTask) {
    OperationResult opResult = new OperationResult(OperationConstants.AUDIT_REINDEX + ".run");
    opResult.setStatus(OperationResultStatus.IN_PROGRESS);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    final Long expectedTotal = auditService.countObjects("select count(*) from RAuditEventRecord as aer where 1=1", null);
    AuditResultHandler resultHandler = new AuditResultHandler() {

        private AtomicInteger processedObjects = new AtomicInteger();

        @Override
        public boolean handle(AuditEventRecord auditRecord) {
            auditService.reindexEntry(auditRecord);
            processedObjects.incrementAndGet();
            return true;
        }

        @Override
        public int getProgress() {
            return processedObjects.get();
        }
    };
    if (resultHandler == null) {
        // the error should already be in the runResult
        return runResult;
    }
    try {
        LOGGER.trace("{}: expecting {} objects to be processed", taskName, expectedTotal);
        runResult.setProgress(0);
        coordinatorTask.setProgress(0);
        if (expectedTotal != null) {
            coordinatorTask.setExpectedTotal(expectedTotal);
        }
        try {
            coordinatorTask.savePendingModifications(opResult);
        } catch (ObjectAlreadyExistsException e) {
            // try block
            throw new IllegalStateException("Unexpected ObjectAlreadyExistsException when updating task progress/expectedTotal", e);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        while (true) {
            params.put("setFirstResult", firstResult);
            params.put("setMaxResults", maxResults);
            List<AuditEventRecord> records = auditService.listRecords(null, params);
            if (CollectionUtils.isNotEmpty(records)) {
                for (AuditEventRecord record : records) {
                    resultHandler.handle(record);
                    runResult.setProgress(resultHandler.getProgress());
                }
                firstResult += maxResults;
                maxResults = ((expectedTotal.intValue() - firstResult) > maxResults ? maxResults : (expectedTotal.intValue() - firstResult));
            } else {
                break;
            }
        }
        opResult.recordSuccess();
    } catch (ObjectNotFoundException e) {
        // This is bad. The resource does not exist. Permanent problem.
        logErrorAndSetResult(runResult, resultHandler, "Object not found", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (SchemaException e) {
        // Not sure about this. But most likely it is a misconfigured
        // resource or connector
        // It may be worth to retry. Error is fatal, but may not be
        // permanent.
        logErrorAndSetResult(runResult, resultHandler, "Error dealing with schema", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (RuntimeException e) {
        // Can be anything ... but we can't recover from that.
        // It is most likely a programming error. Does not make much sense
        // to retry.
        logErrorAndSetResult(runResult, resultHandler, "Internal error", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    // TODO: check last handler status
    runResult.setProgress(resultHandler.getProgress());
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    String finishMessage = "Finished " + taskName + " (" + coordinatorTask + "). ";
    String statistics = "Processed " + resultHandler.getProgress() + " objects";
    opResult.createSubresult(OperationConstants.AUDIT_REINDEX + ".statistics").recordStatus(OperationResultStatus.SUCCESS, statistics);
    LOGGER.info(finishMessage + statistics);
    LOGGER.trace("{} run finished (task {}, run result {})", taskName, coordinatorTask, runResult);
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) HashMap(java.util.HashMap) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) AuditResultHandler(com.evolveum.midpoint.audit.api.AuditResultHandler) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 14 with TaskRunResult

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

the class CleanUpTaskHandler method runInternal.

private TaskRunResult runInternal(Task task) {
    LOGGER.trace("CleanUpTaskHandler.run starting");
    long progress = task.getProgress();
    OperationResult opResult = new OperationResult(OperationConstants.CLEANUP);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    CleanupPoliciesType cleanupPolicies = task.getExtensionPropertyRealValue(SchemaConstants.MODEL_EXTENSION_CLEANUP_POLICIES);
    if (cleanupPolicies != null) {
        LOGGER.info("Using task-specific cleanupPolicies: {}", cleanupPolicies);
    } else {
        PrismObject<SystemConfigurationType> systemConfig;
        try {
            systemConfig = repositoryService.getObject(SystemConfigurationType.class, SystemObjectsType.SYSTEM_CONFIGURATION.value(), null, opResult);
        } catch (ObjectNotFoundException ex) {
            LOGGER.error("Cleanup: Object does not exist: {}", ex.getMessage(), ex);
            opResult.recordFatalError("Object does not exist: " + ex.getMessage(), ex);
            runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
            runResult.setProgress(progress);
            return runResult;
        } catch (SchemaException ex) {
            LOGGER.error("Cleanup: Error dealing with schema: {}", ex.getMessage(), ex);
            opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
            runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
            runResult.setProgress(progress);
            return runResult;
        }
        SystemConfigurationType systemConfigType = systemConfig.asObjectable();
        cleanupPolicies = systemConfigType.getCleanupPolicy();
    }
    if (cleanupPolicies == null) {
        LOGGER.trace("Cleanup: No clean up polices specified. Finishing clean up task.");
        opResult.computeStatus();
        runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
        runResult.setProgress(progress);
        return runResult;
    }
    CleanupPolicyType auditCleanupPolicy = cleanupPolicies.getAuditRecords();
    if (auditCleanupPolicy != null) {
        try {
            auditService.cleanupAudit(auditCleanupPolicy, opResult);
        } catch (Exception ex) {
            LOGGER.error("Cleanup: {}", ex.getMessage(), ex);
            opResult.recordFatalError(ex.getMessage(), ex);
            runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
            runResult.setProgress(progress);
        }
    } else {
        LOGGER.trace("Cleanup: No clean up policy for audit specified. Finishing clean up task.");
    }
    CleanupPolicyType closedTasksPolicy = cleanupPolicies.getClosedTasks();
    if (closedTasksPolicy != null) {
        try {
            taskManager.cleanupTasks(closedTasksPolicy, task, opResult);
        } catch (Exception ex) {
            LOGGER.error("Cleanup: {}", ex.getMessage(), ex);
            opResult.recordFatalError(ex.getMessage(), ex);
            runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
            runResult.setProgress(progress);
        }
    } else {
        LOGGER.trace("Cleanup: No clean up policy for closed tasks specified. Finishing clean up task.");
    }
    CleanupPolicyType reportCleanupPolicy = cleanupPolicies.getOutputReports();
    if (reportCleanupPolicy != null) {
        try {
            if (reportManager == null) {
                //TODO improve dependencies for report-impl (probably for tests) and set autowire to required
                LOGGER.error("Report manager was not autowired, reports cleanup will be skipped.");
            } else {
                reportManager.cleanupReports(reportCleanupPolicy, opResult);
            }
        } catch (Exception ex) {
            LOGGER.error("Cleanup: {}", ex.getMessage(), ex);
            opResult.recordFatalError(ex.getMessage(), ex);
            runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
            runResult.setProgress(progress);
        }
    } else {
        LOGGER.trace("Cleanup: No clean up policy for report specified. Finishing clean up task.");
    }
    opResult.computeStatus();
    // This "run" is finished. But the task goes on ...
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    runResult.setProgress(progress);
    LOGGER.trace("CleanUpTaskHandler.run stopping");
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) CleanupPolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) CleanupPoliciesType(com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPoliciesType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SystemConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 15 with TaskRunResult

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

the class MockTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    LOGGER.trace("MockTaskHandler.run starting");
    OperationResult opResult = new OperationResult(OperationConstants.RECONCILIATION);
    opResult.setStatus(OperationResultStatus.IN_PROGRESS);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    opResult.recordSuccess();
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    runResult.setProgress(1);
    LOGGER.trace("MockTaskHandler.run ending");
    return runResult;
}
Also used : TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Aggregations

TaskRunResult (com.evolveum.midpoint.task.api.TaskRunResult)17 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)16 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)8 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)3 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)2 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)2 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)2 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)2 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)2 SystemException (com.evolveum.midpoint.util.exception.SystemException)2 AccessCertificationCampaignType (com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCampaignType)2 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)2 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)2 SystemConfigurationType (com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType)2 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)1 AuditResultHandler (com.evolveum.midpoint.audit.api.AuditResultHandler)1 CertificationHandler (com.evolveum.midpoint.certification.impl.handlers.CertificationHandler)1