public void WithLeaseContainerPassesCorrectValues()
        {
            CosmosContainer leaseContainerForBuilder = ChangeFeedProcessorBuilderTests.GetMockedContainer("leases");

            Action <DocumentServiceLeaseStoreManager,
                    CosmosContainer,
                    string,
                    string,
                    ChangeFeedLeaseOptions,
                    ChangeFeedProcessorOptions,
                    CosmosContainer> verifier = (DocumentServiceLeaseStoreManager leaseStoreManager,
                                                 CosmosContainer leaseContainer,
                                                 string leaseContainerPrefix,
                                                 string instanceName,
                                                 ChangeFeedLeaseOptions changeFeedLeaseOptions,
                                                 ChangeFeedProcessorOptions changeFeedProcessorOptions,
                                                 CosmosContainer monitoredContainer) =>
            {
                Assert.AreEqual(leaseContainerForBuilder, leaseContainer);
            };

            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                verifier);

            builder.WithLeaseContainer(leaseContainerForBuilder);

            builder.Build();
        }
示例#2
0
        public static async Task Main(string[] args)
        {
            using CosmosClient client = new CosmosClient(endpoint, apikey);
            Database  db             = client.GetDatabase("StoreDB");
            Container container      = db.GetContainer("CartContainer");
            Container leasecontainer = await db.CreateContainerIfNotExistsAsync("Lease", "/id");

            ChangeFeedProcessorBuilder builder = container.GetChangeFeedProcessorBuilder(
                "sampleProcessor",
                async(IReadOnlyCollection <Item> changes, CancellationToken cancellationToken) =>
            {
                await Console.Out.WriteLineAsync("***Changes occured***");
                foreach (Item change in changes)
                {
                    await Console.Out.WriteLineAsync(change.id + " - " + change.location + " - " + change.name);
                }
            });

            ChangeFeedProcessor processor = builder
                                            .WithInstanceName("firstinstance")
                                            .WithLeaseContainer(leasecontainer)
                                            .Build();

            await processor.StartAsync();

            Console.ReadKey();

            await processor.StopAsync();
        }
示例#3
0
        public async Task StartAsync()
        {
            var feedCollectionInfo = new DocumentCollectionInfo
            {
                DatabaseName   = _database,
                CollectionName = _eventContainer,
                Uri            = new Uri(_endpointUri),
                MasterKey      = _authKey
            };

            var leaseCollectionInfo = new DocumentCollectionInfo
            {
                DatabaseName   = _database,
                CollectionName = _leaseContainer,
                Uri            = new Uri(_endpointUri),
                MasterKey      = _authKey
            };

            var viewRepository = new CosmosDBViewRepository(_endpointUri, _authKey, _database);

            var builder = new ChangeFeedProcessorBuilder();

            _changeFeedProcessor = await builder
                                   .WithHostName("ProjectionHost")
                                   .WithFeedCollection(feedCollectionInfo)
                                   .WithLeaseCollection(leaseCollectionInfo)
                                   .WithObserverFactory(new EventObserverFactory(_projections, viewRepository))
                                   .WithProcessorOptions(new ChangeFeedProcessorOptions {
                StartFromBeginning = true
            })
                                   .BuildAsync();

            await _changeFeedProcessor.StartAsync();
        }
示例#4
0
    public async Task StartAsync()
    {
        Container itemContainer = await _containerService.GetContainerAsync <TSourcedEvent>();

        Container leaseContainer = await _leaseContainerProvider.GetLeaseContainerAsync();

        ChangeFeedProcessorBuilder builder = itemContainer
                                             .GetChangeFeedProcessorBuilder <TSourcedEvent>(_options.ProcessorName, (changes, token) =>
                                                                                            OnChangesAsync(changes, token, itemContainer.Id))
                                             .WithLeaseContainer(leaseContainer)
                                             .WithInstanceName(_options.InstanceName)
                                             .WithErrorNotification((_, exception) => OnErrorAsync(exception, itemContainer.Id));

        if (_options.PollInterval.HasValue)
        {
            builder.WithPollInterval(_options.PollInterval.Value);
        }

        _processor = builder.Build();

        _logger.LogInformation("Starting change feed processor for container {ContainerName} with key {ProjectionKey} and processor name {ProcessorName}",
                               itemContainer.Id,
                               typeof(TProjectionKey).Name,
                               _options.ProcessorName);

        await _processor.StartAsync();

        _logger.LogInformation("Successfully started change feed processor for container {ContainerName} with key {ProjectionKey} and processor name {ProcessorName}",
                               itemContainer.Id,
                               typeof(TProjectionKey).Name,
                               _options.ProcessorName);
    }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        static async Task RunProcessingAsync()
        {
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = Constants.CosmosDb_DatabaseName,
                CollectionName = Constants.CosmosDb_CollectionName,
                Uri            = new Uri(Constants.CosmosDb_Uri),
                MasterKey      = Constants.CosmosDb_Key
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = Constants.CosmosDb_DatabaseName,
                CollectionName = "leases",
                Uri            = new Uri(Constants.CosmosDb_Uri),
                MasterKey      = Constants.CosmosDb_Key
            };

            var builder   = new ChangeFeedProcessorBuilder();
            var processor = await builder
                            .WithHostName("ProductChangeObserverHost")
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            .WithObserver <ProductChangeObserver>()
                            .BuildAsync();

            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");
            Console.ReadLine();

            await processor.StopAsync();
        }
        public void WithInMemoryLeaseContainerInitializesStoreCorrectly()
        {
            Action <DocumentServiceLeaseStoreManager,
                    CosmosContainer,
                    string,
                    string,
                    ChangeFeedLeaseOptions,
                    ChangeFeedProcessorOptions,
                    CosmosContainer> verifier = (DocumentServiceLeaseStoreManager leaseStoreManager,
                                                 CosmosContainer leaseContainer,
                                                 string leaseContainerPrefix,
                                                 string instanceName,
                                                 ChangeFeedLeaseOptions changeFeedLeaseOptions,
                                                 ChangeFeedProcessorOptions changeFeedProcessorOptions,
                                                 CosmosContainer monitoredContainer) =>
            {
                Assert.IsInstanceOfType(leaseStoreManager, typeof(DocumentServiceLeaseStoreManagerInMemory));
            };

            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                verifier);

            builder.WithInMemoryLeaseContainer();

            builder.Build();
        }
示例#7
0
        private static ChangeFeedProcessorBuilder CreateBuilder <T>(DocumentClient client) where T : DocumentBase
        {
            var builder  = new ChangeFeedProcessorBuilder();
            var uri      = new Uri(CosmosUrl);
            var dbClient = new ChangeFeedDocumentClient(client);

            builder
            .WithHostName(HostName)
            .WithFeedCollection(new DocumentCollectionInfo
            {
                DatabaseName   = typeof(T).Name,
                CollectionName = "Items",
                Uri            = uri,
                MasterKey      = CosmosKey
            })
            .WithLeaseCollection(new DocumentCollectionInfo
            {
                DatabaseName   = typeof(T).Name,
                CollectionName = "Leases",
                Uri            = uri,
                MasterKey      = CosmosKey
            })
            .WithProcessorOptions(new ChangeFeedProcessorOptions
            {
                FeedPollDelay = TimeSpan.FromSeconds(15),
            })
            .WithFeedDocumentClient(dbClient)
            .WithLeaseDocumentClient(dbClient)
            .WithObserver <DocumentFeedObserver <T> >();

            return(builder);
        }
