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 void Process(string route, string messageId, string topic, string payload, bool resend, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); try { using (var scope = _instanceResolver.CreateLifeTimeScope()) { var type = _messageTypesCache.Get(topic); var message = JsonConvert.DeserializeObject(payload, type); var handlerType = _messageHandlersCache.Get(topic); var handlerInstance = (IHandler)scope.ServiceProvider.GetService(handlerType); await handlerInstance.Handle(route, messageId, message, resend, cancellationToken); } } catch (Exception e) { _messagingLogger.Error(e); } finally { _messagingLogger.Debug($"{messageId} processed in {sw.ElapsedMilliseconds}"); } }
private async Task RepeatEvery(Func <Task> action, TimeSpan interval) { while (true) { try { _messagingLogger.Debug("starting resending cycle"); await action(); await Task.Delay(interval); _messagingLogger.Debug("resending cycle ended"); } catch (TaskCanceledException) { return; } } }
public void Publish <TMessage>(TMessage message) { var sw = Stopwatch.StartNew(); var topic = _topicNameProvider.Get <TMessage>(); var subscribers = _subscriptionAgent.GetSubscribers(topic); if (subscribers != null && subscribers.Any()) { foreach (var subscriber in subscribers) { var payload = JsonConvert.SerializeObject(message); _publishingAgent.PublishToSubscriber(subscriber.Name, topic, payload); } } else { _messagingLogger.Debug($"no subsriptions for {topic}"); } _messagingLogger.Debug($"{topic} sent in {sw.ElapsedMilliseconds}"); }
public async Task PublishAsync(string topic, object message) { var sw = Stopwatch.StartNew(); var subscribers = await _subscriptionAgent.GetSubscribersAsync(topic); var payload = JsonConvert.SerializeObject(message); if (subscribers != null && subscribers.Any()) { foreach (var subscriber in subscribers) { await _publishingAgent.PublishToSubscriberAsync(subscriber.Name, topic, payload); } } else { _messagingLogger.Debug($"no subsriptions for {topic}"); await _deadLettersAgent.PublishAsync(topic, payload); } _messagingLogger.Debug($"{topic} sent in {sw.ElapsedMilliseconds}"); }