Example #1
0
        /// <summary>
        /// Creates JobExecution.
        /// </summary>
        /// <param name="jobName"></param>
        /// <param name="jobParameters"></param>
        /// <returns></returns>
        public JobExecution CreateJobExecution(string jobName, JobParameters jobParameters)
        {
            Assert.NotNull(jobName, "Job name must not be null.");
            Assert.NotNull(jobParameters, "JobParameters must not be null.");
            var jobInstance = _jobInstanceDao.GetJobInstance(jobName, jobParameters);
            ExecutionContext executionContext;

            // existing job instance found
            if (jobInstance != null)
            {
                var executions = _jobExecutionDao.FindJobExecutions(jobInstance);

                // check for running executions and find the last started
                foreach (var execution in executions)
                {
                    if (execution.IsRunning())
                    {
                        throw new JobExecutionAlreadyRunningException(string.Format("A job execution for this job is already running: {0}",
                                                                                    jobInstance));
                    }

                    var status = execution.Status;
                    if (execution.JobParameters.GetParameters().Any() &&
                        (status == BatchStatus.Completed || status == BatchStatus.Abandoned))
                    {
                        throw new JobInstanceAlreadyCompleteException(string.Format(
                                                                          "A job instance already exists and is complete for parameters={0}"
                                                                          + ".  If you want to run this job again, change the parameters.", jobParameters));
                    }
                }
                executionContext = _executionContextDao.GetExecutionContext(_jobExecutionDao.GetLastJobExecution(jobInstance));
            }
            else
            {
                // no job found, create one
                jobInstance      = _jobInstanceDao.CreateJobInstance(jobName, jobParameters);
                executionContext = new ExecutionContext();
            }

            var jobExecution = new JobExecution(jobInstance, jobParameters, null)
            {
                ExecutionContext = executionContext,
                LastUpdated      = DateTime.Now
            };

            // Save the JobExecution so that it picks up an ID (useful for clients
            // monitoring asynchronous executions):
            _jobExecutionDao.SaveJobExecution(jobExecution);
            _executionContextDao.SaveExecutionContext(jobExecution);

            return(jobExecution);
        }
        /// <summary>
        /// Retrieves job executions by their job instance. The corresponding step
        /// executions may not be fully hydrated (e.g. their execution context may be
        /// missing), depending on the implementation. Use
        /// GetStepExecution(Long, Long) to hydrate them in that case.
        /// </summary>
        /// <param name="jobInstance">the JobInstance to query</param>
        /// <returns>the set of all executions for the specified JobInstance</returns>
        public IList <JobExecution> GetJobExecutions(JobInstance jobInstance)
        {
            IList <JobExecution> executions = _jobExecutionDao.FindJobExecutions(jobInstance);

            foreach (JobExecution jobExecution in executions)
            {
                GetJobExecutionDependencies(jobExecution);
                foreach (StepExecution stepExecution in jobExecution.StepExecutions)
                {
                    GetStepExecutionDependencies(stepExecution);
                }
            }
            return(executions);
        }