public bool RemoveSubscription(SubscriptionItem subscription)
        {
            var res = _activeSubscriptions.Remove(subscription.CorrelationId);

            LogDebug("RemoveSubscription {0}, result {1}.", subscription, res);
            return(res);
        }
        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 COULD NOT SUBSCRIBE {0}.", subscription);
                RemoveSubscription(subscription);
            }
            else
            {
                LogDebug("StartSubscription SUBSCRIBING {0}.", subscription);
            }
        }
Example #3
0
        private void StartSubscription(StartPersistentSubscriptionMessage 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 ConnectToPersistentSubscriptionOperation(_settings.Log, msg.Source, msg.SubscriptionId, msg.BufferSize, msg.StreamId,
                                                                             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));
            }
        }
        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);
        }
 public bool TryGetActiveSubscription(Guid correlationId, out SubscriptionItem subscription)
 {
     return(_activeSubscriptions.TryGetValue(correlationId, out subscription));
 }
 public void EnqueueSubscription(SubscriptionItem subscriptionItem)
 {
     _waitingSubscriptions.Enqueue(subscriptionItem);
 }
 private void StartSubscription(StartPersistentSubscriptionMessage 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 ConnectToPersistentSubscriptionOperation(_settings.Log, msg.Source, msg.SubscriptionId, msg.BufferSize, msg.StreamId,
                                                       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));
     }
 }
 public bool TryGetActiveSubscription(Guid correlationId, out SubscriptionItem subscription)
 {
     return _activeSubscriptions.TryGetValue(correlationId, out subscription);
 }
        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 COULD NOT SUBSCRIBE {0}.", subscription);
                RemoveSubscription(subscription);
            }
            else
            {
                LogDebug("StartSubscription SUBSCRIBING {0}.", subscription);
            }
        }
Example #10
0
 public void EnqueueSubscription(SubscriptionItem subscriptionItem)
 {
     _waitingSubscriptions.Enqueue(subscriptionItem);
 }
Example #11
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);
        }
Example #12
0
 public bool RemoveSubscription(SubscriptionItem subscription)
 {
     var res = _activeSubscriptions.Remove(subscription.CorrelationId);
     LogDebug("RemoveSubscription {0}, result {1}.", subscription, res);
     return res;
 }