示例#1
0
        private async Task EventAppeared(EventStorePersistentSubscriptionBase sub, ResolvedEvent e, CancellationToken token,
                                         Func <string, long, IFullEvent, Task> callback)
        {
            // Don't care about metadata streams
            if (e.Event == null || e.Event.EventStreamId[0] == '$')
            {
                sub.Acknowledge(e.OriginalEvent.EventId);
                return;
            }

            if (token.IsCancellationRequested)
            {
                Logger.WarnEvent("Cancelation", "Token cancel requested");
                ThreadPool.QueueUserWorkItem((_) => sub.Stop(TimeSpan.FromSeconds(10)));
                token.ThrowIfCancellationRequested();
            }

            var eventId = $"{e.Event.EventId}:{e.Event.EventStreamId}:{e.Event.EventNumber}";

            _outstandingEvents[eventId] = new Tuple <EventStorePersistentSubscriptionBase, Guid>(sub, e.OriginalEvent.EventId);

            try
            {
                await EventAppeared(e, token, callback).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.ErrorEvent("AppearedException", ex, "Stream: [{Stream:l}] Position: {StreamPosition} {ExceptionType} - {ExceptionMessage}", e.Event.EventStreamId, e.Event.EventNumber, ex.GetType().Name, ex.Message);
                sub.Fail(e, PersistentSubscriptionNakEventAction.Park, ex.GetType().Name);
                // don't throw, stops subscription and causes reconnect
                //throw;
            }
        }
示例#2
0
 public void Dispose()
 {
     if (_disposed)
     {
         return;
     }
     _disposed = true;
     _subscription.Stop(TimeSpan.FromSeconds(30));
 }
        public async Task persistent_subscription_delivers_all_events(int iteration)
        {
            const int eventCount  = 250;
            const int totalEvents = eventCount * 2;
            EventStorePersistentSubscriptionBase subscription = null;

            try {
                var userCredentials = new UserCredentials("admin", "changeit");

                int hitCount = 0;

                var streamName       = $"stream_{iteration}";
                var subscriptionName = $"subscription_{iteration}";

                var completed = new AutoResetEvent(false);

                var connection = _fixture.Connection;

                for (var i = 0; i < eventCount; i++)
                {
                    await connection.AppendToStreamAsync(streamName, ExpectedVersion.Any, _fixture.CreateTestEvents())
                    .WithTimeout();
                }

                var builder = PersistentSubscriptionSettings.Create()
                              .StartFromBeginning()
                              .ResolveLinkTos();

                await connection.CreatePersistentSubscriptionAsync(streamName,
                                                                   subscriptionName,
                                                                   builder.Build(),
                                                                   userCredentials).WithTimeout();

                subscription = await connection.ConnectToPersistentSubscriptionAsync(streamName, subscriptionName,
                                                                                     (_, resolvedEvent) => {
                    var result = Interlocked.Increment(ref hitCount);
                    _.Acknowledge(resolvedEvent);

                    if (totalEvents == result)
                    {
                        completed.Set();
                    }
                });

                for (var i = 0; i < eventCount; i++)
                {
                    await connection.AppendToStreamAsync(streamName, ExpectedVersion.Any, _fixture.CreateTestEvents())
                    .WithTimeout();
                }

                completed.WaitOne(TimeSpan.FromSeconds(30));
                Assert.Equal(totalEvents, hitCount);
            } finally {
                subscription?.Stop(TimeSpan.FromSeconds(5));
            }
        }
        public bool Close()
        {
            try
            {
                _eventStoreSubscription.Stop(TimeSpan.FromSeconds(5));

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
示例#5
0
        private void SubscribeToStream(IEventStoreConnection connection)
        {
            if (_subscription != null)
            {
                _subscription.Stop(TimeSpan.FromDays(1));
                _subscription = null;
            }

            try
            {
                CreateSubscription(connection);
                _subscription = connection.ConnectToPersistentSubscriptionAsync(_streamName, Group, EventAppeared, SubscriptionDropped).Result;

            }
            catch (Exception e) when (e is ConnectionClosedException || e is ObjectDisposedException || e.InnerException is ConnectionClosedException || e.InnerException is ObjectDisposedException)
            {
                SubscriptionDropped(null, SubscriptionDropReason.ConnectionClosed, e);
            }
        }
        private void EventAppeared(EventStorePersistentSubscriptionBase sub, ResolvedEvent e, CancellationToken token,
                                   Action <string, long, IFullEvent> callback)
        {
            // Don't care about metadata streams
            if (e.Event == null || e.Event.EventStreamId[0] == '$')
            {
                sub.Acknowledge(e.OriginalEvent.EventId);
                return;
            }

            if (token.IsCancellationRequested)
            {
                Logger.Warn($"Token cancelation requested, stopping persistent subscription");
                sub.Stop(TimeSpan.FromSeconds(5));
                lock (_subLock) _persistentSubs.Remove(sub);
                token.ThrowIfCancellationRequested();
            }
            _outstandingEvents[e.Event.EventId] = new Tuple <EventStorePersistentSubscriptionBase, Guid>(sub, e.OriginalEvent.EventId);

            EventAppeared(e, token, callback);
        }
 public override void Disconnect()
 {
     _eventStorePersistentSubscription?.Stop(TimeSpan.FromSeconds(5));
 }