public async Task ProcessAsync(CallEmailImportCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken) { progress.Report(1, $"Starting the {Name} Task"); try { //await Task.Run(async () => //{ var _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); var logic = new CallEmailImporterLogic(); //var items = await _departmentsService.GetAllDepartmentEmailSettingsAsync(); var items = new List <DepartmentCallEmail>(); if (items != null) { _logger.LogInformation("CallEmailImport::Email Import Settings: " + items.Count); foreach (var i in items) { var cqi = new CallEmailQueueItem(); cqi.EmailSettings = i; _logger.LogInformation("CallEmailImport::Processing Email for DepartmentCallEmailId:" + cqi.EmailSettings.DepartmentCallEmailId); var result = await logic.Process(cqi); if (result.Item1) { _logger.LogInformation($"CallEmailImport::Processed Email Import {cqi.EmailSettings.DepartmentCallEmailId} successfully."); } else { _logger.LogInformation($"CallEmailImport::Failed to Processed Email Import {cqi.EmailSettings.DepartmentCallEmailId} error {result.Item2}"); } } } //}, cancellationToken); progress.Report(100, $"Finishing the {Name} Task"); } catch (Exception ex) { Resgrid.Framework.Logging.LogException(ex); _logger.LogError(ex.ToString()); } }
public async Task ProcessAsync(CallEmailImportCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken) { progress.Report(1, $"Starting the {Name} Task"); await Task.Factory.StartNew(() => { var _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); var logic = new CallEmailImporterLogic(); var items = _departmentsService.GetAllDepartmentEmailSettings(); if (items != null) { _logger.LogInformation("CallEmailImport::Email Import Settings: " + items.Count); foreach (var i in items) { var cqi = new CallEmailQueueItem(); cqi.EmailSettings = i; _logger.LogInformation("CallEmailImport::Processing Email for DepartmentCallEmailId:" + cqi.EmailSettings.DepartmentCallEmailId); var result = logic.Process(cqi); if (result.Item1) { _logger.LogInformation($"CallEmailImport::Processed Email Import {cqi.EmailSettings.DepartmentCallEmailId} successfully."); } else { _logger.LogInformation($"CallEmailImport::Failed to Processed Email Import {cqi.EmailSettings.DepartmentCallEmailId} error {result.Item2}"); } } } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); progress.Report(100, $"Finishing the {Name} Task"); }
public async Task <Tuple <bool, string> > Process(CallEmailQueueItem item) { bool success = true; string result = ""; _callEmailProvider = Bootstrapper.GetKernel().Resolve <ICallEmailProvider>(); if (!String.IsNullOrWhiteSpace(item?.EmailSettings?.Hostname)) { CallEmailsResult emailResult = _callEmailProvider.GetAllCallEmailsFromServer(item.EmailSettings); if (emailResult?.Emails != null && emailResult.Emails.Count > 0) { var calls = new List <Call>(); _callsService = Bootstrapper.GetKernel().Resolve <ICallsService>(); _queueService = Bootstrapper.GetKernel().Resolve <IQueueService>(); _departmentsService = Bootstrapper.GetKernel().Resolve <IDepartmentsService>(); _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); _departmentSettingsService = Bootstrapper.GetKernel().Resolve <IDepartmentSettingsService>(); _unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>(); // Ran into an issue where the department users didn't come back. We can't put the email back in the POP // email box so just added some simple retry logic here. List <IdentityUser> departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true); var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(item.EmailSettings.DepartmentId); int retry = 0; while (retry < 3 && departmentUsers == null) { Thread.Sleep(150); departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(item.EmailSettings.DepartmentId, true); retry++; } foreach (var email in emailResult.Emails) { var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(item.EmailSettings.Department.DepartmentId); var units = await _unitsService.GetUnitsForDepartmentAsync(item.EmailSettings.Department.DepartmentId); var priorities = await _callsService.GetActiveCallPrioritiesForDepartmentAsync(item.EmailSettings.Department.DepartmentId); int defaultPriority = (int)CallPriority.High; if (priorities != null && priorities.Any()) { var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false); if (defaultPrio != null) { defaultPriority = defaultPrio.DepartmentCallPriorityId; } } var call = _callsService.GenerateCallFromEmail(item.EmailSettings.FormatType, email, item.EmailSettings.Department.ManagingUserId, departmentUsers, item.EmailSettings.Department, activeCalls, units, defaultPriority, priorities); if (call != null) { call.DepartmentId = item.EmailSettings.DepartmentId; if (!calls.Any(x => x.Name == call.Name && x.NatureOfCall == call.NatureOfCall)) { calls.Add(call); } } } if (calls.Any()) { var departmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(item.EmailSettings.DepartmentId); foreach (var call in calls) { try { // Adding this in here to try and fix the error below with ObjectContext issues. var newCall = CreateNewCallFromCall(call); if (newCall.Dispatches != null && newCall.Dispatches.Any()) { // We've been having this error here: // The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects. // So I'm wrapping this in a try catch to prevent all calls form being dropped. var savedCall = await _callsService.SaveCallAsync(newCall); var cqi = new CallQueueItem(); cqi.Call = savedCall; cqi.Profiles = profiles.Values.ToList(); cqi.DepartmentTextNumber = departmentTextNumber; await _queueService.EnqueueCallBroadcastAsync(cqi); } } catch (Exception ex) { result = ex.ToString(); Logging.LogException(ex); } } } await _departmentsService.SaveDepartmentEmailSettingsAsync(emailResult.EmailSettings); _callsService = null; _queueService = null; _departmentsService = null; _callEmailProvider = null; _userProfileService = null; _departmentSettingsService = null; } } return(new Tuple <bool, string>(success, result)); }