示例#8
0
        private static async Task <IChangeFeedProcessor> RunChangeFeedProcessorAsync(string uri, string key, string collection)
        {
            IChangeFeedDocumentClient dbClient = new ChangeFeedDocumentClient(new DocumentClient(new Uri(uri), key));

            dbClient = new QoSMeteringChangeFeedDocumentClient(dbClient, new QoSMeteringReporter());


            var builder = new ChangeFeedProcessorBuilder()
                          .WithObserver <ConsoleObserver>()
                          .WithHostName("console_app_host")
                          .WithFeedCollection(new DocumentCollectionInfo()
            {
                Uri            = new Uri(uri),
                MasterKey      = key,
                CollectionName = collection,
                DatabaseName   = DbName
            })
                          .WithLeaseCollection(new DocumentCollectionInfo()
            {
                CollectionName = $"{collection}.Lease.ConsoleApp",
                DatabaseName   = DbName,
                Uri            = new Uri(uri),
                MasterKey      = key
            })
                          .WithFeedDocumentClient(dbClient)
                          .WithLeaseDocumentClient(dbClient);

            var processor = await builder.BuildAsync();

            var estimator = await builder.BuildEstimatorAsync();

            await processor.StartAsync().ConfigureAwait(false);

            return(processor);
        }
        public void WithInstanceNameCorrectlyPassesParameters()
        {
            string myInstance = "myInstance";
            Action <DocumentServiceLeaseStoreManager,
                    CosmosContainer,
                    string,
                    string,
                    ChangeFeedLeaseOptions,
                    ChangeFeedProcessorOptions,
                    CosmosContainer> verifier = (DocumentServiceLeaseStoreManager leaseStoreManager,
                                                 CosmosContainer leaseContainer,
                                                 string leaseContainerPrefix,
                                                 string instanceName,
                                                 ChangeFeedLeaseOptions changeFeedLeaseOptions,
                                                 ChangeFeedProcessorOptions changeFeedProcessorOptions,
                                                 CosmosContainer monitoredContainer) =>
            {
                Assert.AreEqual(myInstance, instanceName);
            };

            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                verifier);

            builder.WithInMemoryLeaseContainer();
            builder.WithInstanceName(myInstance);

            builder.Build();
        }
示例#10
0
        private static async Task <IRemainingWorkEstimator> CreateEstimatorAsync(string uri, string key, string collection)
        {
            IChangeFeedDocumentClient dbClient = new ChangeFeedDocumentClient(new DocumentClient(new Uri(uri), key));

            var builder = new ChangeFeedProcessorBuilder()
                          .WithHostName("console_app_host")
                          .WithFeedCollection(new DocumentCollectionInfo()
            {
                Uri            = new Uri(uri),
                MasterKey      = key,
                CollectionName = collection,
                DatabaseName   = DbName
            })
                          .WithLeaseCollection(new DocumentCollectionInfo()
            {
                CollectionName = $"{collection}.Lease.ConsoleApp",
                DatabaseName   = DbName,
                Uri            = new Uri(uri),
                MasterKey      = key
            })
                          .WithFeedDocumentClient(dbClient)
                          .WithLeaseDocumentClient(dbClient);

            return(await builder.BuildEstimatorAsync());
        }
示例#11
0
        static async Task RunAsync()
        {
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "Sjaas",
                CollectionName = "Job",
                Uri            = new Uri("to-be-filled"),
                MasterKey      = "to-be-filled"
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "Sjaas",
                CollectionName = "Lease",
                Uri            = new Uri("to-be-filled"),
                MasterKey      = "to-be-filled"
            };

            var builder   = new ChangeFeedProcessorBuilder();
            var processor = await builder
                            .WithHostName("SjaasWorker")
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            .WithObserver <SjaasWorker>()
                            .BuildAsync();

            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");
            Console.ReadLine();

            await processor.StopAsync();
        }
        public async Task BuildEstimatorThrowsWhenNoneOfLeaseCollectionInfoOrLeaseStoreManagerSpecified()
        {
            var builder = new ChangeFeedProcessorBuilder()
                          .WithHostName("host")
                          .WithFeedCollection(CollectionInfo)
                          .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>());

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await builder.BuildEstimatorAsync());
        }
        public void MonitoredContainer_LeaseStore_IsRequired()
        {
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                ChangeFeedProcessorBuilderTests.GetEmptyInitialization());

            builder.Build();
        }
        public void WorkFlowName_IsRequired()
        {
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(null,
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                ChangeFeedProcessorBuilderTests.GetEmptyInitialization());

            builder.Build();
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                Log("ExecuteAsync");

                var hostName = _cosmosSettings.HostName;
                var dbName   = _cosmosSettings.DatabaseName;
                var key      = _cosmosSettings.Key;
                var uri      = _cosmosSettings.Uri;

                DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
                {
                    DatabaseName   = dbName,
                    CollectionName = _cosmosSettings.CollectionName,
                    Uri            = new Uri(uri),
                    MasterKey      = key
                };

                DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
                {
                    DatabaseName   = dbName,
                    CollectionName = _cosmosSettings.LeaseCollectionName,
                    Uri            = new Uri(uri),
                    MasterKey      = key
                };

                var observerFactory = new CosmosDBObserverFactory(_memoryCache, _log);
                var builder         = new ChangeFeedProcessorBuilder();
                var processor       = await builder
                                      .WithHostName(hostName)
                                      .WithFeedCollection(feedCollectionInfo)
                                      .WithLeaseCollection(leaseCollectionInfo)
                                      .WithObserverFactory(observerFactory)
                                      .BuildAsync();

                Log("before StartAsync");
                await processor.StartAsync();

                Log("after StartAsync");

                await Task.Delay(-1);

                Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");
                Console.ReadLine();

                Log("before StopAsync");
                await processor.StopAsync();

                Log("after StopAsync");
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
        }
