Search in sources :

Example 1 with ScheduleType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType in project midpoint by Evolveum.

the class TaskEditableState method getScheduleType.

public ScheduleType getScheduleType() {
    ScheduleType rv = new ScheduleType();
    rv.setInterval(interval);
    rv.setCronLikePattern(cronSpecification);
    rv.setEarliestStartTime(MiscUtil.asXMLGregorianCalendar(notStartBefore));
    rv.setLatestStartTime(MiscUtil.asXMLGregorianCalendar(notStartAfter));
    rv.setMisfireAction(misfireActionType);
    return rv;
}
Also used : ScheduleType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType)

Example 2 with ScheduleType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType in project midpoint by Evolveum.

the class PageTaskAdd method createTask.

private TaskType createTask(TaskAddDto dto) throws SchemaException {
    TaskType task = new TaskType();
    MidPointPrincipal owner = SecurityUtils.getPrincipalUser();
    ObjectReferenceType ownerRef = new ObjectReferenceType();
    ownerRef.setOid(owner.getOid());
    ownerRef.setType(owner.getUser().COMPLEX_TYPE);
    task.setOwnerRef(ownerRef);
    task.setCategory(dto.getCategory());
    String handlerUri = getTaskManager().getHandlerUriForCategory(dto.getCategory());
    if (handlerUri == null) {
        throw new SystemException("Cannot determine task handler URI for category " + dto.getCategory());
    }
    task.setHandlerUri(handlerUri);
    ObjectReferenceType objectRef;
    if (dto.getResource() != null) {
        objectRef = new ObjectReferenceType();
        objectRef.setOid(dto.getResource().getOid());
        objectRef.setType(ResourceType.COMPLEX_TYPE);
        task.setObjectRef(objectRef);
    }
    task.setName(WebComponentUtil.createPolyFromOrigString(dto.getName()));
    task.setRecurrence(dto.getReccuring() ? TaskRecurrenceType.RECURRING : TaskRecurrenceType.SINGLE);
    task.setBinding(dto.getBound() ? TaskBindingType.TIGHT : TaskBindingType.LOOSE);
    ScheduleType schedule = new ScheduleType();
    schedule.setInterval(dto.getInterval());
    schedule.setCronLikePattern(dto.getCron());
    schedule.setEarliestStartTime(MiscUtil.asXMLGregorianCalendar(dto.getNotStartBefore()));
    schedule.setLatestStartTime(MiscUtil.asXMLGregorianCalendar(dto.getNotStartAfter()));
    schedule.setMisfireAction(dto.getMisfireAction());
    task.setSchedule(schedule);
    if (dto.getSuspendedState()) {
        task.setExecutionStatus(TaskExecutionStatusType.SUSPENDED);
    } else {
        task.setExecutionStatus(TaskExecutionStatusType.RUNNABLE);
    }
    if (dto.getThreadStop() != null) {
        task.setThreadStopAction(dto.getThreadStop());
    } else {
        // fill-in default
        if (dto.getRunUntilNodeDown() == true) {
            task.setThreadStopAction(ThreadStopActionType.CLOSE);
        } else {
            task.setThreadStopAction(ThreadStopActionType.RESTART);
        }
    }
    if (dto.isDryRun()) {
        PrismObject<TaskType> prismTask = task.asPrismObject();
        ItemPath path = new ItemPath(TaskType.F_EXTENSION, SchemaConstants.MODEL_EXTENSION_DRY_RUN);
        PrismProperty dryRun = prismTask.findOrCreateProperty(path);
        SchemaRegistry registry = getPrismContext().getSchemaRegistry();
        PrismPropertyDefinition def = registry.findPropertyDefinitionByElementName(SchemaConstants.MODEL_EXTENSION_DRY_RUN);
        dryRun.setDefinition(def);
        dryRun.setRealValue(true);
    }
    if (dto.getFocusType() != null) {
        PrismObject<TaskType> prismTask = task.asPrismObject();
        ItemPath path = new ItemPath(TaskType.F_EXTENSION, SchemaConstants.MODEL_EXTENSION_OBJECT_TYPE);
        PrismProperty focusType = prismTask.findOrCreateProperty(path);
        focusType.setRealValue(dto.getFocusType());
    }
    if (dto.getKind() != null) {
        PrismObject<TaskType> prismTask = task.asPrismObject();
        ItemPath path = new ItemPath(TaskType.F_EXTENSION, SchemaConstants.MODEL_EXTENSION_KIND);
        PrismProperty kind = prismTask.findOrCreateProperty(path);
        SchemaRegistry registry = getPrismContext().getSchemaRegistry();
        PrismPropertyDefinition def = registry.findPropertyDefinitionByElementName(SchemaConstants.MODEL_EXTENSION_KIND);
        kind.setDefinition(def);
        kind.setRealValue(dto.getKind());
    }
    if (dto.getIntent() != null && StringUtils.isNotEmpty(dto.getIntent())) {
        PrismObject<TaskType> prismTask = task.asPrismObject();
        ItemPath path = new ItemPath(TaskType.F_EXTENSION, SchemaConstants.MODEL_EXTENSION_INTENT);
        PrismProperty intent = prismTask.findOrCreateProperty(path);
        SchemaRegistry registry = getPrismContext().getSchemaRegistry();
        PrismPropertyDefinition def = registry.findPropertyDefinitionByElementName(SchemaConstants.MODEL_EXTENSION_INTENT);
        intent.setDefinition(def);
        intent.setRealValue(dto.getIntent());
    }
    if (dto.getObjectClass() != null && StringUtils.isNotEmpty(dto.getObjectClass())) {
        PrismObject<TaskType> prismTask = task.asPrismObject();
        ItemPath path = new ItemPath(TaskType.F_EXTENSION, SchemaConstants.OBJECTCLASS_PROPERTY_NAME);
        PrismProperty objectClassProperty = prismTask.findOrCreateProperty(path);
        QName objectClass = null;
        for (QName q : model.getObject().getObjectClassList()) {
            if (q.getLocalPart().equals(dto.getObjectClass())) {
                objectClass = q;
            }
        }
        SchemaRegistry registry = getPrismContext().getSchemaRegistry();
        PrismPropertyDefinition def = registry.findPropertyDefinitionByElementName(SchemaConstants.OBJECTCLASS_PROPERTY_NAME);
        objectClassProperty.setDefinition(def);
        objectClassProperty.setRealValue(objectClass);
    }
    return task;
}
Also used : ScheduleType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) SystemException(com.evolveum.midpoint.util.exception.SystemException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) QName(javax.xml.namespace.QName) TaskType(com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType) SchemaRegistry(com.evolveum.midpoint.prism.schema.SchemaRegistry) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 3 with ScheduleType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType in project midpoint by Evolveum.

