public static async Task <IActionResult> DeletePlans(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = null)] HttpRequest req,
            ILogger log)
        {
            CosmosRepo cosmosRepo = new CosmosRepo(log);

            IEnumerable <Plan> plans = await cosmosRepo.GetItemsAsync <Plan>("select * from c", null);

            log.LogInformation($"Total plans to delete, {plans.Count()}");


            //Update status to Available
            BulkProcessData dataToDelete = new BulkProcessData();

            dataToDelete.ItemData = new List <ItemData>();
            foreach (Plan plan in plans)
            {
                dataToDelete.ItemData.Add(new ItemData
                {
                    Item         = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(plan))),            // JsonConvert.SerializeObject(s),
                    ItemId       = plan.id,
                    PartitionKey = plan.__partitionKey
                });
            }
            dataToDelete.Operation = Operation.DELETE;
            await cosmosRepo.BulkProcessData(dataToDelete);

            string responseMessage = $"Total plans to delete, {plans.Count()}";

            log.LogInformation(responseMessage);
            return(new OkObjectResult(responseMessage));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            CosmosRepo cosmosRepo = new CosmosRepo(log);

            List <Plan>     plans        = CreateItems();
            BulkProcessData dataToImport = new BulkProcessData();

            dataToImport.ItemData = new List <ItemData>();
            foreach (Plan plan in plans)
            {
                dataToImport.ItemData.Add(new ItemData
                {
                    Item         = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(plan))),            // JsonConvert.SerializeObject(s),
                    ItemId       = plan.id,
                    PartitionKey = plan.__partitionKey
                });
            }
            dataToImport.Operation = Operation.IMPORT;
            await cosmosRepo.BulkProcessData(dataToImport);

            string responseMessage = $"Total Non available plans count, {plans.Where(m=>m.Status!="AVAILABLE").Count()}";

            log.LogInformation(responseMessage);
            return(new OkObjectResult(responseMessage));
        }
        public async Task BulkProcessData(BulkProcessData dataToImport)
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            //cancellationTokenSource.CancelAfter(runtimeInSeconds * 1000);
            CancellationToken cancellationToken = cancellationTokenSource.Token;

            try
            {
                ConcurrentBag <Task>   concurrentTasks = new ConcurrentBag <Task>();
                ConcurrentBag <string> completedTasks  = new ConcurrentBag <string>();

                foreach (var itemData in dataToImport.ItemData)
                {
                    if (dataToImport.Operation == Operation.IMPORT)
                    {
                        var tsk = this._container.UpsertItemStreamAsync(itemData.Item, new PartitionKey(itemData.PartitionKey), null, cancellationToken)
                                  .ContinueWith((Task <ResponseMessage> task) =>
                        {
                            logger.LogInformation("ReplaceItemStreamAsync itemId" + itemData.ItemId);
                            logger.LogInformation("ReplaceItemStreamAsync  task IsCompleted status " + task.IsCompleted);
                            logger.LogInformation("ReplaceItemStreamAsync  task IsCompletedSuccessfully status " + task.IsCompletedSuccessfully);
                            logger.LogInformation("ReplaceItemStreamAsync  task IsCanceled status " + task.IsCanceled);
                            logger.LogInformation("ReplaceItemStreamAsync  task IsFaulted status " + task.IsFaulted);
                            if (task.IsCompletedSuccessfully)
                            {
                                completedTasks.Add(itemData.ItemId);
                                if (itemData.Item != null)
                                {
                                    itemData.Item.Dispose();
                                }
                                if (task.Result != null)
                                {
                                    task.Result.Dispose();
                                }
                            }
                            else
                            {
                                logger.LogError("Error while updating document.", JsonConvert.SerializeObject(task.Result.Diagnostics));
                            }

                            task.Dispose();
                        });
                        concurrentTasks.Add(tsk);
                    }
                    else if (dataToImport.Operation == Operation.DELETE)
                    {
                        var tsk = this._container.DeleteItemStreamAsync(itemData.ItemId, new PartitionKey(itemData.PartitionKey), null, cancellationToken)
                                  .ContinueWith((Task <ResponseMessage> task) =>
                        {
                            if (task.IsCompletedSuccessfully)
                            {
                                completedTasks.Add(itemData.ItemId);
                                if (itemData.Item != null)
                                {
                                    itemData.Item.Dispose();
                                }
                                if (task.Result != null)
                                {
                                    task.Result.Dispose();
                                }
                            }
                            else
                            {
                                logger.LogError("Error while deleting document.", JsonConvert.SerializeObject(task.Result.Diagnostics));
                            }

                            task.Dispose();
                        });
                        concurrentTasks.Add(tsk);
                    }
                }
                await Task.WhenAll(concurrentTasks);

                //logger?.LogInformation(string.Format("Processed ItemIds {0}", JsonConvert.SerializeObject(completedTasks)));

                logger?.LogInformation(string.Format("Bulk processed {0}  out of {2} items for database {1}", completedTasks.Count, this._container.Database.Id, concurrentTasks.Count));
            }
            catch (Exception ex)
            {
                logger?.LogError(JsonConvert.SerializeObject(ex));
            }
        }