示例#16
0
        /// <summary>
        /// Registers a change feed observer to update changes read on
        /// change feed to destination collection. Deregisters change feed
        /// observer and closes process when enter key is pressed
        /// </summary>
        /// <returns>A Task to allow asynchronous execution</returns>
        private async Task RunChangeFeedHostAsync()
        {
            string monitoredUri           = ConfigurationManager.AppSettings["CosmosDBEndpointUri"];
            string monitoredSecretKey     = ConfigurationManager.AppSettings["CosmosDBAuthKey"];
            string monitoredDbName        = ConfigurationManager.AppSettings["DatabaseName"];
            string monitoredContainerName = ConfigurationManager.AppSettings["ContainerName"];

            // Source collection to be monitored for changes
            DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(monitoredUri),
                MasterKey      = monitoredSecretKey,
                DatabaseName   = monitoredDbName,
                CollectionName = monitoredContainerName
            };

            string leaseUri           = ConfigurationManager.AppSettings["LeaseUri"];
            string leaseSecretKey     = ConfigurationManager.AppSettings["LeaseSecretKey"];
            string leaseDbName        = ConfigurationManager.AppSettings["LeaseDbName"];
            string leaseContainerName = ConfigurationManager.AppSettings["LeaseContainerName"];

            // Lease Collection managing leases on each of the underlying shards of the source collection being monitored
            DocumentCollectionInfo leaseContainerInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(leaseUri),
                MasterKey      = leaseSecretKey,
                DatabaseName   = leaseDbName,
                CollectionName = leaseContainerName
            };

            DocumentFeedObserverFactory docObserverFactory   = new DocumentFeedObserverFactory(this.DocumentClient);
            ChangeFeedProcessorOptions  feedProcessorOptions = new ChangeFeedProcessorOptions();

            int feedPollDelayInSeconds = int.Parse(ConfigurationManager.AppSettings["FeedPollDelayInSeconds"]);

            feedProcessorOptions.LeaseRenewInterval      = TimeSpan.FromSeconds(240);
            feedProcessorOptions.LeaseExpirationInterval = TimeSpan.FromSeconds(240);
            feedProcessorOptions.FeedPollDelay           = TimeSpan.FromMilliseconds(feedPollDelayInSeconds * 1000);
            feedProcessorOptions.StartFromBeginning      = true;
            feedProcessorOptions.MaxItemCount            = 2000;

            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder();

            builder
            .WithHostName(this.HostName)
            .WithFeedCollection(documentCollectionInfo)
            .WithLeaseCollection(leaseContainerInfo)
            .WithProcessorOptions(feedProcessorOptions)
            .WithObserverFactory(new DocumentFeedObserverFactory(this.DocumentClient));

            var result = await builder.BuildAsync();

            await result.StartAsync().ConfigureAwait(false);
        }
        public void CanBuildWithInMemoryContainer()
        {
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                ChangeFeedProcessorBuilderTests.GetEmptyInitialization());

            builder.WithLeaseContainer(ChangeFeedProcessorBuilderTests.GetMockedContainer());

            Assert.IsInstanceOfType(builder.Build(), typeof(ChangeFeedProcessor));
        }
        public async Task BuildEstimatorWhenOnlyLeaseStoreManagerSpecified()
        {
            var builder = new ChangeFeedProcessorBuilder()
                          .WithHostName("host")
                          .WithFeedCollection(CollectionInfo)
                          .WithFeedDocumentClient(CreateMockDocumentClient())
                          .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>())
                          .WithLeaseStoreManager(Mock.Of <ILeaseStoreManager>());

            await builder.BuildEstimatorAsync();
        }
