|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Net; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Azure; |
| 11 | +using Azure.Storage.Blobs; |
| 12 | +using Azure.Storage.Blobs.Models; |
| 13 | + |
| 14 | +namespace UpdateBlobProperties |
| 15 | +{ |
| 16 | + public class Updater : IUpdater |
| 17 | + { |
| 18 | + private const int MaxRetries = 3; |
| 19 | + |
| 20 | + private readonly BlobContainerClient _blobContainerClient; |
| 21 | + private readonly BlobInfo _blobInfo; |
| 22 | + private readonly ILogger<Updater> _logger; |
| 23 | + |
| 24 | + public Updater(BlobContainerClient blobContainerClient, BlobInfo blobInfo, ILogger<Updater> logger) |
| 25 | + { |
| 26 | + _blobContainerClient = blobContainerClient; |
| 27 | + _blobInfo = blobInfo; |
| 28 | + _logger = logger; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task UpdateBlobPropertiesAsync(PackageInfo packageInfo, CancellationToken token) |
| 32 | + { |
| 33 | + var blobName = _blobInfo.GetBlobName(packageInfo); |
| 34 | + var blobClient = _blobContainerClient.GetBlobClient(blobName); |
| 35 | + |
| 36 | + // Retries for concurrency control (Blob client is enabled with the retry policy) |
| 37 | + var retries = 0; |
| 38 | + while (retries < MaxRetries) |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + var response = await blobClient.GetPropertiesAsync(cancellationToken: token); |
| 43 | + if (!TryGetHeadersWhenBlobPropertiesNotMatched(response.Value, _blobInfo.GetUpdatedBlobProperties(), out var blobHttpHeaders)) |
| 44 | + { |
| 45 | + _logger.LogInformation("Blob properties of Package Id: {packageId} and Version: {packageVersion} (Blob Uri: {blobUri}) are matched.", |
| 46 | + packageInfo.Id, packageInfo.Version, blobClient.Uri); |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (await UpdateAsync(blobClient, blobHttpHeaders, response.Value.ETag, token)) |
| 52 | + { |
| 53 | + _logger.LogInformation("Blob properties of Package Id: {packageId} and Version: {packageVersion} (Blob Uri: {blobUri}) are updated successfully.", |
| 54 | + packageInfo.Id, packageInfo.Version, blobClient.Uri); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + _logger.LogInformation("The blob of Package Id: {packageId} and Version: {packageVersion} (Blob Uri: {blobUri}) has been updated since the last read.", |
| 61 | + packageInfo.Id, packageInfo.Version, blobClient.Uri); |
| 62 | + } |
| 63 | + } |
| 64 | + catch (RequestFailedException e) when (e.Status == (int)HttpStatusCode.NotFound) |
| 65 | + { |
| 66 | + _logger.LogInformation("The blob of Package Id: {packageId} and Version: {packageVersion} (Blob Uri: {blobUri}) does not exist.", |
| 67 | + packageInfo.Id, packageInfo.Version, blobClient.Uri); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + retries++; |
| 73 | + if (retries == MaxRetries) |
| 74 | + { |
| 75 | + throw new Exception($"Failed to update blob properties of Package Id: {packageInfo.Id} and Version: {packageInfo.Version} (Blob Uri: {blobClient.Uri}) after {MaxRetries} retries."); |
| 76 | + } |
| 77 | + |
| 78 | + await Task.Delay(TimeSpan.FromSeconds(retries * 5), token); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private async Task<bool> UpdateAsync(BlobClient blobClient, BlobHttpHeaders blobHttpHeaders, ETag eTag, CancellationToken token) |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + await blobClient.SetHttpHeadersAsync(blobHttpHeaders, new BlobRequestConditions() { IfMatch = eTag }, cancellationToken: token); |
| 87 | + |
| 88 | + return true; |
| 89 | + } |
| 90 | + catch (RequestFailedException e) when (e.Status == (int)HttpStatusCode.PreconditionFailed) |
| 91 | + { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private bool TryGetHeadersWhenBlobPropertiesNotMatched(BlobProperties blobProperties, IDictionary<string, string> updatedBlobProperties, |
| 97 | + out BlobHttpHeaders blobHttpHeaders) |
| 98 | + { |
| 99 | + blobHttpHeaders = new BlobHttpHeaders |
| 100 | + { |
| 101 | + CacheControl = blobProperties.CacheControl, |
| 102 | + ContentType = blobProperties.ContentType, |
| 103 | + ContentHash = blobProperties.ContentHash, |
| 104 | + ContentEncoding = blobProperties.ContentEncoding, |
| 105 | + ContentLanguage = blobProperties.ContentLanguage, |
| 106 | + ContentDisposition = blobProperties.ContentDisposition, |
| 107 | + }; |
| 108 | + |
| 109 | + var notMatched = false; |
| 110 | + if (updatedBlobProperties.TryGetValue(nameof(BlobProperties.CacheControl), out var value) && |
| 111 | + blobProperties.CacheControl != value) |
| 112 | + { |
| 113 | + blobHttpHeaders.CacheControl = value; |
| 114 | + notMatched = true; |
| 115 | + } |
| 116 | + |
| 117 | + return notMatched; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments