Esempio n. 1
0
        public async Task <List <Envelope> > GetUnprocessed(string route, CancellationToken cancellationToken)
        {
            var threshold         = DateTime.UtcNow - _messagingConfiguration.ResendThreshold;
            var collection        = _mongoAgent.GetEnvelops(route);
            var notProcessedQuery = Query.And(
                Query <Envelope> .LT(x => x.ReadAt, threshold),
                Query <Envelope> .EQ(x => x.IsRead, true),
                Query <Envelope> .EQ(x => x.IsProcessed, false)
                );
            var notProcessed = collection.Find(notProcessedQuery).ToList();

            _messagingLogger.Debug($"got {notProcessed.Count} unprocessed messages to resend");
            return(notProcessed);
        }
        public async Task <bool> TrySetProcessingStartedAt(string route, string messageId, CancellationToken cancellationToken)
        {
            var collection = _mongoAgent.GetEnvelops(route);
            var query      = Query.And(Query <Envelope> .EQ(x => x.Id, messageId), Query <Envelope> .EQ(x => x.IsProcessed, false));
            var update     = Update <Envelope> .Set(x => x.ProcessingStartedAt, DateTime.UtcNow).Set(x => x.IsProcessingStarted, true);

            var result = collection.FindAndModify(new FindAndModifyArgs
            {
                Query           = query,
                Update          = update,
                VersionReturned = FindAndModifyDocumentVersion.Modified,
                Upsert          = false,
            });

            return(result.ModifiedDocument != null);
        }
Esempio n. 3
0
        public async Task Listen(string route, CancellationToken cancellationToken)
        {
            var notReadQuery = Query <Envelope> .EQ(x => x.IsRead, false);

            var collection = _mongoAgent.GetEnvelops(route);

            try
            {
                while (true)
                {
                    var messages = collection.Find(notReadQuery).ToList();
                    foreach (var envelope in messages)
                    {
                        var readMessage = await _messageStatusManager.TrySetReadAt(route, envelope.Id, cancellationToken);

                        if (readMessage != null)
                        {
                            var resend = readMessage.OriginalId != IdGenerator.Empty;
                            _messageProcessor.Process(route, readMessage.Id, readMessage.Topic, readMessage.Payload, resend, cancellationToken);
                        }
                    }
                    if (!messages.Any())
                    {
                        await Task.Delay(100, cancellationToken);
                    }
                }
            }
            catch (MongoCommandException mongoCommandException)
            {
                if (mongoCommandException.Code == 96)
                {
                    _messagingLogger.Error(mongoCommandException,
                                           $"{route} reader processes messages slower than they occur");
                }
                else
                {
                    _messagingLogger.Error(mongoCommandException, $"{route}");
                }
            }
        }
        public void PublishToSubscriber(string subscriberName, string topic, string payload, Ack ack = Ack.Master)
        {
            var collection = _mongoAgent.GetEnvelops(subscriberName);

            collection.Insert(new Envelope(topic, payload), ack.ToWriteConcern());
        }
Esempio n. 5
0
        public void PublishToSubscriber(string subscriberName, string topic, string payload)
        {
            var collection = _mongoAgent.GetEnvelops(subscriberName);

            collection.Insert(new Envelope(topic, payload));
        }