/// <inheritdoc/> public async Task IssueContractReminders() { await _auditService.TrySendAuditAsync( new Audit.Api.Client.Models.Audit { Severity = 0, Action = Audit.Api.Client.Enumerations.ActionType.ContractEmailReminderQueued, Ukprn = null, Message = $"Contract Reminder function has been triggered.", User = Audit_User_System }); ContractReminders reminders = null; int remindersSent = 0; do { reminders = await _contractNotificationService.GetOverdueContracts(); if (reminders?.Contracts?.Count > 0) { _logger.LogInformation($"Processing a list of contracts with {reminders.Contracts.Count} records."); IList <Task> tasks = new List <Task>(); foreach (var contract in reminders.Contracts) { var reminderProcess = Task.Run(async() => { try { _logger.LogInformation($"Starting processing contract with id {contract.Id}."); await _contractNotificationService.QueueContractEmailReminderMessage(contract); await _contractNotificationService.NotifyContractReminderSent(contract); _logger.LogInformation($"Completed processing for contract with id {contract.Id}."); } catch (Exception e) { _logger.LogError(e, $"Contract with id [{contract.Id}] failed to process successfully."); throw; } }); tasks.Add(reminderProcess); } try { await Task.WhenAll(tasks); } catch (Exception e) { _logger.LogError(e, "Error processign one or more records."); } if (tasks.All(p => !p.IsCompletedSuccessfully)) { _logger.LogCritical("All processes failed to complete successfully. Aborting..."); _logger.LogInformation($"Contract reminders processes aborted. Sent {remindersSent} reminders prior to abort."); throw new ContractProcessingException("All processes failed to complete successfully.", tasks.First(p => !p.IsCompletedSuccessfully).Exception); } else { remindersSent += tasks.Count(p => p.IsCompletedSuccessfully); } } }while (reminders?.Contracts?.Count > 0); { await _auditService.TrySendAuditAsync( new Audit.Api.Client.Models.Audit { Severity = 0, Action = Audit.Api.Client.Enumerations.ActionType.ContractEmailReminderQueued, Ukprn = null, Message = $"Contract Reminder function has completed. {remindersSent} contract email reminder(s) processed.", User = Audit_User_System }); _logger.LogInformation($"Contract reminders process completed. Sent {remindersSent} reminders."); } }