public async Task <IActionResult> GetJobById(string jobId, bool includeChildJobs) { Guard.IsNullOrWhiteSpace(jobId, nameof(jobId)); Job job = await _jobsRepositoryPolicy.ExecuteAsync(() => _jobRepository.GetJobById(jobId)); if (job == null) { return(new NotFoundResult()); } JobViewModel jobViewModel = _mapper.Map <JobViewModel>(job); IEnumerable <Job> childJobs = _jobsRepositoryNonAsyncPolicy.Execute(() => _jobRepository.GetChildJobsForParent(jobId)); if (!childJobs.IsNullOrEmpty()) { foreach (Job childJob in childJobs) { jobViewModel.ChildJobs.Add(_mapper.Map <JobViewModel>(childJob)); } } return(new OkObjectResult(jobViewModel)); }
public async Task <IEnumerable <JobDefinition> > GetAllJobDefinitions() { IEnumerable <JobDefinition> jobDefinitions = await _cachePolicy.ExecuteAsync(() => _cacheProvider.GetAsync <List <JobDefinition> >(CacheKeys.JobDefinitions)); if (!jobDefinitions.IsNullOrEmpty()) { return(jobDefinitions); } jobDefinitions = _jobDefinitionsRepositoryNonAsyncPolicy.Execute(() => _jobDefinitionsRepository.GetJobDefinitions()); if (!jobDefinitions.IsNullOrEmpty()) { await _cachePolicy.ExecuteAsync(() => _cacheProvider.SetAsync(CacheKeys.JobDefinitions, jobDefinitions.ToList())); } return(jobDefinitions); }
public async Task ProcessJobNotification(Message message) { Guard.ArgumentNotNull(message, nameof(message)); // When a job completes see if the parent job can be completed JobNotification jobNotification = message.GetPayloadAsInstanceOf <JobNotification>(); Guard.ArgumentNotNull(jobNotification, "message payload"); if (jobNotification.RunningStatus == RunningStatus.Completed) { if (!message.UserProperties.ContainsKey("jobId")) { _logger.Error("Job Notification message has no JobId"); return; } string jobId = message.UserProperties["jobId"].ToString(); Job job = await _jobsRepositoryPolicy.ExecuteAsync(() => _jobRepository.GetJobById(jobId)); if (job == null) { _logger.Error("Could not find job with id {JobId}", jobId); return; } if (!string.IsNullOrEmpty(job.ParentJobId)) { IEnumerable <Job> childJobs = _jobsRepositoryNonAsyncPolicy.Execute(() => _jobRepository.GetChildJobsForParent(job.ParentJobId)); if (!childJobs.IsNullOrEmpty() && childJobs.All(j => j.RunningStatus == RunningStatus.Completed)) { await semaphoreSlim.WaitAsync(); try { Job parentJob = await _jobsRepositoryPolicy.ExecuteAsync(() => _jobRepository.GetJobById(job.ParentJobId)); if (parentJob.RunningStatus != RunningStatus.Completed) { parentJob.Completed = DateTimeOffset.UtcNow; parentJob.RunningStatus = RunningStatus.Completed; parentJob.CompletionStatus = DetermineCompletionStatus(childJobs); parentJob.Outcome = "All child jobs completed"; await _jobsRepositoryPolicy.ExecuteAsync(() => _jobRepository.UpdateJob(parentJob)); _logger.Information("Parent Job {ParentJobId} of Completed Job {JobId} has been completed because all child jobs are now complete", job.ParentJobId, jobId); await _notificationService.SendNotification(CreateJobNotificationFromJob(parentJob)); } } finally { semaphoreSlim.Release(); } } else { _logger.Information("Completed Job {JobId} parent {ParentJobId} has in progress child jobs and cannot be completed", jobId, job.ParentJobId); } } else { _logger.Information("Completed Job {JobId} has no parent", jobId); } } }