private async Task <bool> SaveDataToStorage(string collectionId, string key, DeploymentHistoryModel deploymentHistory, string tenantId)
        {
            bool isSuccess = false;
            var  value     = JsonConvert.SerializeObject(
                deploymentHistory,
                Formatting.None,
                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
            });

            KeyValueDocument document = new KeyValueDocument(collectionId, key, value, Guid.NewGuid().ToString());

            try
            {
                await this.client.CreateDocumentAsync(this.DocumentDbDatabaseId, this.GetPcsCollectionId(tenantId), document);

                isSuccess = true;
            }
            catch (Exception)
            {
            }

            return(isSuccess);
        }
        protected async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                // Get the list of tenants
                TableQuery <TenantMigrationModel> query   = new TableQuery <TenantMigrationModel>().Where(TableQuery.GenerateFilterConditionForBool("IsIotHubDeployed", QueryComparisons.Equal, true));
                List <TenantMigrationModel>       tenants = await this.tableStorageClient.QueryAsync <TenantMigrationModel>(TenantTable, query);

                // Verify against the list of processed tenants
                TableQuery <TenantMigrationModel> processedTenantsQuery = new TableQuery <TenantMigrationModel>().Where(TableQuery.GenerateFilterConditionForBool(nameof(TenantMigrationModel.IsMigrationCompleted), QueryComparisons.Equal, true));

                List <TenantMigrationModel> processedTenants = await this.tableStorageClient.QueryAsync <TenantMigrationModel>(TenantMigrationStatus, processedTenantsQuery);

                var tenantsToMigrate = tenants.Select(t => t.TenantId).Except(processedTenants.Select(t => t.TenantId));

                if (tenantsToMigrate != null && tenantsToMigrate.Count() > 0)
                {
                    string tenantToMigrate = tenantsToMigrate.First();

                    // Get Deployments for Name
                    Dictionary <string, string> deployments = await this.GetDeployments(tenantToMigrate, "deployments");

                    bool isTenantMigrationCompleted = false;
                    while (!isTenantMigrationCompleted)
                    {
                        // get the data from tenant for deploymentdevices collection
                        IEnumerable <ValueServiceModel> deploymentImpactedDevicesData = await this.GetDataForMigration(tenantToMigrate, DeploymentDevicePropertiesCollection);

                        if (deploymentImpactedDevicesData != null && deploymentImpactedDevicesData.Count() > 0)
                        {
                            // Convert the data into new model to be saved
                            foreach (ValueServiceModel data in deploymentImpactedDevicesData)
                            {
                                DeploymentHistoryModel deviceDeploymentHistory = new DeploymentHistoryModel();
                                deviceDeploymentHistory.DeploymentId           = data.CollectionId.Substring(18);
                                deviceDeploymentHistory.DeviceId               = data.Key;
                                deviceDeploymentHistory.DeploymentName         = deployments.ContainsKey(deviceDeploymentHistory.DeploymentId) ? deployments[deviceDeploymentHistory.DeploymentId] : string.Empty;
                                deviceDeploymentHistory.LastUpdatedDateTimeUtc = data.Timestamp.UtcDateTime;
                                deviceDeploymentHistory.PreviousFirmwareTwin   = null;
                                deviceDeploymentHistory.Twin = JsonConvert.DeserializeObject <TwinServiceModel>(data.Data);

                                // save the record to new collection
                                var isSuccess = await this.SaveDataToStorage(string.Format(DeviceDeploymentHistory, deviceDeploymentHistory.DeviceId), deviceDeploymentHistory.DeploymentId, deviceDeploymentHistory, tenantToMigrate);

                                if (isSuccess)
                                {
                                    // Delete the existing data once saved.
                                    await this.DeleteDataFromStorage(tenantToMigrate, data.CollectionId, data.Key);
                                }
                            }
                        }
                        else
                        {
                            IEnumerable <ValueServiceModel> deploymentImpactedDevicesHistoryData = await this.GetDataForMigration(tenantToMigrate, DeploymentHistoryPropertiesCollection);

                            if (deploymentImpactedDevicesHistoryData != null && deploymentImpactedDevicesHistoryData.Count() > 0)
                            {
                                // Convert the data into new model to be saved
                                foreach (ValueServiceModel data in deploymentImpactedDevicesData)
                                {
                                    DeploymentHistoryModel deviceDeploymentHistory = new DeploymentHistoryModel();
                                    deviceDeploymentHistory.DeploymentId           = data.CollectionId.Substring(18, data.CollectionId.Length - (36 + 1) - 18);
                                    deviceDeploymentHistory.DeviceId               = data.Key;
                                    deviceDeploymentHistory.DeploymentName         = deployments.ContainsKey(deviceDeploymentHistory.DeploymentId) ? deployments[deviceDeploymentHistory.DeploymentId] : string.Empty;
                                    deviceDeploymentHistory.LastUpdatedDateTimeUtc = data.Timestamp.UtcDateTime;
                                    deviceDeploymentHistory.PreviousFirmwareTwin   = null;
                                    deviceDeploymentHistory.Twin = JsonConvert.DeserializeObject <TwinServiceModel>(data.Data);

                                    // save the record to new collection
                                    var isSuccess = await this.SaveDataToStorage(string.Format(DeviceDeploymentHistory, deviceDeploymentHistory.DeviceId), deviceDeploymentHistory.DeploymentId, deviceDeploymentHistory, tenantToMigrate);

                                    if (isSuccess)
                                    {
                                        // Delete the existing data once saved.
                                        await this.DeleteDataFromStorage(tenantToMigrate, data.CollectionId, data.Key);
                                    }
                                }
                            }
                            else
                            {
                                isTenantMigrationCompleted = true;
                                break;
                            }
                        }

                        await Task.Delay(30 * 1000, stoppingToken);
                    }

                    // Update the TenantMigrationStatus to true;
                    await this.InsertTenantMigrationStatus(tenantToMigrate);
                }

                await Task.Delay(300 * 1000, stoppingToken);
            }
        }