11
11
using System . Text . RegularExpressions ;
12
12
using System . Threading ;
13
13
using System . Threading . Tasks ;
14
- using Microsoft . Azure . Storage ;
15
- using Microsoft . Azure . Storage . Blob ;
14
+ using Azure . Storage . Blobs ;
16
15
using Microsoft . Azure . WebJobs . Host . Executors ;
17
16
using Microsoft . Azure . WebJobs . Script . Description ;
18
17
using Microsoft . Azure . WebJobs . Script . Models ;
@@ -60,10 +59,11 @@ public class FunctionsSyncManager : IFunctionsSyncManager, IDisposable
60
59
private readonly HostNameProvider _hostNameProvider ;
61
60
private readonly IFunctionMetadataManager _functionMetadataManager ;
62
61
private readonly SemaphoreSlim _syncSemaphore = new SemaphoreSlim ( 1 , 1 ) ;
62
+ private readonly IAzureStorageProvider _azureStorageProvider ;
63
63
64
- private CloudBlockBlob _hashBlob ;
64
+ private BlobClient _hashBlobClient ;
65
65
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 )
67
67
{
68
68
_applicationHostOptions = applicationHostOptions ;
69
69
_logger = logger ;
@@ -75,6 +75,7 @@ public FunctionsSyncManager(IConfiguration configuration, IHostIdProvider hostId
75
75
_environment = environment ;
76
76
_hostNameProvider = hostNameProvider ;
77
77
_functionMetadataManager = functionMetadataManager ;
78
+ _azureStorageProvider = azureStorageProvider ;
78
79
}
79
80
80
81
internal bool ArmCacheEnabled
@@ -104,8 +105,8 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
104
105
{
105
106
await _syncSemaphore . WaitAsync ( ) ;
106
107
107
- var hashBlob = await GetHashBlobAsync ( ) ;
108
- if ( isBackgroundSync && hashBlob == null )
108
+ var hashBlobClient = await GetHashBlobAsync ( ) ;
109
+ if ( isBackgroundSync && hashBlobClient == null )
109
110
{
110
111
// short circuit before doing any work in background sync
111
112
// cases where we need to check/update hash but don't have
@@ -128,7 +129,7 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
128
129
string newHash = null ;
129
130
if ( isBackgroundSync )
130
131
{
131
- newHash = await CheckHashAsync ( hashBlob , payload . Content ) ;
132
+ newHash = await CheckHashAsync ( hashBlobClient , payload . Content ) ;
132
133
shouldSyncTriggers = newHash != null ;
133
134
}
134
135
@@ -137,7 +138,7 @@ public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync
137
138
var ( success , error ) = await SetTriggersAsync ( payload . Content ) ;
138
139
if ( success && newHash != null )
139
140
{
140
- await UpdateHashAsync ( hashBlob , newHash ) ;
141
+ await UpdateHashAsync ( hashBlobClient , newHash ) ;
141
142
}
142
143
result . Success = success ;
143
144
result . Error = error ;
@@ -193,7 +194,7 @@ internal static bool IsSyncTriggersEnvironment(IScriptWebHostEnvironment webHost
193
194
return true ;
194
195
}
195
196
196
- internal async Task < string > CheckHashAsync ( CloudBlockBlob hashBlob , string content )
197
+ internal async Task < string > CheckHashAsync ( BlobClient hashBlobClient , string content )
197
198
{
198
199
try
199
200
{
@@ -210,9 +211,13 @@ internal async Task<string> CheckHashAsync(CloudBlockBlob hashBlob, string conte
210
211
211
212
// get the last hash value if present
212
213
string lastHash = null ;
213
- if ( await hashBlob . ExistsAsync ( ) )
214
+ if ( await hashBlobClient . ExistsAsync ( ) )
214
215
{
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
+ }
216
221
_logger . LogDebug ( $ "SyncTriggers hash (Last='{ lastHash } ', Current='{ currentHash } ')") ;
217
222
}
218
223
@@ -234,13 +239,16 @@ internal async Task<string> CheckHashAsync(CloudBlockBlob hashBlob, string conte
234
239
return null ;
235
240
}
236
241
237
- internal async Task UpdateHashAsync ( CloudBlockBlob hashBlob , string hash )
242
+ internal async Task UpdateHashAsync ( BlobClient hashBlobClient , string hash )
238
243
{
239
244
try
240
245
{
241
246
// hash value has changed or was not yet stored
242
247
// 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
+ }
244
252
_logger . LogDebug ( $ "SyncTriggers hash updated to '{ hash } '") ;
245
253
}
246
254
catch ( Exception ex )
@@ -250,23 +258,19 @@ internal async Task UpdateHashAsync(CloudBlockBlob hashBlob, string hash)
250
258
}
251
259
}
252
260
253
- internal async Task < CloudBlockBlob > GetHashBlobAsync ( )
261
+ internal async Task < BlobClient > GetHashBlobAsync ( )
254
262
{
255
- if ( _hashBlob == null )
263
+ if ( _hashBlobClient == null )
256
264
{
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 ) )
261
266
{
262
267
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 ) ;
265
269
string hashBlobPath = $ "synctriggers/{ hostId } /last";
266
- _hashBlob = blobContainer . GetBlockBlobReference ( hashBlobPath ) ;
270
+ _hashBlobClient = blobContainerClient . GetBlobClient ( hashBlobPath ) ;
267
271
}
268
272
}
269
- return _hashBlob ;
273
+ return _hashBlobClient ;
270
274
}
271
275
272
276
public async Task < SyncTriggersPayload > GetSyncTriggersPayload ( )
0 commit comments