public async Task RunAsync() { var db = await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = jobdb }); await client.CreateDocumentCollectionIfNotExistsAsync(db.Resource.SelfLink, new DocumentCollection() { Id = jobColl }); while (true) { // Check if a migration doc got inserted in the last hour if (currentMigrationId == null) { var option = new FeedOptions { EnableCrossPartitionQuery = true }; var configDocs = client.CreateDocumentQuery <MigrationConfig>(UriFactory.CreateDocumentCollectionUri(jobdb, jobColl), string.Format("select * from c where NOT c.completed"), option).AsEnumerable <MigrationConfig>().ToList(); if (configDocs.Count == 0) { Console.WriteLine("No job for process: " + Process.GetCurrentProcess().Id); await Task.Delay(5000); continue; } var config = configDocs.First(); currentMigrationId = config.Id; changeFeedProcessorHost = new ChangeFeedProcessorHost(config); await changeFeedProcessorHost.StartAsync(); } else { var option = new FeedOptions { EnableCrossPartitionQuery = true }; var configDocs = client.CreateDocumentQuery <MigrationConfig>(UriFactory.CreateDocumentCollectionUri(jobdb, jobColl), string.Format("select * from c where c.id = \"{0}\"", currentMigrationId), option).AsEnumerable <MigrationConfig>().ToList(); if (configDocs.Count == 0 || configDocs.First().Completed) { Console.WriteLine("Current Migration is completed or deleted, closing migration " + Process.GetCurrentProcess().Id); this.currentMigrationId = null; await changeFeedProcessorHost.CloseAsync(); this.changeFeedProcessorHost = null; continue; } await Task.Delay(5000); } } }
public async Task RunAsync() { Database db = await client.CreateDatabaseIfNotExistsAsync(jobdb); Container container = await db.CreateContainerIfNotExistsAsync(new ContainerProperties(jobColl, "/_partitionKey")); while (true) { // Check if a migration doc got inserted in the last hour if (currentMigrationId == null) { var configDocs = container.GetItemQueryIterator <MigrationConfig>("select * from c where NOT c.completed").ReadNextAsync().Result.AsEnumerable <MigrationConfig>().ToList(); if (configDocs.Count == 0) { Console.WriteLine("No job for process: " + Process.GetCurrentProcess().Id); await Task.Delay(5000); continue; } var config = configDocs.First(); currentMigrationId = config.Id; changeFeedProcessorHost = new ChangeFeedProcessorHost(config); await changeFeedProcessorHost.StartAsync(); } else { var configDocs = container.GetItemQueryIterator <MigrationConfig>(string.Format("select * from c where c.id = \"{0}\"", currentMigrationId)).ReadNextAsync().Result.AsEnumerable <MigrationConfig>().ToList(); if (configDocs.Count == 0 || configDocs.First().Completed) { Console.WriteLine("Current Migration is completed or deleted, closing migration " + Process.GetCurrentProcess().Id); this.currentMigrationId = null; await changeFeedProcessorHost.CloseAsync(); this.changeFeedProcessorHost = null; continue; } await Task.Delay(5000); } } }