/** * Creates {@code ExecutionContext} which defines the context for executing the finalizing step of * the job. If the step exists, it must be part of a job, therefore the {@code Job} entity is * being set as part of the context. * * @param stepId The unique identifier of the step. Must not be {@code null}. * @return The context for monitoring the finalizing step of the job, or {@code null} if no such * step. */ public static ExecutionContext createFinalizingContext(Guid stepId) { ExecutionContext context = null; try { Step step = JobRepositoryFactory.getJobRepository().getStep(stepId); if (step != null && step.getParentStepId() != null) { context = new ExecutionContext(); Step executionStep = JobRepositoryFactory.getJobRepository().getStep(step.getParentStepId()); // indicates if a step is monitored at Job level or as an inner step Guid parentStepId = executionStep.getParentStepId(); if (parentStepId == null) { context.setExecutionMethod(ExecutionMethod.AsJob); context.setJob(JobRepositoryFactory.getJobRepository().getJobWithSteps(step.getJobId())); } else { context.setExecutionMethod(ExecutionMethod.AsStep); Step parentStep = JobRepositoryFactory.getJobRepository().getStep(parentStepId); parentStep.setSteps( DbFacade.getInstance().getStepDao().getStepsByParentStepId(parentStep.getId())); context.setStep(parentStep); } context.setMonitored(true); } } catch (Exception e) { log.error(e); } return context; }
/** * Gets a list of {@link Step} entities ordered by: * <li>parent step id, preceded by nulls * <li>step number * * @param steps * @return a collection of the steps. */ private List<Step> buildStepsTree(List<Step> steps) { List<Step> jobDirectSteps = new ArrayList<Step>(); // a map of parent step id and a list of child-steps Map<NGuid, List<Step>> parentStepMap = new HashMap<NGuid, List<Step>>(); for (Step step : steps) { if (step.getParentStepId() == null) { jobDirectSteps.add(step); } else { MultiValueMapUtils.addToMap(step.getParentStepId(), step, parentStepMap); } } for (Step step : steps) { step.setSteps(parentStepMap.get(step.getId())); } return jobDirectSteps; }
/** * Finalizes Job with VDSM tasks, as this case requires verification that no other steps are * running in order to close the entire Job * * @param executionContext 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 static void endTaskJob(ExecutionContext context, boolean exitStatus) { if (context == null) { return; } try { if (context.getExecutionMethod() == ExecutionMethod.AsJob && context.getJob() != null) { endJob(context, exitStatus); } else { Step parentStep = context.getStep(); if (context.getExecutionMethod() == ExecutionMethod.AsStep && parentStep != null) { Step finalizingStep = parentStep.getStep(StepEnum.FINALIZING); if (finalizingStep != null) { finalizingStep.markStepEnded(exitStatus); JobRepositoryFactory.getJobRepository().updateStep(finalizingStep); } parentStep.markStepEnded(exitStatus); JobRepositoryFactory.getJobRepository().updateStep(parentStep); List<Step> steps = DbFacade.getInstance().getStepDao().getStepsByJobId(parentStep.getJobId()); boolean hasChildStepsRunning = false; for (Step step : steps) { if (step.getStatus() == JobExecutionStatus.STARTED && step.getParentStepId() != null) { hasChildStepsRunning = true; break; } } if (!hasChildStepsRunning) { endJob( exitStatus, JobRepositoryFactory.getJobRepository().getJob(parentStep.getJobId())); } } } } catch (RuntimeException e) { log.error(e); } }