示例#19
0
        /// <summary>
        /// Registers change feed observer to update changes read on change feed to destination
        /// collection. Deregisters change feed observer and closes process when enter key is pressed
        /// </summary>
        /// <returns>A Task to allow asynchronous execution</returns>

        public async Task RunChangeFeedHostAsync()
        {
            string hostName = Guid.NewGuid().ToString();

            // monitored collection info
            DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.monitoredUri),
                MasterKey      = this.monitoredSecretKey,
                DatabaseName   = this.monitoredDbName,
                CollectionName = this.monitoredCollectionName
            };


            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.leaseUri),
                MasterKey      = this.leaseSecretKey,
                DatabaseName   = this.leaseDbName,
                CollectionName = this.leaseCollectionName
            };
            DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory();
            ChangeFeedOptions           feedOptions        = new ChangeFeedOptions();

            /* ie customize StartFromBeginning so change feed reads from beginning
             *  can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning
             */

            feedOptions.StartFromBeginning = true;

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            // ie. customizing lease renewal interval to 15 seconds
            // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay
            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder();

            builder
            .WithHostName(hostName)
            .WithFeedCollection(documentCollectionInfo)
            .WithLeaseCollection(leaseCollectionInfo)
            .WithProcessorOptions(feedProcessorOptions)
            .WithObserverFactory(new DocumentFeedObserverFactory());

            //    .WithObserver<DocumentFeedObserver>();  or just pass a observer

            var result = await builder.BuildAsync();

            await result.StartAsync();

            Console.Read();
            await result.StopAsync();
        }
示例#20
0
 private void InitializeBuilder()
 {
     if (_hostBuilder == null)
     {
         _hostBuilder = new ChangeFeedProcessorBuilder()
                        .WithHostName(_hostName)
                        .WithFeedDocumentClient((DocumentClient)_monitoredDocumentClient)
                        .WithLeaseDocumentClient((DocumentClient)_leaseDocumentClient)
                        .WithFeedCollection(_monitorCollection)
                        .WithLeaseCollection(_leaseCollection)
                        .WithProcessorOptions(_processorOptions)
                        .WithObserverFactory(this);
     }
 }
        private static async Task RunChangeFeedHostAsync()
        {
            // monitored collection info
            DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(cosmosSettings.DbUrl),
                MasterKey      = cosmosSettings.AuthorizationKey,
                DatabaseName   = cosmosSettings.DbName,
                CollectionName = cosmosSettings.CollectionName
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(cosmosSettings.DbUrl),
                MasterKey      = cosmosSettings.AuthorizationKey,
                DatabaseName   = cosmosSettings.DbName,
                CollectionName = cosmosSettings.LeaseCollectionName
            };
            DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory();
            ChangeFeedOptions           feedOptions        = new ChangeFeedOptions
            {
                /* ie customize StartFromBeginning so change feed reads from beginning
                 *  can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning
                 */
                StartFromBeginning = true
            };

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            // ie. customizing lease renewal interval to 15 seconds
            // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay
            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder();

            builder
            .WithHostName(hostName)
            .WithFeedCollection(documentCollectionInfo)
            .WithLeaseCollection(leaseCollectionInfo)
            .WithProcessorOptions(feedProcessorOptions)
            .WithObserverFactory(new DocumentFeedObserverFactory());
            //.WithObserver<DocumentFeedObserver>();  If no factory then just pass an observer

            var result = await builder.BuildAsync();

            await result.StartAsync();

            Console.Read();
            await result.StopAsync();
        }
        public void CannotBuildTwice()
        {
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder("workflowName",
                                                                                ChangeFeedProcessorBuilderTests.GetMockedContainer(),
                                                                                ChangeFeedProcessorBuilderTests.GetMockedProcessor(),
                                                                                ChangeFeedProcessorBuilderTests.GetEmptyInitialization());

            builder.WithLeaseContainer(ChangeFeedProcessorBuilderTests.GetMockedContainer());

            // This build should not throw
            builder.Build();

            // This one should
            Assert.ThrowsException <InvalidOperationException>(() => builder.Build());
        }
