/// <remarks>
        /// Timeout implementation inspired by: http://stackoverflow.com/questions/5018921/implement-c-sharp-timeout
        /// </remarks>
        public PollResponse PollEvents(long consumerVersion, string consumerName)
        {
            var ecv = this.eventCollectionVersion;

            var newEvents = new List <NewRawEvent>();

            // last received version could be somehow less than 0. I found once that was -1,
            // and was always pushing "0 events", as the signal r tracing showed (27/10/2015)
            if (consumerVersion < 0)
            {
                consumerVersion = 0;
            }

            // the consumer says that is more updated than the source. That is an error. Maybe the publisher did not started yet!
            if (ecv < consumerVersion)
            {
                return(PollResponse.CreateSerializedResponse(true, false, this.streamType, newEvents, consumerVersion, ecv));
            }

            bool newEventsWereFound = false;
            var  stopwatch          = Stopwatch.StartNew();

            while (!this.stopping && stopwatch.Elapsed < this.longPollingTimeout)
            {
                if (ecv == consumerVersion)
                {
                    // consumer is up to date, and now is waiting until something happens!
                    Thread.Sleep(1);
                    ecv = this.eventCollectionVersion; // I Forgot to update!
                }
                // weird error, but is crash proof. Once i had an error where in an infinite loop there was an error saying: Pushing 0 events to....
                // A Charly le paso. Sucede que limpio la base de datos y justo queria entregar un evento y no devolvia nada.
                else if (ecv > consumerVersion)
                {
                    newEvents = this.dao.FindEvents(consumerVersion, eventsToPushMaxCount);

                    if (newEvents.Count > 0)
                    {
                        newEventsWereFound = true;
                        this.log.Trace($"{this.SourceName} publisher is pushing {newEvents.Count} event/s to {consumerName}");
                        break;
                    }
                    else
                    {
                        // Lo que le paso a charly.
                        newEventsWereFound = false;
                        //this.log.Error($"There is an error in the event store or a racy condition. The consumer [{consumerName}] version is {consumerVersion} and the local event collection version should be {this.eventCollectionVersion} but it is not.");
                        this.bus.Publish(new FatalErrorOcurred(new FatalErrorException($"There is an error in the event store or a racy condition. The consumer [{consumerName}] version is {consumerVersion} and the local event collection version should be {this.eventCollectionVersion} but it is not.")));
                        break;
                    }
                }
                else
                {
                    // bizzare, but helpful to avoid infinite loops
                    break;
                }
            }

            return(PollResponse.CreateSerializedResponse(false, newEventsWereFound, this.streamType, newEvents, consumerVersion, ecv));
        }
Beispiel #2
0
        public PollResponse PollEvents(long eventBufferVersion, string consumerName)
        {
            this.consumerEventCollectionVersion = eventBufferVersion < 0 ? 0 : eventBufferVersion;

            PollResponse clientResponse;

            while (!this.producerResponse.TryTake(out clientResponse))
            {
                Thread.Sleep(1);
            }

            if (!clientResponse.ErrorDetected &&
                clientResponse.NewEventsWereFound &&
                clientResponse.ConsumerVersion == this.consumerEventCollectionVersion && // only updates if versions are equall. Nasty errors ocurred while trying to synchronize.
                clientResponse.ProducerVersion > eventBufferVersion &&
                clientResponse.NewRawEvents.Max(x => x.EventCollectionVersion) > eventBufferVersion)
            {
                clientResponse = PollResponse.CreateSerializedResponse(false, true, clientResponse.StreamType,
                                                                       clientResponse.NewRawEvents.Where(x => x.EventCollectionVersion > eventBufferVersion).ToList(), eventBufferVersion, clientResponse.ProducerVersion);
            }
            else
            {
                clientResponse = PollResponse.CreateSerializedResponse(false, false, clientResponse.StreamType, new List <SerializedEvent>(), 0, 0);
            }

            return(clientResponse);
        }
Beispiel #3
0
        public PollResponse PollEvents(long eventBufferVersion, string consumerName)
        {
            this.serverStatus.Add(new ServerStatus(eventBufferVersion));

            PollResponse clientResponse;

            while (!this.clientResponse.TryTake(out clientResponse))
            {
                Thread.Sleep(1);
            }

            lock (this)
            {
                if (clientResponse.ProducerVersion > eventBufferVersion &&
                    clientResponse.NewRawEvents.Max(x => x.EventCollectionVersion) > eventBufferVersion)
                {
                    clientResponse = PollResponse.CreateSerializedResponse(false, true, clientResponse.StreamType,
                                                                           clientResponse.NewRawEvents.Where(x => x.EventCollectionVersion > eventBufferVersion).ToList(), eventBufferVersion, clientResponse.ProducerVersion);
                }
                else
                {
                    clientResponse = PollResponse.CreateSerializedResponse(false, false, clientResponse.StreamType, new List <NewRawEvent>(), 0, 0);
                }

                return(clientResponse);
            }
        }
        public PollResponse PollEvents(Guid streamId, long consumerVersion, string consumerName)
        {
            var ecv = this.store.CurrentEventCollectionVersion;

            var newEvents = new SerializedEvent[0];

            // last received version could be somehow less than 0. I found once that was -1,
            // and was always pushing "0 events", as the signal r tracing showed (27/10/2015)
            if (consumerVersion < 0)
            {
                consumerVersion = 0;
            }

            // the consumer says that is more updated than the source. That is an error. Maybe the publisher did not started yet!
            if (ecv < consumerVersion)
            {
                return(PollResponse.CreateSerializedResponse(true, false, this.streamType, newEvents, consumerVersion, ecv));
            }

            bool newEventsWereFound = false;
            var  errorDetected      = false;
            var  stopwatch          = Stopwatch.StartNew();

            while (!this.stopping && stopwatch.Elapsed < this.longPollingTimeout)
            {
                if (ecv == consumerVersion)
                {
                    // consumer is up to date, and now is waiting until something happens!
                    Thread.Sleep(1);
                    ecv = this.store.CurrentEventCollectionVersion; // I Forgot to update!
                }
                // weird error, but is crash proof. Once i had an error where in an infinite loop there was an error saying: Pushing 0 events to....
                // A Charly le paso. Sucede que limpio la base de datos y justo queria entregar un evento y no devolvia nada.
                else if (ecv > consumerVersion)
                {
                    newEvents = this.store.FindEventsForConsumer(consumerVersion, ecv, streamId, this.eventsToPushMaxCount, consumerName);

                    newEventsWereFound = true;
                    this.log.Trace($"{this.streamType} publisher is pushing {newEvents.Length} event/s to {consumerName}");
                    break;
                }
                else
                {
                    // bizzare, but helpful to avoid infinite loops
                    break;
                }
            }

            return(PollResponse.CreateSerializedResponse(errorDetected, newEventsWereFound, this.streamType, newEvents, consumerVersion, ecv));
        }