public void RunQueuedSearchesAsync() { try { var search = courseSearchQueueBll.GetNextSearchToProcess(); if (search != null) { search.Status = SearchStatus.Pending; courseSearchQueueBll.Update(search); try { RunSearch(search.Id, search.StartTermId, search.EndTermId); } catch (Exception ex) { search.Status = SearchStatus.Failed; search.StatusMessage = ex.Message; courseSearchQueueBll.Update(search); } if (search.Status != SearchStatus.Failed) { search.Status = SearchStatus.Completed; courseSearchQueueBll.Update(search); } Dictionary <string, string> subjectReplacements = new Dictionary <string, string>() { { "@Status", Enum.GetName(typeof(SearchStatus), search.Status) } }; sendEmailBll.SendEmail(search.SubmittedByEmail, EmailTemplate.UnusedCourseSearch, new Dictionary <string, string>(), subjectReplacements); } } catch (Exception ex) { FileLogger.Log("RunQueuedSearchesAsync :: " + ex.ToString()); } }
public ActionResult UnusedCoursesReport(SearchQueueResultViewModel viewModel) { var courseSearchQueue = courseSearchQueueBll.Get(viewModel.CourseSearchQueueId); courseSearchQueue.DeleteAllRequested = true; courseSearchQueueBll.Update(courseSearchQueue); var unusedCourses = unusedCourseBll.GetAll() .Where(x => x.CourseSearchQueueId == viewModel.CourseSearchQueueId).ToList(); unusedCourseBll.UpdateStatusRange(unusedCourses, CourseStatus.PendingDeletion); viewModel.UnusedCourses = unusedCourses; return(View()); }
static void ProcessCoursesPendingDeletion(IUnusedCourseBLL unusedCourseBll, ICourseSearchQueueBLL courseSearchQueueBll, ISendEmailBLL sendEmailBll) { DateTime deleteStartTimeStamp = DateTime.Now; var coursesPendingDeletion = unusedCourseBll.GetAll().Where(x => x.Status == CourseStatus.PendingDeletion).ToList(); if (coursesPendingDeletion.Any()) { var coursesByAccount = coursesPendingDeletion.GroupBy(x => x.AccountId); var coursesDeleted = 0; var errors = new List <string>(); foreach (var courseList in coursesByAccount) { var courseChunks = courseList.ToList().ChunkBy(MAXCOURSESTHATCANBEMODIFIED); foreach (var courseChunk in courseChunks) { var courseIds = courseChunk.Select(x => x.CourseId); var form = new MultipartFormDataContent(); foreach (var courseId in courseIds) { form.Add(new StringContent(courseId), "course_ids[]"); } form.Add(new StringContent("delete"), "event"); var result = client.PutAsync($"accounts/{courseList.Key}/courses", form).GetAwaiter().GetResult(); if (result.IsSuccessStatusCode) { coursesDeleted += courseChunk.Count; foreach (var unusedCourse in courseChunk) { unusedCourse.Status = CourseStatus.Deleted; } unusedCourseBll.UpdateRange(courseChunk); } else { string error = $"{(int)result.StatusCode}: {result.ReasonPhrase}. {result.RequestMessage.RequestUri}"; if (!errors.Contains(error)) { errors.Add(error); } } } } var deleteEndTimeStamp = DateTime.Now; var courseSearchQueueIds = coursesPendingDeletion.Select(x => x.CourseSearchQueueId).Distinct().ToList(); foreach (var courseSearchQueueId in courseSearchQueueIds) { // Send email if DELETE ALL was requested var courseSearchQueue = courseSearchQueueBll.Get(courseSearchQueueId); if (courseSearchQueue.DeleteAllRequested) { sendEmailBll.SendBatchDeleteCoursesCompletedEmailAsync(deleteStartTimeStamp, deleteEndTimeStamp, coursesDeleted, errors, courseSearchQueue.SubmittedByEmail).GetAwaiter().GetResult(); courseSearchQueue.DeleteAllRequested = false; courseSearchQueueBll.Update(courseSearchQueue); } } } }