Пример #1
0
        private void StartSubscription(StartSubscriptionMessage msg)
        {
            switch (_state)
            {
            case ConnectionState.Init:
                msg.Source.SetException(new InvalidOperationException(string.Format("EventStoreConnection '{0}' is not active.", _esConnection.ConnectionName)));
                break;

            case ConnectionState.Connecting:
            case ConnectionState.Connected:
                var operation = new SubscriptionOperation(_settings.Log, msg.Source, msg.StreamId, msg.ResolveLinkTos,
                                                          msg.UserCredentials, msg.EventAppeared, msg.SubscriptionDropped,
                                                          _settings.VerboseLogging, () => _connection);
                LogDebug("StartSubscription {4} {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, msg.MaxRetries, msg.Timeout, _state == ConnectionState.Connected ? "fire" : "enqueue");
                var subscription = new SubscriptionItem(operation, msg.MaxRetries, msg.Timeout);
                if (_state == ConnectionState.Connecting)
                {
                    _subscriptions.EnqueueSubscription(subscription);
                }
                else
                {
                    _subscriptions.StartSubscription(subscription, _connection);
                }
                break;

            case ConnectionState.Closed:
                msg.Source.SetException(new ObjectDisposedException(_esConnection.ConnectionName));
                break;

            default: throw new Exception(string.Format("Unknown state: {0}.", _state));
            }
        }
Пример #2
0
        public void StartSubscription(SubscriptionItem subscription, TcpPackageConnection connection)
        {
            Ensure.NotNull(connection, "connection");

            if (subscription.IsSubscribed)
            {
                LogDebug("StartSubscription REMOVING due to already subscribed {0}.", subscription);
                RemoveSubscription(subscription);
                return;
            }

            subscription.CorrelationId = Guid.NewGuid();
            subscription.ConnectionId  = connection.ConnectionId;
            subscription.LastUpdated   = DateTime.UtcNow;

            _activeSubscriptions.Add(subscription.CorrelationId, subscription);

            if (!subscription.Operation.Subscribe(subscription.CorrelationId, connection))
            {
                LogDebug("StartSubscription REMOVING AS COULDN'T SUBSCRIBE {0}.", subscription);
                RemoveSubscription(subscription);
            }
            else
            {
                LogDebug("StartSubscription SUBSCRIBING {0}.", subscription);
            }
        }
Пример #3
0
        public bool RemoveSubscription(SubscriptionItem subscription)
        {
            var res = _activeSubscriptions.Remove(subscription.CorrelationId);

            LogDebug("RemoveSubscription {0}, result {1}.", subscription, res);
            return(res);
        }
Пример #4
0
        public void ScheduleSubscriptionRetry(SubscriptionItem subscription)
        {
            if (!RemoveSubscription(subscription))
            {
                LogDebug("RemoveSubscription failed when trying to retry {0}.", subscription);
                return;
            }

            if (subscription.MaxRetries >= 0 && subscription.RetryCount >= subscription.MaxRetries)
            {
                LogDebug("RETRIES LIMIT REACHED when trying to retry {0}.", subscription);
                subscription.Operation.DropSubscription(SubscriptionDropReason.SubscribingError,
                                                        new RetriesLimitReachedException(subscription.ToString(), subscription.RetryCount));
                return;
            }

            LogDebug("retrying subscription {0}.", subscription);
            _retryPendingSubscriptions.Add(subscription);
        }
 private void StartSubscription(StartSubscriptionMessage msg)
 {
     switch (_state)
     {
         case ConnectionState.Init:
             msg.Source.SetException(new InvalidOperationException(string.Format("EventStoreConnection '{0}' is not active.", _esConnection.ConnectionName)));
             break;
         case ConnectionState.Connecting:
         case ConnectionState.Connected:
             var operation = new SubscriptionOperation(_settings.Log, msg.Source, msg.StreamId, msg.ResolveLinkTos, 
                                                       msg.UserCredentials, msg.EventAppeared, msg.SubscriptionDropped, 
                                                       _settings.VerboseLogging, () => _connection);
             LogDebug("StartSubscription {4} {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, msg.MaxRetries, msg.Timeout, _state == ConnectionState.Connected ? "fire" : "enqueue");
             var subscription = new SubscriptionItem(operation, msg.MaxRetries, msg.Timeout);
             if (_state == ConnectionState.Connecting)
                 _subscriptions.EnqueueSubscription(subscription);
             else
                 _subscriptions.StartSubscription(subscription, _connection);
             break;
         case ConnectionState.Closed:
             msg.Source.SetException(new ObjectDisposedException(_esConnection.ConnectionName));
             break;
         default: throw new Exception(string.Format("Unknown state: {0}.", _state));
     }
 }
Пример #6
0
 public bool TryGetActiveSubscription(Guid correlationId, out SubscriptionItem subscription)
 {
     return _activeSubscriptions.TryGetValue(correlationId, out subscription);
 }
Пример #7
0
        public void StartSubscription(SubscriptionItem subscription, TcpPackageConnection connection)
        {
            Ensure.NotNull(connection, "connection");

            if (subscription.IsSubscribed)
            {
                LogDebug("StartSubscription REMOVING due to already subscribed {0}.", subscription);
                RemoveSubscription(subscription);
                return;
            }

            subscription.CorrelationId = Guid.NewGuid();
            subscription.ConnectionId = connection.ConnectionId;
            subscription.LastUpdated = DateTime.UtcNow;

            _activeSubscriptions.Add(subscription.CorrelationId, subscription);

            if (!subscription.Operation.Subscribe(subscription.CorrelationId, connection))
            {
                LogDebug("StartSubscription REMOVING AS COULDN'T SUBSCRIBE {0}.", subscription);
                RemoveSubscription(subscription);
            }
            else
            {
                LogDebug("StartSubscription SUBSCRIBING {0}.", subscription);
            }
        }
Пример #8
0
 public void EnqueueSubscription(SubscriptionItem subscriptionItem)
 {
     _waitingSubscriptions.Enqueue(subscriptionItem);
 }
Пример #9
0
        public void ScheduleSubscriptionRetry(SubscriptionItem subscription)
        {
            if (!RemoveSubscription(subscription))
            {
                LogDebug("RemoveSubscription failed when trying to retry {0}.", subscription);
                return;
            }

            if (subscription.MaxRetries >= 0 && subscription.RetryCount >= subscription.MaxRetries)
            {
                LogDebug("RETRIES LIMIT REACHED when trying to retry {0}.", subscription);
                subscription.Operation.DropSubscription(SubscriptionDropReason.SubscribingError,
                                                        new RetriesLimitReachedException(subscription.ToString(), subscription.RetryCount));
                return;
            }

            LogDebug("retrying subscription {0}.", subscription);
            _retryPendingSubscriptions.Add(subscription);
        }
Пример #10
0
 public bool RemoveSubscription(SubscriptionItem subscription)
 {
     var res = _activeSubscriptions.Remove(subscription.CorrelationId);
     LogDebug("RemoveSubscription {0}, result {1}.", subscription, res);
     return res;
 }
Пример #11
0
 public bool TryGetActiveSubscription(Guid correlationId, out SubscriptionItem subscription)
 {
     return(_activeSubscriptions.TryGetValue(correlationId, out subscription));
 }
Пример #12
0
 public void EnqueueSubscription(SubscriptionItem subscriptionItem)
 {
     _waitingSubscriptions.Enqueue(subscriptionItem);
 }