the class TaskQuartzImplUtil method createTriggerForTask.

public static Trigger createTriggerForTask(Task task) throws ParseException {
    if (task.getExecutionStatus() != TaskExecutionStatus.RUNNABLE) {
        // no triggers for such tasks
        return null;
    }
    // special case - recurrent task with no schedule (means "run on demand only")
    if (task.isCycle() && (task.getSchedule() == null || (task.getSchedule().getInterval() == null && task.getSchedule().getCronLikePattern() == null))) {
        return null;
    }
    TriggerBuilder<Trigger> tb = TriggerBuilder.newTrigger().withIdentity(createTriggerKeyForTask(task)).forJob(createJobKeyForTask(task));
    if (task.getSchedule() != null) {
        Date est = task.getSchedule().getEarliestStartTime() != null ? task.getSchedule().getEarliestStartTime().toGregorianCalendar().getTime() : null;
        Date lst = task.getSchedule().getLatestStartTime() != null ? task.getSchedule().getLatestStartTime().toGregorianCalendar().getTime() : null;
        // endAt must not be sooner than startAt
        if (lst != null && est == null) {
            if (lst.getTime() < System.currentTimeMillis()) {
                // there's no point in setting est to current time
                est = lst;
            }
        }
        if (est != null) {
            tb.startAt(est);
        } else {
            tb.startNow();
        }
        if (lst != null) {
            tb.endAt(lst);
        // LST is checked also within JobExecutor (needed mainly for tightly-bound recurrent tasks)
        }
        if (task.getSchedule().getLatestFinishTime() != null) {
            tb.endAt(task.getSchedule().getLatestFinishTime().toGregorianCalendar().getTime());
        // however, it is the responsibility of task handler to finish no later than this time
        }
    }
    boolean looselyBoundRecurrent;
    if (task.isCycle() && task.isLooselyBound()) {
        looselyBoundRecurrent = true;
        ScheduleType sch = task.getSchedule();
        if (sch == null) {
            return null;
        //throw new IllegalStateException("Recurrent task " + task + " does not have a schedule.");
        }
        ScheduleBuilder sb;
        if (sch.getInterval() != null) {
            SimpleScheduleBuilder ssb = simpleSchedule().withIntervalInSeconds(sch.getInterval().intValue()).repeatForever();
            if (sch.getMisfireAction() == null || sch.getMisfireAction() == MisfireActionType.EXECUTE_IMMEDIATELY) {
                sb = ssb.withMisfireHandlingInstructionFireNow();
            } else if (sch.getMisfireAction() == MisfireActionType.RESCHEDULE) {
                sb = ssb.withMisfireHandlingInstructionNextWithRemainingCount();
            } else {
                throw new SystemException("Invalid value of misfireAction: " + sch.getMisfireAction() + " for task " + task);
            }
        } else if (sch.getCronLikePattern() != null) {
            // may throw ParseException
            CronScheduleBuilder csb = cronScheduleNonvalidatedExpression(sch.getCronLikePattern());
            if (sch.getMisfireAction() == null || sch.getMisfireAction() == MisfireActionType.EXECUTE_IMMEDIATELY) {
                sb = csb.withMisfireHandlingInstructionFireAndProceed();
            } else if (sch.getMisfireAction() == MisfireActionType.RESCHEDULE) {
                sb = csb.withMisfireHandlingInstructionDoNothing();
            } else {
                throw new SystemException("Invalid value of misfireAction: " + sch.getMisfireAction() + " for task " + task);
            }
        } else {
            return null;
        //throw new IllegalStateException("The schedule for task " + task + " is neither fixed nor cron-like one.");
        }
        tb.withSchedule(sb);
    } else {
        // even non-recurrent tasks will be triggered, to check whether they should not be restarted
        // (their trigger will be erased when these tasks will be completed)
        looselyBoundRecurrent = false;
    // tb.withSchedule(simpleSchedule().withIntervalInMilliseconds(SINGLE_TASK_CHECK_INTERVAL).repeatForever());
    }
    tb.usingJobData("schedule", scheduleFingerprint(task.getSchedule()));
    tb.usingJobData("looselyBoundRecurrent", looselyBoundRecurrent);
    tb.usingJobData("handlerUri", task.getHandlerUri());
    return tb.build();
}
Also used : ScheduleType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType) SystemException(com.evolveum.midpoint.util.exception.SystemException) Date(java.util.Date)

