public void PublishToSubscriber(string subscriberName, string topic, string payload, Ack ack = Ack.Master) { var collection = _mongoAgent .GetEnvelops(subscriberName) .WithWriteConcern(ack.ToWriteConcern()); collection.InsertOne(new Envelope(topic, payload)); }
public async Task <bool> TrySetProcessingStartedAt(string route, string messageId, CancellationToken cancellationToken) { var collection = _mongoAgent.GetEnvelops(route); var filter = Builders <Envelope> .Filter.And(Builders <Envelope> .Filter.Eq(x => x.Id, messageId), Builders <Envelope> .Filter.Eq(x => x.IsProcessed, false)); var found = await collection.FindOneAndUpdateAsync <Envelope>(filter, Builders <Envelope> .Update.Set(x => x.ProcessingStartedAt, DateTime.UtcNow).Set(x => x.IsProcessingStarted, true), cancellationToken : cancellationToken); return(found != null); }
public async Task<List<Envelope>> GetUnprocessed(string route, CancellationToken cancellationToken) { var threshold = DateTime.UtcNow - _messagingConfiguration.ResendThreshold; var collection = _mongoAgent.GetEnvelops(route); var notProcessedFilter = Builders<Envelope>.Filter.And( Builders<Envelope>.Filter.Lt(x => x.ReadAt, threshold), Builders<Envelope>.Filter.Eq(x => x.IsRead, true), Builders<Envelope>.Filter.Eq(x => x.IsProcessed, false) ); var notProcessed = await (await collection.FindAsync(notProcessedFilter, cancellationToken: cancellationToken)) .ToListAsync( cancellationToken); return notProcessed; }
public async Task Listen(string route, CancellationToken cancellationToken) { var notReadFilter = Builders <Envelope> .Filter.Eq(x => x.IsRead, false); var collection = _mongoAgent.GetEnvelops(route); try { while (true) { var messages = await(await collection.FindAsync(notReadFilter, null, cancellationToken)).ToListAsync(cancellationToken); { foreach (var message in messages) { var readMessage = await _messageStatusManager.TrySetReadAt(route, message.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) { var collection = _mongoAgent.GetEnvelops(subscriberName); collection.InsertOne(new Envelope(topic, payload)); }