Search in sources :

Example 26 with Job

use of org.ovirt.engine.core.common.job.Job in project ovirt-engine by oVirt.

the class ExecutionHandler method addStep.

/**
 * Adds a {@link Step} entity by the provided context. A {@link Step} will not be created if
 * {@code ExecutionContext.isMonitored()} returns false.
 *
 * @param context
 *            The context of the execution which defines visibility and execution method.
 * @param stepName
 *            The name of the step.
 * @param description
 *            A presentation name for the step. If not provided, the presentation name is resolved by the
 *            {@code stepName}.
 * @param isExternal
 *        Indicates if the step is invoked by a plug-in
 */
public Step addStep(ExecutionContext context, StepEnum stepName, String description, boolean isExternal) {
    if (context == null) {
        return null;
    }
    Step step = null;
    if (context.isMonitored()) {
        if (description == null) {
            description = ExecutionMessageDirector.getInstance().getStepMessage(stepName);
        }
        try {
            Job job = context.getJob();
            if (context.getExecutionMethod() == ExecutionMethod.AsJob && job != null) {
                step = job.addStep(stepName, description);
                try {
                    step.setExternal(isExternal);
                    jobRepository.saveStep(step);
                } catch (Exception e) {
                    log.error("Failed to save new step '{}' for job '{}', '{}': {}", stepName.name(), job.getId(), job.getActionType().name(), e.getMessage());
                    log.debug("Exception", e);
                    job.getSteps().remove(step);
                    step = null;
                }
            } else {
                Step contextStep = context.getStep();
                if (context.getExecutionMethod() == ExecutionMethod.AsStep && contextStep != null) {
                    step = addSubStep(contextStep, stepName, description, Collections.emptyList());
                    step.setExternal(isExternal);
                }
            }
        } catch (Exception e) {
            log.error("Exception", e);
        }
    }
    return step;
}
Also used : Step(org.ovirt.engine.core.common.job.Step) Job(org.ovirt.engine.core.common.job.Job)

Example 27 with Job

use of org.ovirt.engine.core.common.job.Job in project ovirt-engine by oVirt.

the class ExecutionHandler method endJob.

/**
 * Finalizes a {@code Job} execution by a given context in which the job was performed and by the exit status of
 * the step. If the {@code Job} execution continues beyond the scope of the command, the {@code Job.isAsyncJob()}
 * should be set to {@code true}. If {@code ExecutionMethod.AsStep} is defined, the current active step can end the
 * running {@code Job} by setting the {@link ExecutionContext#shouldEndJob()} to
 * {@code true}.
 *
 * @param context
 *            The context of the execution which defines how the job should be ended
 * @param exitStatus
 *            Indicates if the execution described by the job ended successfully or not.
 */
