Exemple #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                if (!await _reader.Ensure(stoppingToken))
                {
                    _logger.LogError("Cannot ensure that Azure storage queue exists.");
                    return;
                }

                if (!await _writer.Verify(stoppingToken))
                {
                    _logger.LogError("Cannot verify MongoDB database connection.");
                    return;
                }

                while (!stoppingToken.IsCancellationRequested)
                {
                    var messages = _reader.GetChanges(stoppingToken);

                    await foreach (var message in messages.WithCancellation(stoppingToken))
                    {
                        var change = message.Change;
                        _logger.LogInformation("a1. Process Mutation with Entity ID: {entityId}; MutationId: {mutationId}", change.EntityId, change.MutationId);
                        if (await _writer.Append(change, stoppingToken))
                        {
                            _logger.LogInformation("a2. Successful processed Entity ID: {entityId}; MutationId: {mutationId}",
                                                   change.EntityId, change.MutationId);
                            await _reader.DeleteMessage(message, stoppingToken);
                        }
                        else
                        {
                            _logger.LogWarning("a2. Cannot process Entity ID: {entityId}; MutationId: {mutationId}", change.EntityId, change.MutationId);
                        }
                    }

                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                    await Task.Delay(100, stoppingToken);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
        }
 void ReceiveMessages(CancellationToken outer, IEnumerable <string> queues)
 {
     using (var source = CancellationTokenSource.CreateLinkedTokenSource(_disposal.Token, outer))
     {
         var             token = source.Token;
         MessageEnvelope envelope;
         foreach (var queue in queues)
         {
             while (_queueReader.TakeMessage(queue, token, out envelope))
             {
                 try
                 {
                     var msg       = envelope.MessageReference.Message;
                     var consumers = _consumerFactory.GetMessageConsumers(msg);
                     foreach (var consumer in consumers)
                     {
                         _observer.Notify(new ConsumeBegin(msg, consumer, envelope.QueueName));
                         consumer.Consume(msg);
                         _observer.Notify(new ConsumeEnd(msg, consumer, envelope.QueueName));
                     }
                 }
                 catch (Exception ex)
                 {
                     _observer.Notify(new FailedToConsumeMessage(ex, envelope.EnvelopeId, envelope.QueueName));
                 }
                 try
                 {
                     _queueReader.DeleteMessage(envelope);
                 }
                 catch (Exception ex)
                 {
                     // not a big deal. Message will be processed again.
                     _observer.Notify(new FailedToAckMessage(ex, envelope.EnvelopeId, envelope.QueueName));
                 }
             }
         }
     }
 }