Example #1
0
        public void Handle(PollResponseWasReceived message)
        {
            if (this.producerListIsReady)
            {
                return; // quick return;
            }
            lock (this)
            {
                if (!this.producerListIsReady)
                {
                    if (!this.producers.Any(x => x.ProducerName == message.Response.StreamType))
                    {
                        this.producers.Add(new ProducerPollingStatus(message.Response.StreamType, message.Response.ProducerVersion));
                    }

                    var subs = this.poller.GetSubscriptionsMetrics().ToList();
                    if (subs.TrueForAll(s => this.producers.Any(p => p.ProducerName == s.ProducerName)))
                    {
                        this.producerListIsReady = true;
                    }
                }
            }
        }
Example #2
0
        public void Handle(PollResponseWasReceived message)
        {
            if (this.stopping)
                return;

            var response = message.Response;

            SubscriptionBuffer subscription;
            try
            {
                if (!this.onTheFlySubscriptionsDetected)
                    subscription = this.bufferPool.Where(s => s.StreamType == response.StreamType).Single();
                else
                {
                    subscription = this.bufferPool.Where(s => s.StreamType == response.StreamType).SingleOrDefault();
                    if (subscription == null)
                        subscription = this.onTheFlyBufferPool.Where(s => s.StreamType == response.StreamType).Single();
                }
            }
            catch (Exception ex)
            {
                var errorMessage = $@"An error occurred while receiving poll response message. Check if the stream type matches that of the subscribed source. Remote message type that probably was not found is '{response.StreamType}'";
                this.log.Error(ex, errorMessage);

                this.bus.Publish(
                    new FatalErrorOcurred(
                        new FatalErrorException(errorMessage, ex)));

                throw;
            }

            if (message.Response.ErrorDetected)
            {
                this.log.Error($"An error was detected while polling {message.Response.StreamType}. The error will be ignored and polling will continue with safety.");
                subscription.IsPolling = false;
                return;
            }

            if (response.NewEventsWereFound)
            {
                var orderedEvents =
                    response.IsSerialized

                    // is serialized, then we need to deserialize them
                    ? response
                        .NewRawEvents
                        .Select(e =>
                        {
                            IEvent incomingEvent;

                            try
                            {
                                incomingEvent = this.serializer.Deserialize<IEvent>(e.Payload);
                            }
#if !DEBUG
                            catch (SerializationException)
                            {
#endif
#if DEBUG
                            catch (SerializationException ex)
                            {
                                // Maybe the event source has new events type that we are not aware off.

                                if (this.log.Verbose)
                                    this.log.Error(ex, "An error ocurred while deserializing a message");
                                //this.log.Error($"An error was detected when serializing a message from {buffer.ProducerName} with event collection number of {raw.EventCollectionVersion}. The message will be ignored.");
#endif
                                incomingEvent = new Message();
                            }
                            catch (Exception ex)
                            {
                                var errorMessage = $"An error occurred while trying to deserialize a payload from an incoming event. Check the inner exception for more information. System will shut down.";
                                this.log.Error(ex, errorMessage);

                                this.bus.Publish(
                                    new FatalErrorOcurred(
                                        new FatalErrorException(errorMessage, ex)));

                                throw;
                            }

                            ((Message)incomingEvent).EventCollectionVersion = e.EventCollectionVersion;
                            return incomingEvent;
                        })
                        .OrderBy(e => e.EventCollectionVersion)

                    // Not serialized, we just need to put in order...
                    : response.Events.OrderBy(e => e.EventCollectionVersion);

                subscription.CurrentBufferVersion = orderedEvents.Max(e => e.EventCollectionVersion);

                foreach (var e in orderedEvents)
                {
                    //this.log.Trace("Event: ECVersion {0}", e.EventCollectionVersion);
                    subscription.NewEventsQueue.Enqueue(e);
                }
            }

            subscription.ConsumerVersion = response.ConsumerVersion;
            subscription.ProducerVersion = response.ProducerVersion;

            subscription.IsPolling = false;

            if (this.log.Verbose)
            {
                var messageCount = response.IsSerialized ? response.NewRawEvents.Count() : response.Events.Count();
                if (messageCount > 0)
                    this.log.Trace($"{this.microserviceName} pulled {messageCount} event/s from {subscription.StreamType}");
            }
        }