public void endJob(ExecutionContext context, boolean exitStatus) {
    if (context == null) {
        return;
    }
    Job job = context.getJob();
    try {
        if (context.isMonitored()) {
            if (context.getExecutionMethod() == ExecutionMethod.AsJob && job != null) {
                if (context.shouldEndJob() || !(job.isAsyncJob() && exitStatus)) {
                    context.setCompleted(true);
                    endJob(exitStatus, job);
                }
            } else {
                Step step = context.getStep();
                if (context.getExecutionMethod() == ExecutionMethod.AsStep && step != null) {
                    if (context.shouldEndJob()) {
                        if (job == null) {
                            job = jobRepository.getJob(step.getJobId());
                        }
                        if (job != null) {
                            context.setCompleted(true);
                            endJob(exitStatus, job);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception", e);
    }
}
Also used : Step(org.ovirt.engine.core.common.job.Step) Job(org.ovirt.engine.core.common.job.Job)

Example 28 with Job

use of org.ovirt.engine.core.common.job.Job in project ovirt-engine by oVirt.

the class ExecutionHandler method getJob.

private Job getJob(CommandBase<?> command, ActionType actionType) {
    ActionParametersBase params = command.getParameters();
    Job job;
    // if Job is external, we had already created the Job by AddExternalJobCommand, so just get it from DB
    if (params.getJobId() != null) {
        job = jobDao.get(params.getJobId());
    } else {
        job = createJob(actionType, command);
        jobRepository.saveJob(job);
    }
    return job;
}
Also used : Job(org.ovirt.engine.core.common.job.Job) ActionParametersBase(org.ovirt.engine.core.common.action.ActionParametersBase)

Example 29 with Job

use of org.ovirt.engine.core.common.job.Job in project ovirt-engine by oVirt.

the class ExecutionHandler method startFinalizingStep.

/**
 * Method should be called when finalizing the command. The execution step is being ended with success and the
 * finalization step is started.
 *
 * @param executionContext
 *            The context of the job
 * @return A created instance of the Finalizing step
 */
public Step startFinalizingStep(ExecutionContext executionContext) {
    if (executionContext == null) {
        return null;
    }
    Step step = null;
    try {
        if (executionContext.getExecutionMethod() == ExecutionMethod.AsJob) {
            Job job = executionContext.getJob();
            if (job != null) {
                Step executingStep = job.getStep(StepEnum.EXECUTING);
                Step finalizingStep = job.addStep(StepEnum.FINALIZING, ExecutionMessageDirector.getInstance().getStepMessage(StepEnum.FINALIZING));
                if (executingStep != null) {
                    executingStep.markStepEnded(true);
                    jobRepository.updateExistingStepAndSaveNewStep(executingStep, finalizingStep);
                } else {
                    jobRepository.saveStep(finalizingStep);
                }
            }
        } else if (executionContext.getExecutionMethod() == ExecutionMethod.AsStep) {
            Step parentStep = executionContext.getStep();
            if (parentStep != null) {
                Step executingStep = parentStep.getStep(StepEnum.EXECUTING);
                Step finalizingStep = parentStep.addStep(StepEnum.FINALIZING, ExecutionMessageDirector.getInstance().getStepMessage(StepEnum.FINALIZING));
                if (executingStep != null) {
                    executingStep.markStepEnded(true);
                    jobRepository.updateExistingStepAndSaveNewStep(executingStep, finalizingStep);
                } else {
                    jobRepository.saveStep(finalizingStep);
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception", e);
    }
    return step;
}
Also used : Step(org.ovirt.engine.core.common.job.Step) Job(org.ovirt.engine.core.common.job.Job)

Example 30 with Job

use of org.ovirt.engine.core.common.job.Job in project ovirt-engine by oVirt.

the class ExecutionHandler method endStep.

/**
 * Finalizes a {@code Step} execution by a given context in which the step was performed and by the exit status of
 * the step.
 *
 * @param context
 *            The context in which the {@code Step} was executed.
 * @param step
 *            The step to finalize.
 * @param exitStatus
 *            Indicates if the execution described by the step ended successfully or not.
 */
public void endStep(ExecutionContext context, Step step, boolean exitStatus) {
    if (context == null) {
        return;
    }
    if (context.isMonitored()) {
        Job job = context.getJob();
        try {
            if (step != null) {
                step.markStepEnded(exitStatus);
                jobRepository.updateStep(step);
            }
            if (context.getExecutionMethod() == ExecutionMethod.AsJob && job != null && !exitStatus) {
                // step failure will cause the job to be marked as failed
                context.setCompleted(true);
                job.markJobEnded(false);
                jobRepository.updateCompletedJobAndSteps(job);
            } else {
                Step parentStep = context.getStep();
                if (context.getExecutionMethod() == ExecutionMethod.AsStep && parentStep != null) {
                    context.setCompleted(true);
                    if (job != null && !exitStatus) {
                        job.markJobEnded(false);
                        jobRepository.updateCompletedJobAndSteps(job);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Exception", e);
        }
    }
}
Also used : Step(org.ovirt.engine.core.common.job.Step) Job(org.ovirt.engine.core.common.job.Job)

Aggregations

Job (org.ovirt.engine.core.common.job.Job)35 Step (org.ovirt.engine.core.common.job.Step)7 Date (java.util.Date)6 Guid (org.ovirt.engine.core.compat.Guid)6 JobExecutionStatus (org.ovirt.engine.core.common.job.JobExecutionStatus)4 ArrayList (java.util.ArrayList)3 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)3 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)3 Inject (com.google.inject.Inject)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 VdcObjectType (org.ovirt.engine.core.common.VdcObjectType)2 GetJobsByOffsetQueryParameters (org.ovirt.engine.core.common.queries.GetJobsByOffsetQueryParameters)2 Event (org.ovirt.engine.ui.uicompat.Event)2 EventArgs (org.ovirt.engine.ui.uicompat.EventArgs)2 IEventListener (org.ovirt.engine.ui.uicompat.IEventListener)2 TaskModelProvider (org.ovirt.engine.ui.webadmin.uicommon.model.TaskModelProvider)2