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); } } }
/// <summary> /// Publish <paramref name="message"/> to underlying queue /// </summary> /// <param name="message">Message to publish</param> /// <param name="routingKey">Message routing key. Overrides routing key, which specified in the publisher settings</param> /// <remarks>Method is not thread safe</remarks> /// <returns>Task to await</returns> /// <exception cref="RabbitMqBrokerException">Some error occurred while publishing</exception> public Task ProduceAsync(TMessageModel message, string routingKey) { ThrowIfNotStarted(); if (message == null) { throw new ArgumentNullException(nameof(message)); } var body = _serializer.Serialize(message); _rawPublisher.Produce(new RawMessage(body, routingKey)); return(Task.CompletedTask); }
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); } } }