public async Task DeliverBlast(EmailBlast blast, CancellationToken cancellationToken) { _logger.LogInformation($"Processing email blast with id {blast.Id}"); if (blast.Template == null) { throw new Exception($"Could not send blast with id {blast.Id}: template id was null"); } if (blast.Customer == null) { throw new Exception($"Could not send blast with id {blast.Id}: customer id was null"); } var template = await _templateRepository.GetByIdAsync(blast.Template, cancellationToken); if (template == null) { await MarkBlastAsErrored(blast, cancellationToken); throw new Exception($"Could not send blast with id {blast.Id}: template with " + $"id {blast.Template} not found"); } var customer = await _customerRepository.GetByIdAsync(blast.Customer, cancellationToken); if (customer == null) { await MarkBlastAsErrored(blast, cancellationToken); throw new Exception($"Could not send email blast with id {blast.Id}: " + $"customer not found"); } try { _logger.LogInformation($"Delivering email blast with template '{template.Name}' " + $"for customer '{customer.FirstName} {customer.LastName}'"); var recipients = await _recipientRepository.GetRecipientsForCustomer(blast.Customer, cancellationToken); await ProcessBlast(template, customer, recipients, cancellationToken); await MarkBlastAsDelivered(blast, recipients.Count, cancellationToken); } catch (Exception ex) { _logger.LogError(ex, $"Could not send email blast for blast {blast.Id}"); await MarkBlastAsErrored(blast, cancellationToken); } }
public async Task ExecuteAsync(CancellationToken cancellationToken) { while (cancellationToken.WaitHandle.WaitOne(5000) == false) { var pendingBlasts = await _emailBlastRepository.GetPendingEmailBlastsAsync(cancellationToken); foreach (var blast in pendingBlasts) { _logger.LogInformation($"Processing email blast with id {blast.Id}"); if (blast.Template == null) { _logger.LogWarning($"Could not send blast with id {blast.Id}: template id was null"); continue; } if (blast.Customer == null) { _logger.LogWarning($"Could not send blast with id {blast.Id}: customer id was null"); continue; } try { var template = await _templateRepository.GetByIdAsync(blast.Template, cancellationToken); if (template == null) { _logger.LogWarning( $"Could not send blast with id {blast.Id}: template with " + $"id {blast.Template} not found"); await MarkBlastAsErrored(blast, cancellationToken); continue; } var customer = await _customerRepository.GetByIdAsync(blast.Customer, cancellationToken); if (customer == null) { _logger.LogWarning($"Could not send email blast with id {blast.Id}: " + $"customer not found"); await MarkBlastAsErrored(blast, cancellationToken); continue; } _logger.LogInformation($"Delivering email blast with template '{template.Name}' " + $"for customer '{customer.FirstName} {customer.LastName}'"); var recipients = await _recipientRepository.GetRecipientsForCustomer(blast.Customer, cancellationToken); await ProcessBlast(template, customer, recipients, cancellationToken); await MarkBlastAsDelivered(blast, recipients.Count, cancellationToken); } catch (Exception ex) { _logger.LogError(ex, $"Could not send email blast for blast {blast.Id}"); await MarkBlastAsErrored(blast, cancellationToken); } } } }