1111using System . Text . RegularExpressions ;
1212using System . Threading ;
1313using System . Threading . Tasks ;
14- using Microsoft . Azure . Storage ;
15- using Microsoft . Azure . Storage . Blob ;
14+ using Azure . Storage . Blobs ;
1615using Microsoft . Azure . WebJobs . Host . Executors ;
1716using Microsoft . Azure . WebJobs . Script . Description ;
1817using Microsoft . Azure . WebJobs . Script . Models ;
@@ -60,10 +59,11 @@ public class FunctionsSyncManager : IFunctionsSyncManager, IDisposable
6059 private readonly HostNameProvider _hostNameProvider ;
6160 private readonly IFunctionMetadataManager _functionMetadataManager ;
6261 private readonly SemaphoreSlim _syncSemaphore = new SemaphoreSlim ( 1 , 1 ) ;
62+ private readonly IAzureStorageProvider _azureStorageProvider ;
6363
64- private CloudBlockBlob _hashBlob ;
64+ private BlobClient _hashBlobClient ;
6565
66- public FunctionsSyncManager ( IConfiguration configuration , IHostIdProvider hostIdProvider , IOptionsMonitor < ScriptApplicationHostOptions > applicationHostOptions , ILogger < FunctionsSyncManager > logger , HttpClient httpClient , ISecretManagerProvider secretManagerProvider , IScriptWebHostEnvironment webHostEnvironment , IEnvironment environment , HostNameProvider hostNameProvider , IFunctionMetadataManager functionMetadataManager )
66+ public FunctionsSyncManager ( IConfiguration configuration , IHostIdProvider hostIdProvider , IOptionsMonitor < ScriptApplicationHostOptions > applicationHostOptions , ILogger < FunctionsSyncManager > logger , HttpClient httpClient , ISecretManagerProvider secretManagerProvider , IScriptWebHostEnvironment webHostEnvironment , IEnvironment environment , HostNameProvider hostNameProvider , IFunctionMetadataManager functionMetadataManager , IAzureStorageProvider azureStorageProvider )
6767 {
6868 _applicationHostOptions = applicationHostOptions ;
6969 _logger = logger ;
@@ -75,6 +75,7 @@ public FunctionsSyncManager(IConfiguration configuration, IHostIdProvider hostId
7575 _environment = environment ;
7676 _hostNameProvider = hostNameProvider ;
7777 _functionMetadataManager = functionMetadataManager ;
78+ _azureStorageProvider = azureStorageProvider ;
7879 }
7980
8081 internal bool ArmCacheEnabled
@@ -104,8 +105,8 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
104105 {
105106 await _syncSemaphore . WaitAsync ( ) ;
106107
107- var hashBlob = await GetHashBlobAsync ( ) ;
108- if ( isBackgroundSync && hashBlob == null )
108+ var hashBlobClient = await GetHashBlobAsync ( ) ;
109+ if ( isBackgroundSync && hashBlobClient == null )
109110 {
110111 // short circuit before doing any work in background sync
111112 // cases where we need to check/update hash but don't have
@@ -128,7 +129,7 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
128129 string newHash = null ;
129130 if ( isBackgroundSync )
130131 {
131- newHash = await CheckHashAsync ( hashBlob , payload . Content ) ;
132+ newHash = await CheckHashAsync ( hashBlobClient , payload . Content ) ;
132133 shouldSyncTriggers = newHash != null ;
133134 }
134135
@@ -137,7 +138,7 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
137138 var ( success , error ) = await SetTriggersAsync ( payload . Content ) ;
138139 if ( success && newHash != null )
139140 {
140- await UpdateHashAsync ( hashBlob , newHash ) ;
141+ await UpdateHashAsync ( hashBlobClient , newHash ) ;
141142 }
142143 result . Success = success ;
143144 result . Error = error ;
@@ -193,7 +194,7 @@ internal static bool IsSyncTriggersEnvironment(IScriptWebHostEnvironment webHost
193194 return true ;
194195 }
195196
196- internal async Task < string > CheckHashAsync ( CloudBlockBlob hashBlob , string content )
197+ internal async Task < string > CheckHashAsync ( BlobClient hashBlobClient , string content )
197198 {
198199 try
199200 {
@@ -210,9 +211,13 @@ internal async Task<string> CheckHashAsync(CloudBlockBlob hashBlob, string conte
210211
211212 // get the last hash value if present
212213 string lastHash = null ;
213- if ( await hashBlob . ExistsAsync ( ) )
214+ if ( await hashBlobClient . ExistsAsync ( ) )
214215 {
215- lastHash = await hashBlob . DownloadTextAsync ( ) ;
216+ var downloadResponse = await hashBlobClient . DownloadAsync ( ) ;
217+ using ( StreamReader reader = new StreamReader ( downloadResponse . Value . Content ) )
218+ {
219+ lastHash = reader . ReadToEnd ( ) ;
220+ }
216221 _logger . LogDebug ( $ "SyncTriggers hash (Last='{ lastHash } ', Current='{ currentHash } ')") ;
217222 }
218223
@@ -234,13 +239,16 @@ internal async Task<string> CheckHashAsync(CloudBlockBlob hashBlob, string conte
234239 return null ;
235240 }
236241
237- internal async Task UpdateHashAsync ( CloudBlockBlob hashBlob , string hash )
242+ internal async Task UpdateHashAsync ( BlobClient hashBlobClient , string hash )
238243 {
239244 try
240245 {
241246 // hash value has changed or was not yet stored
242247 // update the last hash value in storage
243- await hashBlob . UploadTextAsync ( hash ) ;
248+ using ( Stream stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( hash ) ) )
249+ {
250+ await hashBlobClient . UploadAsync ( stream ) ;
251+ }
244252 _logger . LogDebug ( $ "SyncTriggers hash updated to '{ hash } '") ;
245253 }
246254 catch ( Exception ex )
@@ -250,23 +258,19 @@ internal async Task UpdateHashAsync(CloudBlockBlob hashBlob, string hash)
250258 }
251259 }
252260
253- internal async Task < CloudBlockBlob > GetHashBlobAsync ( )
261+ internal async Task < BlobClient > GetHashBlobAsync ( )
254262 {
255- if ( _hashBlob == null )
263+ if ( _hashBlobClient == null )
256264 {
257- string storageConnectionString = _configuration . GetWebJobsConnectionString ( ConnectionStringNames . Storage ) ;
258- CloudStorageAccount account = null ;
259- if ( ! string . IsNullOrEmpty ( storageConnectionString ) &&
260- CloudStorageAccount . TryParse ( storageConnectionString , out account ) )
265+ if ( _azureStorageProvider . TryGetBlobServiceClientFromConnection ( out BlobServiceClient blobClient , ConnectionStringNames . Storage ) )
261266 {
262267 string hostId = await _hostIdProvider . GetHostIdAsync ( CancellationToken . None ) ;
263- CloudBlobClient blobClient = account . CreateCloudBlobClient ( ) ;
264- var blobContainer = blobClient . GetContainerReference ( ScriptConstants . AzureWebJobsHostsContainerName ) ;
268+ var blobContainerClient = blobClient . GetBlobContainerClient ( ScriptConstants . AzureWebJobsHostsContainerName ) ;
265269 string hashBlobPath = $ "synctriggers/{ hostId } /last";
266- _hashBlob = blobContainer . GetBlockBlobReference ( hashBlobPath ) ;
270+ _hashBlobClient = blobContainerClient . GetBlobClient ( hashBlobPath ) ;
267271 }
268272 }
269- return _hashBlob ;
273+ return _hashBlobClient ;
270274 }
271275
272276 public async Task < SyncTriggersPayload > GetSyncTriggersPayload ( )
0 commit comments