示例#23
0
 private void InitializeBuilder()
 {
     if (this._hostBuilder == null)
     {
         this._hostBuilder = new ChangeFeedProcessorBuilder()
                             .WithHostName(this._hostName)
                             .WithFeedDocumentClient(this._monitoredCosmosDBService.GetClient())
                             .WithLeaseDocumentClient(this._leasesCosmosDBService.GetClient())
                             .WithFeedCollection(this._monitorCollection)
                             .WithLeaseCollection(this._leaseCollection)
                             .WithProcessorOptions(this._processorOptions)
                             .WithHealthMonitor(this._healthMonitor)
                             .WithObserverFactory(this);
     }
 }
示例#24
0
        static async Task DoProcessingAsync()
        {
            string hostName = Guid.NewGuid().ToString();
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = Constants.CosmosDb_DatabaseName,
                CollectionName = Constants.CosmosDb_CollectionName,
                Uri            = new Uri(Constants.CosmosDb_Uri),
                MasterKey      = Constants.CosmosDb_Key
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = Constants.CosmosDb_DatabaseName,
                CollectionName = "Clothing",
                Uri            = new Uri(Constants.CosmosDb_Uri),
                MasterKey      = Constants.CosmosDb_Key
            };

            var builder = new ChangeFeedProcessorBuilder();


            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            feedProcessorOptions.StartFromBeginning = true;

            //ChangeFeedEventHost host = new ChangeFeedEventHost("ProductChangeObserverHost", feedCollectionInfo, leaseCollectionInfo, feedOptions, feedHostOptions);

            var processor = await builder
                            .WithHostName("ProductChangeObserverHost-" + hostName)
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            //.WithObserver<ProductChangeObserver>()
                            .WithProcessorOptions(feedProcessorOptions)
                            .WithObserverFactory(new ProductFeedObserverFactory())
                            .BuildAsync();


            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");
            Console.ReadLine();

            await processor.StopAsync();
        }
示例#25
0
        /// <summary>
        /// Registers change feed observer to update changes read on change feed to destination
        /// collection. Deregisters change feed observer and closes process when enter key is pressed
        /// </summary>
        /// <returns>A Task to allow asynchronous execution</returns>

        public async Task RunChangeFeedHostAsync()
        {
            string hostName = Guid.NewGuid().ToString();

            // monitored collection info
            DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.monitoredUri),
                MasterKey      = this.monitoredSecretKey,
                DatabaseName   = this.monitoredDbName,
                CollectionName = this.monitoredCollectionName
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.leaseUri),
                MasterKey      = this.leaseSecretKey,
                DatabaseName   = this.leaseDbName,
                CollectionName = this.leaseCollectionName
            };
            DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory();

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            feedProcessorOptions.StartFromBeginning = true;
            // ie. customizing lease renewal interval to 15 seconds
            // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay
            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder();

            builder
            .WithHostName(hostName)
            .WithFeedCollection(documentCollectionInfo)
            .WithLeaseCollection(leaseCollectionInfo)
            .WithProcessorOptions(feedProcessorOptions)
            .WithPartitionLoadBalancingStrategy(new LoadBalancingStrategy())
            .WithObserverFactory(new DocumentFeedObserverFactory());

            var result = await builder.BuildAsync();

            await result.StartAsync();

            Console.Read();
            await result.StopAsync();
        }
示例#26
0
        static async Task RunAsync()
        {
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "DemoDatabase",
                CollectionName = "DemoCollection",
                Uri            = new Uri("endpoint"),
                MasterKey      = ""
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "DemoDatabase",
                CollectionName = "leases",
                Uri            = new Uri("endpoint"),
                MasterKey      = "key"
            };

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            // ie. customizing lease renewal interval to 15 seconds
            // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay
            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            feedProcessorOptions.StartFromBeginning = true;
            //feedProcessorOptions.StartTime = DateTime.Now - TimeSpan.FromDays(1);

            var builder   = new ChangeFeedProcessorBuilder();
            var processor = await builder
                            .WithHostName("SampleHost")
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            .WithObserver <SampleObserver>() // add as many sample observers (aka consumers as needed) via WithObserver<>
                            .WithProcessorOptions(feedProcessorOptions)
                            .BuildAsync();

            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");
            Console.ReadLine();

            await processor.StopAsync();
        }
        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);
        }
