private async Task ProcessMessagesAsync() { var messages = await _repository.GetOverdueMessagesAsync(DateTime.UtcNow); var removeTasks = new List <Task>(); try { foreach (var message in messages) { _publisher.Produce(message.Message); removeTasks.Add(_repository.RemoveAsync(message.Key)); } } catch (Exception e) { _logger.LogError(e, "Failed to publish messages"); } finally { if (removeTasks.Any()) { await Task.WhenAll(removeTasks); } } }
public override async Task Execute() { var messages = await _repository.GetOverdueMessagesAsync(DateTime.UtcNow); var removeTasks = new List <Task>(); try { foreach (var message in messages) { _publisher.Produce(message.Message); removeTasks.Add(_repository.RemoveAsync(message.Key)); } } finally { if (removeTasks.Any()) { await Task.WhenAll(removeTasks); } } }