Skip to content

Commit 2d8bd38

Browse files
authored
Using hostMetadataProvider for CreateOrUpdate call in FunctionController (#10678)
1 parent ba3e9ef commit 2d8bd38

11 files changed

+63
-39
lines changed

release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
- My change description (#PR)
55
-->
66

7-
- Setting force refersh to false for CreateOrUpdate call (#10668)
7+
- Using hostMetadataProvider for CreateOrUpdate call in FunctionController (#10678)

src/WebJobs.Script.WebHost/Management/WebFunctionsManager.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
using System.Linq;
88
using System.Net.Http;
99
using System.Threading.Tasks;
10+
using Azure;
11+
using Azure.Core;
1012
using Microsoft.AspNetCore.Http;
1113
using Microsoft.Azure.WebJobs.Script.Description;
1214
using Microsoft.Azure.WebJobs.Script.Management.Models;
1315
using Microsoft.Azure.WebJobs.Script.WebHost.Extensions;
16+
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
17+
using Microsoft.Extensions.DependencyInjection;
1418
using Microsoft.Extensions.Logging;
1519
using Microsoft.Extensions.Options;
1620
using Newtonsoft.Json;
@@ -27,8 +31,11 @@ public class WebFunctionsManager : IWebFunctionsManager
2731
private readonly IFunctionsSyncManager _functionsSyncManager;
2832
private readonly HostNameProvider _hostNameProvider;
2933
private readonly IFunctionMetadataManager _functionMetadataManager;
34+
private readonly IHostFunctionMetadataProvider _hostFunctionMetadataProvider;
35+
private readonly IOptionsMonitor<LanguageWorkerOptions> _languageWorkerOptions;
3036

31-
public WebFunctionsManager(IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, ILoggerFactory loggerFactory, IHttpClientFactory httpClientFactory, ISecretManagerProvider secretManagerProvider, IFunctionsSyncManager functionsSyncManager, HostNameProvider hostNameProvider, IFunctionMetadataManager functionMetadataManager)
37+
public WebFunctionsManager(IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, ILoggerFactory loggerFactory, IHttpClientFactory httpClientFactory, ISecretManagerProvider secretManagerProvider, IFunctionsSyncManager functionsSyncManager, HostNameProvider hostNameProvider, IFunctionMetadataManager functionMetadataManager, IHostFunctionMetadataProvider hostFunctionMetadataProvider,
38+
IOptionsMonitor<LanguageWorkerOptions> languageWorkerOptions)
3239
{
3340
_applicationHostOptions = applicationHostOptions;
3441
_logger = loggerFactory?.CreateLogger(ScriptConstants.LogCategoryHostGeneral);
@@ -37,6 +44,8 @@ public WebFunctionsManager(IOptionsMonitor<ScriptApplicationHostOptions> applica
3744
_functionsSyncManager = functionsSyncManager;
3845
_hostNameProvider = hostNameProvider;
3946
_functionMetadataManager = functionMetadataManager;
47+
_hostFunctionMetadataProvider = hostFunctionMetadataProvider;
48+
_languageWorkerOptions = languageWorkerOptions;
4049
}
4150

4251
public async Task<IEnumerable<FunctionMetadataResponse>> GetFunctionsMetadata(bool includeProxies)
@@ -144,12 +153,21 @@ await functionMetadata
144153
await FileUtility.WriteAsync(dataFilePath, functionMetadata.TestData);
145154
}
146155

156+
// Using HostFunctionMetadataProvider instead of IFunctionMetadataManager. More details logged here https://github.com/Azure/azure-functions-host/issues/10691
157+
var metadata = (await _hostFunctionMetadataProvider.GetFunctionMetadataAsync(_languageWorkerOptions.CurrentValue.WorkerConfigs, true))
158+
.FirstOrDefault(metadata => Utility.FunctionNamesMatch(metadata.Name, name));
159+
160+
bool success = false;
161+
FunctionMetadataResponse functionMetadataResult = null;
162+
if (metadata != null)
163+
{
164+
functionMetadataResult = await GetFunctionMetadataResponseAsync(metadata, hostOptions, request);
165+
success = true;
166+
}
167+
147168
// we need to sync triggers if config changed, or the files changed
148169
await _functionsSyncManager.TrySyncTriggersAsync();
149170

150-
// Setting force refresh to false as host restart causes a refersh already
151-
(var success, var functionMetadataResult) = await TryGetFunction(name, request, false);
152-
153171
return (success, configChanged, functionMetadataResult);
154172
}
155173

@@ -179,9 +197,8 @@ await functionMetadata
179197

180198
if (functionMetadata != null)
181199
{
182-
string routePrefix = await GetRoutePrefix(hostOptions.RootScriptPath);
183-
var baseUrl = $"{request.Scheme}://{request.Host}";
184-
return (true, await functionMetadata.ToFunctionMetadataResponse(hostOptions, routePrefix, baseUrl));
200+
var functionMetadataResponse = await GetFunctionMetadataResponseAsync(functionMetadata, hostOptions, request);
201+
return (true, functionMetadataResponse);
185202
}
186203
else
187204
{
@@ -228,6 +245,13 @@ private void DeleteFunctionArtifacts(FunctionMetadataResponse function)
228245
}
229246
}
230247

248+
private static async Task<FunctionMetadataResponse> GetFunctionMetadataResponseAsync(FunctionMetadata functionMetadata, ScriptJobHostOptions hostOptions, HttpRequest request)
249+
{
250+
string routePrefix = await GetRoutePrefix(hostOptions.RootScriptPath);
251+
var baseUrl = $"{request.Scheme}://{request.Host}";
252+
return await functionMetadata.ToFunctionMetadataResponse(hostOptions, routePrefix, baseUrl);
253+
}
254+
231255
// TODO : Due to lifetime scoping issues (this service lifetime is longer than the lifetime
232256
// of HttpOptions sourced from host.json) we're reading the http route prefix anew each time
233257
// to ensure we have the latest configured value.

src/WebJobs.Script/Host/FunctionMetadataManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ internal ImmutableArray<FunctionMetadata> LoadFunctionMetadata(bool forceRefresh
145145

146146
ImmutableArray<FunctionMetadata> immutableFunctionMetadata;
147147

148-
immutableFunctionMetadata = _functionMetadataProvider.GetFunctionMetadataAsync(workerConfigs, _environment, forceRefresh).GetAwaiter().GetResult();
148+
immutableFunctionMetadata = _functionMetadataProvider.GetFunctionMetadataAsync(workerConfigs, forceRefresh).GetAwaiter().GetResult();
149149

150150
var functionMetadataList = new List<FunctionMetadata>();
151151
_functionErrors = new Dictionary<string, ICollection<string>>();

src/WebJobs.Script/Host/FunctionMetadataProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public FunctionMetadataProvider(ILogger<FunctionMetadataProvider> logger, IWorke
3232

3333
public ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors { get; private set; }
3434

35-
public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, IEnvironment environment, bool forceRefresh = false)
35+
public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, bool forceRefresh = false)
3636
{
3737
bool workerIndexing = Utility.CanWorkerIndex(workerConfigs, _environment, _functionsHostingConfigOptions);
3838
if (!workerIndexing)
3939
{
40-
return await GetMetadataFromHostProvider(workerConfigs, environment, forceRefresh);
40+
return await GetMetadataFromHostProvider(workerConfigs, forceRefresh);
4141
}
4242

4343
_logger.LogInformation("Worker indexing is enabled");
@@ -48,15 +48,15 @@ public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEn
4848
if (functionMetadataResult.UseDefaultMetadataIndexing)
4949
{
5050
_logger.LogDebug("Fallback to host indexing as worker denied indexing");
51-
return await GetMetadataFromHostProvider(workerConfigs, environment, forceRefresh);
51+
return await GetMetadataFromHostProvider(workerConfigs, forceRefresh);
5252
}
5353

5454
return functionMetadataResult.Functions;
5555
}
5656

57-
private async Task<ImmutableArray<FunctionMetadata>> GetMetadataFromHostProvider(IEnumerable<RpcWorkerConfig> workerConfigs, IEnvironment environment, bool forceRefresh = false)
57+
private async Task<ImmutableArray<FunctionMetadata>> GetMetadataFromHostProvider(IEnumerable<RpcWorkerConfig> workerConfigs, bool forceRefresh = false)
5858
{
59-
var functions = await _hostFunctionMetadataProvider?.GetFunctionMetadataAsync(workerConfigs, environment, forceRefresh);
59+
var functions = await _hostFunctionMetadataProvider?.GetFunctionMetadataAsync(workerConfigs, forceRefresh);
6060
FunctionErrors = _hostFunctionMetadataProvider.FunctionErrors;
6161
return functions;
6262
}

src/WebJobs.Script/Host/HostFunctionMetadataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public HostFunctionMetadataProvider(IOptionsMonitor<ScriptApplicationHostOptions
4242
public ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors
4343
=> _functionErrors.ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.ToImmutableArray());
4444

45-
public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, IEnvironment environment, bool forceRefresh)
45+
public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, bool forceRefresh)
4646
{
4747
_functions = default(ImmutableArray<FunctionMetadata>);
4848

src/WebJobs.Script/Host/IFunctionMetadataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public interface IFunctionMetadataProvider
1313
{
1414
ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors { get; }
1515

16-
Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, IEnvironment environment, bool forceRefresh = false);
16+
Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, bool forceRefresh = false);
1717
}
1818
}

src/WebJobs.Script/Host/IHostFunctionMetadataProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace Microsoft.Azure.WebJobs.Script
1212
/// <summary>
1313
/// Defines an interface for fetching function metadata from function.json files
1414
/// </summary>
15-
internal interface IHostFunctionMetadataProvider
15+
public interface IHostFunctionMetadataProvider
1616
{
1717
ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors { get; }
1818

1919
/// <summary>
2020
/// Reads function metadata from function.json files present along with each function
2121
/// </summary>
22-
Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, IEnvironment environment, bool forceRefresh = false);
22+
Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync(IEnumerable<RpcWorkerConfig> workerConfigs, bool forceRefresh = false);
2323
}
2424
}

0 commit comments

Comments
 (0)