Ejemplo n.º 1
0
        /// <summary>
        /// Registers an <see cref="IConfigObserver"/> object, which is notified when a configuration changes.
        /// </summary>
        /// <param name="observer">The <see cref="IConfigObserver"/> that will be notified when a configuration
        /// update occurs. These are Memcached and Couchbase Buckets.</param>
        /// <returns>True if the observer was registered without failure.</returns>
        public override bool RegisterObserver(IConfigObserver observer)
        {
            var hasRegistered = false;
            var bucketConfig  = _serverConfig.Buckets.Find(x => x.Name == observer.Name);

            if (bucketConfig == null)
            {
                throw new BucketNotFoundException(observer.Name);
            }

            var cancellationTokenSource = new CancellationTokenSource();

            _cancellationTokens[observer.Name] = cancellationTokenSource;

            var configThreadState = new ConfigThreadState(bucketConfig, ConfigChangedHandler, ErrorOccurredHandler, cancellationTokenSource.Token);
            var thread            = new Thread(configThreadState.ListenForConfigChanges);

            if (_threads.TryAdd(observer.Name, thread) && ConfigObservers.TryAdd(observer.Name, observer))
            {
                _threads[observer.Name].Start();

                if (CountdownEvent.CurrentCount == 0)
                {
                    CountdownEvent.Reset(1);
                }

                //TODO add timeout?
                CountdownEvent.Wait(cancellationTokenSource.Token);
                hasRegistered = true;
            }
            return(hasRegistered);
        }
        /// <summary>
        /// Registers an <see cref="IConfigObserver"/> object, which is notified when a configuration changes.
        /// </summary>
        /// <param name="observer">The <see cref="IConfigObserver"/> that will be notified when a configuration
        /// update occurs. These are Memcached and Couchbase Buckets.</param>
        /// <returns>True if the observer was registered without failure.</returns>
        public override bool RegisterObserver(IConfigObserver observer)
        {
            RegisterEvent.WaitOne(10000);//TODO make configurable
            var hasRegistered = false;

            try
            {
                var bucketConfig = _serverConfig.Buckets.Find(x => x.Name == observer.Name);
                if (bucketConfig == null)
                {
                    throw new BucketNotFoundException(observer.Name);
                }

                var cancellationTokenSource = new CancellationTokenSource();
                _cancellationTokens[observer.Name] = cancellationTokenSource;

                var configThreadState = new ConfigThreadState(bucketConfig, ConfigChangedHandler, ErrorOccurredHandler,
                    cancellationTokenSource.Token, Log);
                var thread = new Thread(configThreadState.ListenForConfigChanges)
                {
                    IsBackground = true
                };

                if (_threads.TryAdd(observer.Name, thread) && ConfigObservers.TryAdd(observer.Name, observer))
                {
                    _threads[observer.Name].Start();
                    if (CountdownEvent.CurrentCount == 0)
                    {
                        CountdownEvent.Reset(1);
                    }

                    //TODO add timeout?
                    CountdownEvent.Wait(10000, cancellationTokenSource.Token);//TODO make configurable
                    hasRegistered = true;
                }
            }
            finally
            {
                RegisterEvent.Set();
            }
            return hasRegistered;
        }