示例#28
0
        // Boilerplate from the ChangeFeedProcessor library.
        public async Task RunChangeFeedHostAsync()
        {
            string hostName = "ChatHost" + DateTime.Now.Ticks.ToString();


            // The collection to be observed is registered here.
            DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(Keys.CosmosDBUri),
                MasterKey      = Keys.CosmosDBKey,
                DatabaseName   = GroupSelectionPageViewModel.SelectedDBName,
                CollectionName = "Messages"
            };

            // The lease is a collection where the changes are temporary stored.
            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(Keys.CosmosDBUri),
                MasterKey      = Keys.CosmosDBKey,
                DatabaseName   = GroupSelectionPageViewModel.SelectedDBName,
                CollectionName = "Lease"
            };
            DocumentFeedObserverFactory docObserverFactory   = new DocumentFeedObserverFactory();
            ChangeFeedProcessorOptions  feedProcessorOptions = new ChangeFeedProcessorOptions();

            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15);
            feedProcessorOptions.StartFromBeginning = true;
            ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder();

            builder
            .WithHostName(hostName)
            .WithFeedCollection(documentCollectionInfo)
            .WithLeaseCollection(leaseCollectionInfo)
            .WithProcessorOptions(feedProcessorOptions)
            .WithObserverFactory(new DocumentFeedObserverFactory());

            // A new ChangeFeedProcessor is built and then run asynchronously.
            var result = await builder.BuildAsync();

            await result.StartAsync();
        }
示例#29
0
        static async Task RunAsync()
        {
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "pinball-leaderboard",
                CollectionName = "leaderboard",
                Uri            = new Uri("https://sqlcomos2019.documents.azure.com:443/"),
                MasterKey      = "0oyBJST0UzbkkU2hIYMmePZAEjBoTPTW7EEqUCL7WPhxNN2DNai8TwpKfnQrE6mlN4R93PGe08fGIoK2FtAYfw=="
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "pinball-leaderboard",
                CollectionName = "leasesScore",
                Uri            = new Uri("https://sqlcomos2019.documents.azure.com:443/"),
                MasterKey      = "0oyBJST0UzbkkU2hIYMmePZAEjBoTPTW7EEqUCL7WPhxNN2DNai8TwpKfnQrE6mlN4R93PGe08fGIoK2FtAYfw=="
            };

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(5);
            feedProcessorOptions.StartFromBeginning = false;

            var builder   = new ChangeFeedProcessorBuilder();
            var processor = await builder
                            .WithHostName("LeaderboardHost")
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            .WithObserver <ChangeFeedObserver>()
                            .WithProcessorOptions(feedProcessorOptions)
                            .BuildAsync();

            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");

            Console.ReadLine();

            await processor.StopAsync();
        }
示例#30
0
        static async Task RunAsync()
        {
            DocumentCollectionInfo feedCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "pinball-leaderboard",
                CollectionName = "leaderboard",
                Uri            = new Uri("<account-connection-string>"),
                MasterKey      = "<primary-key>"
            };

            DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo()
            {
                DatabaseName   = "pinball-leaderboard",
                CollectionName = "leases",
                Uri            = new Uri("<account-connection-string>"),
                MasterKey      = "<primary-key>"
            };

            ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions();

            feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(5);
            feedProcessorOptions.StartFromBeginning = false;

            var builder   = new ChangeFeedProcessorBuilder();
            var processor = await builder
                            .WithHostName("LeaderboardHost")
                            .WithFeedCollection(feedCollectionInfo)
                            .WithLeaseCollection(leaseCollectionInfo)
                            .WithObserver <ChangeFeedObserver>()
                            .WithProcessorOptions(feedProcessorOptions)
                            .BuildAsync();

            await processor.StartAsync();

            Console.WriteLine("Change Feed Processor started. Press <Enter> key to stop...");

            Console.ReadLine();

            await processor.StopAsync();
        }