private static async Task export(string connStrFrom, string sasUri) { Console.WriteLine("Started exporting..."); RegistryManager regMgr = RegistryManager.CreateFromConnectionString(connStrFrom); var jobs = regMgr.GetJobsAsync().Result; foreach (var item in jobs) { try { Debug.WriteLine(item.Status); if (item.Status == JobStatus.Running) { regMgr.CancelJobAsync(item.JobId).Wait(); } } catch (Exception ex) { } } var exportJob = await regMgr.ExportDevicesAsync(sasUri, false); while (true) { if (exportJob.Status == JobStatus.Completed || exportJob.Status == JobStatus.Failed || exportJob.Status == JobStatus.Cancelled) { if (exportJob.Status != JobStatus.Completed) { throw new Exception($"Export failed with status: {exportJob.Status}"); } break; } await Task.Delay(TimeSpan.FromSeconds(5)); } Console.WriteLine("Exporting completed."); }
private async Task CleanupDevices(int deviceCount) { Console.WriteLine($"Using storage container {_blobContainerClient.Name} for importing device delete requests."); // Step 1: Collect the devices that need to be deleted. IReadOnlyList <ExportImportDevice> devicesToBeDeleted = await GetDeviceIdsToDeleteAsync(deviceCount); Console.WriteLine($"Discovered {devicesToBeDeleted.Count} devices for deletion."); string currentJobId = null; if (devicesToBeDeleted.Any()) { try { // Step 2: Write the new import data back to the blob. using Stream devicesFile = ImportExportDevicesHelpers.BuildDevicesStream(devicesToBeDeleted); // Retrieve the SAS Uri that will be used to grant access to the storage containers. BlobClient blobClient = _blobContainerClient.GetBlobClient(ImportExportDevicesFileName); var uploadResult = await blobClient.UploadAsync(devicesFile, overwrite : true); string storageAccountSasUri = GetStorageAccountSasUriForCleanupJob(_blobContainerClient).ToString(); // Step 3: Call import using the same blob to delete all devices. JobProperties importDevicesToBeDeletedProperties = JobProperties .CreateForImportJob( inputBlobContainerUri: storageAccountSasUri, outputBlobContainerUri: storageAccountSasUri, inputBlobName: ImportExportDevicesFileName, storageAuthenticationType: StorageAuthenticationType.KeyBased); JobProperties importDevicesToBeDeletedJob = null; Stopwatch jobTimer = Stopwatch.StartNew(); do { try { importDevicesToBeDeletedJob = await _registryManager.ImportDevicesAsync(importDevicesToBeDeletedProperties); currentJobId = importDevicesToBeDeletedJob.JobId; break; } // Wait for pending jobs to finish. catch (JobQuotaExceededException) { Console.WriteLine($"JobQuotaExceededException... waiting."); await Task.Delay(s_waitDuration); } } while (jobTimer.Elapsed < s_maxJobDuration); // Wait until job is finished. jobTimer.Restart(); while (importDevicesToBeDeletedJob != null && jobTimer.Elapsed < s_maxJobDuration) { importDevicesToBeDeletedJob = await _registryManager.GetJobAsync(importDevicesToBeDeletedJob.JobId); if (s_completedJobs.Contains(importDevicesToBeDeletedJob.Status)) { // Job has finished executing. Console.WriteLine($"Job {importDevicesToBeDeletedJob.JobId} is {importDevicesToBeDeletedJob.Status}."); currentJobId = null; break; } Console.WriteLine($"Job {importDevicesToBeDeletedJob.JobId} is {importDevicesToBeDeletedJob.Status} after {jobTimer.Elapsed}."); await Task.Delay(s_waitDuration); } if (importDevicesToBeDeletedJob?.Status != JobStatus.Completed) { throw new Exception("Importing devices job failed; exiting."); } } finally { if (!String.IsNullOrWhiteSpace(currentJobId)) { Console.WriteLine($"Cancelling job {currentJobId}"); await _registryManager.CancelJobAsync(currentJobId); } } } // Step 4: Delete the storage container created. await _blobContainerClient.DeleteAsync(); Console.WriteLine($"Storage container {_blobContainerClient.Name} deleted."); }