Exemplo n.º 1
0
        public IObservable <IEnumerable <string> > Listen()
        {
            CreateStoresIfNotExistsAsync().Wait();

            var stream = Observable.Create <IEnumerable <string> >(obs =>
            {
                var factory   = new SampleObserverFactory(obs);
                var processor = new ChangeFeedProcessorBuilder()
                                .WithFeedCollection(new DocumentCollectionInfo {
                    Uri = CosmosUri, MasterKey = CosmosKey, DatabaseName = DatabaseName, CollectionName = MonitoredCollection
                })
                                .WithLeaseCollection(new DocumentCollectionInfo {
                    Uri = CosmosUri, MasterKey = CosmosKey, DatabaseName = DatabaseName, CollectionName = LeaseCollection
                })
                                .WithHostName("SampleHost")
                                .WithObserverFactory(factory)
                                /* the explicit checkpoint is important because otherwise when the stream blows, it still checkpoints, and we 'lose' the message */
                                .WithProcessorOptions(new ChangeFeedProcessorOptions {
                    MaxItemCount = 2, CheckpointFrequency = new CheckpointFrequency {
                        ExplicitCheckpoint = true
                    }
                })
                                .BuildAsync().Result;

                processor.StartAsync().Wait();

                var sub = Tuple.Create(obs, processor);
                Subscriptions.Add(sub);

                return(Disposable.Create(() =>
                {
                    // the ConfigureAwait(false) is *critical* to ensure the processor stops!
                    processor.StopAsync().ConfigureAwait(false).AsTask().Wait();
                    Subscriptions.Remove(sub);
                    Log.Warning("Disposable run for subscription");
                }));
            });

            Log.Information("Subscription created");
            return(stream);
        }