private async Task AnonymizeSingleBlobInJsonFormatAsync(BlobClient inputBlobClient, BlockBlobClient outputBlobClient, string blobName, string inputFolderPrefix)
        {
            try
            {
                using Stream contentStream = await OperationExecutionHelper.InvokeWithTimeoutRetryAsync <Stream>(async () =>
                {
                    Stream contentStream = new MemoryStream();
                    await inputBlobClient.DownloadToAsync(contentStream).ConfigureAwait(false);
                    contentStream.Position = 0;

                    return(contentStream);
                },
                                                                                                                 TimeSpan.FromSeconds(FhirAzureConstants.DefaultBlockDownloadTimeoutInSeconds),
                                                                                                                 FhirAzureConstants.DefaultBlockDownloadTimeoutRetryCount,
                                                                                                                 isRetrableException : OperationExecutionHelper.IsRetrableException).ConfigureAwait(false);

                using (var reader = new StreamReader(contentStream))
                {
                    string input = await reader.ReadToEndAsync();

                    var engine   = AnonymizerEngine.CreateWithFileContext(_configFile, blobName, inputFolderPrefix);
                    var settings = new AnonymizerSettings()
                    {
                        IsPrettyOutput = true
                    };
                    string output = engine.AnonymizeJson(input, settings);

                    using (MemoryStream outputStream = new MemoryStream(reader.CurrentEncoding.GetBytes(output)))
                    {
                        await OperationExecutionHelper.InvokeWithTimeoutRetryAsync(async() =>
                        {
                            outputStream.Position     = 0;
                            using MemoryStream stream = new MemoryStream();
                            await outputStream.CopyToAsync(stream).ConfigureAwait(false);
                            stream.Position = 0;

                            return(await outputBlobClient.UploadAsync(stream).ConfigureAwait(false));
                        },
                                                                                   TimeSpan.FromSeconds(FhirAzureConstants.DefaultBlockUploadTimeoutInSeconds),
                                                                                   FhirAzureConstants.DefaultBlockUploadTimeoutRetryCount,
                                                                                   isRetrableException : OperationExecutionHelper.IsRetrableException).ConfigureAwait(false);
                    }

                    Console.WriteLine($"[{blobName}]: Anonymize completed.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[{blobName}]: Anonymize failed, you can find detail error message in stderr.txt.");
                Console.Error.WriteLine($"[{blobName}]: Failed to anonymize blob. \nErrorMessage: {ex.Message}\n Details: {ex.ToString()} \nStackTrace: {ex.StackTrace}");
            }
        }
Exemple #2
0
        public async Task GivenFunctionWithRetriableException_WhenExecute_OperationShouldBeretried()
        {
            int retryCount          = 3;
            Func <Task <int> > func = () =>
            {
                if (retryCount == 0)
                {
                    return(Task.FromResult(1));
                }
                retryCount--;
                throw new IOException();
            };

            await OperationExecutionHelper.InvokeWithTimeoutRetryAsync <int>(func, TimeSpan.FromSeconds(1), 3, 2, OperationExecutionHelper.IsRetrableException);

            Assert.AreEqual(0, retryCount);
        }
Exemple #3
0
        public async Task GivenFunctionWithLongExecutionTime_WhenExecute_OperationShouldBeretried()
        {
            int retryCount          = 3;
            Func <Task <int> > func = async() =>
            {
                if (retryCount == 0)
                {
                    return(1);
                }
                retryCount--;
                await Task.Delay(TimeSpan.FromSeconds(10));

                return(0);
            };

            await OperationExecutionHelper.InvokeWithTimeoutRetryAsync <int>(func, TimeSpan.FromSeconds(1), 3, 2, OperationExecutionHelper.IsRetrableException);

            Assert.AreEqual(0, retryCount);
        }