Example 4 with ScheduleType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType in project midpoint by Evolveum.

the class MockSingleTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    LOGGER.info("MockSingle.run starting (id = " + id + ")");
    long progress = task.getProgress();
    OperationResult opResult = new OperationResult(MockSingleTaskHandler.class.getName() + ".run");
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    // TODO
    progress++;
    opResult.recordSuccess();
    // This "run" is finished. But the task goes on ...
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    runResult.setProgress(progress);
    hasRun = true;
    if ("L1".equals(id)) {
        PrismProperty<Boolean> l1flag = task.getExtensionProperty(L1_FLAG_QNAME);
        if (l1flag == null || l1flag.getRealValue() == false) {
            LOGGER.info("L1 handler, first run - scheduling L2 handler");
            ScheduleType l2Schedule = new ScheduleType();
            l2Schedule.setInterval(2);
            task.pushHandlerUri(TestQuartzTaskManagerContract.L2_TASK_HANDLER_URI, l2Schedule, TaskBinding.TIGHT, ((TaskQuartzImpl) task).createExtensionDelta(l1FlagDefinition, true));
            try {
                task.savePendingModifications(opResult);
            } catch (Exception e) {
                throw new SystemException("Cannot schedule L2 handler", e);
            }
            runResult.setRunResultStatus(TaskRunResultStatus.RESTART_REQUESTED);
        } else {
            LOGGER.info("L1 handler, not the first run (progress = " + progress + ", l1Flag = " + l1flag.getRealValue() + "), exiting.");
        }
    } else if ("L2".equals(id)) {
        if (progress == 5) {
            LOGGER.info("L2 handler, fourth run - scheduling L3 handler");
            task.pushHandlerUri(TestQuartzTaskManagerContract.L3_TASK_HANDLER_URI, new ScheduleType(), null);
            try {
                task.savePendingModifications(opResult);
            } catch (Exception e) {
                throw new SystemException("Cannot schedule L3 handler", e);
            }
            runResult.setRunResultStatus(TaskRunResultStatus.RESTART_REQUESTED);
        } else if (progress < 5) {
            LOGGER.info("L2 handler, progress = " + progress + ", continuing.");
        } else if (progress > 5) {
            LOGGER.info("L2 handler, progress too big, i.e. " + progress + ", exiting.");
            try {
                ((TaskQuartzImpl) task).finishHandler(opResult);
            } catch (Exception e) {
                throw new SystemException("Cannot finish L2 handler", e);
            }
        }
    } else if ("L3".equals(id)) {
        LOGGER.info("L3 handler, simply exiting. Progress = " + progress);
    } else if ("WFS".equals(id)) {
        PrismProperty<Boolean> wfsFlag = task.getExtensionProperty(WFS_FLAG_QNAME);
        if (wfsFlag == null || wfsFlag.getRealValue() == false) {
            LOGGER.info("Wait-for-subtasks creating subtasks...");
            Task t1 = task.createSubtask();
            t1.setHandlerUri(TestQuartzTaskManagerContract.L3_TASK_HANDLER_URI);
            taskManager.switchToBackground(t1, opResult);
            Task t2 = task.createSubtask();
            t2.setHandlerUri(TestQuartzTaskManagerContract.SINGLE_TASK_HANDLER_URI);
            taskManager.switchToBackground(t2, opResult);
            try {
                ArrayList<ItemDelta<?, ?>> deltas = new ArrayList<>();
                deltas.add(((TaskQuartzImpl) task).createExtensionDelta(wfsFlagDefinition, true));
                runResult = ((TaskQuartzImpl) task).waitForSubtasks(2, deltas, opResult);
                runResult.setProgress(1);
            } catch (Exception e) {
                throw new SystemException("WaitForSubtasks failed.", e);
            }
        } else {
            LOGGER.info("Wait-for-subtasks seems to finish successfully; progress = " + progress + ", wfsFlag = " + wfsFlag.getRealValue());
        }
    }
    LOGGER.info("MockSingle.run stopping");
    return runResult;
}
Also used : ScheduleType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SystemException(com.evolveum.midpoint.util.exception.SystemException) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

ScheduleType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScheduleType)4 SystemException (com.evolveum.midpoint.util.exception.SystemException)3 PrismProperty (com.evolveum.midpoint.prism.PrismProperty)2 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 SchemaRegistry (com.evolveum.midpoint.prism.schema.SchemaRegistry)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)1 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)1 TaskType (com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 QName (javax.xml.namespace.QName)1