1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Collections . Immutable ;
7
+ using System . Threading . Tasks ;
8
+ using Microsoft . Azure . WebJobs . Script . Description ;
9
+ using Microsoft . Azure . WebJobs . Script . Workers . Rpc ;
10
+ using Microsoft . Extensions . Logging ;
11
+ using Microsoft . Extensions . Options ;
12
+
13
+ namespace Microsoft . Azure . WebJobs . Script . WebHost
14
+ {
15
+ internal class FunctionMetadataProvider : IFunctionMetadataProvider
16
+ {
17
+ private readonly IEnvironment _environment ;
18
+ private readonly ILogger < FunctionMetadataProvider > _logger ;
19
+ private IWorkerFunctionMetadataProvider _workerFunctionMetadataProvider ;
20
+ private IHostFunctionMetadataProvider _hostFunctionMetadataProvider ;
21
+
22
+ public FunctionMetadataProvider ( ILogger < FunctionMetadataProvider > logger , IWorkerFunctionMetadataProvider workerFunctionMetadataProvider , IHostFunctionMetadataProvider hostFunctionMetadataProvider )
23
+ {
24
+ _logger = logger ;
25
+ _workerFunctionMetadataProvider = workerFunctionMetadataProvider ;
26
+ _hostFunctionMetadataProvider = hostFunctionMetadataProvider ;
27
+ _environment = SystemEnvironment . Instance ;
28
+ }
29
+
30
+ public ImmutableDictionary < string , ImmutableArray < string > > FunctionErrors { get ; private set ; }
31
+
32
+ public async Task < ImmutableArray < FunctionMetadata > > GetFunctionMetadataAsync ( IEnumerable < RpcWorkerConfig > workerConfigs , IEnvironment environment , bool forceRefresh = false )
33
+ {
34
+ bool workerIndexing = Utility . CanWorkerIndex ( workerConfigs , _environment ) ;
35
+ if ( ! workerIndexing )
36
+ {
37
+ return await GetMetadataFromHostProvider ( workerConfigs , environment , forceRefresh ) ;
38
+ }
39
+
40
+ _logger . LogInformation ( "Worker indexing is enabled" ) ;
41
+
42
+ FunctionMetadataResult functionMetadataResult = await _workerFunctionMetadataProvider ? . GetFunctionMetadataAsync ( workerConfigs , SystemEnvironment . Instance , forceRefresh ) ;
43
+ FunctionErrors = _workerFunctionMetadataProvider . FunctionErrors ;
44
+
45
+ if ( functionMetadataResult . UseDefaultMetadataIndexing )
46
+ {
47
+ _logger . LogDebug ( "Fallback to host indexing as worker denied indexing" ) ;
48
+ return await GetMetadataFromHostProvider ( workerConfigs , environment , forceRefresh ) ;
49
+ }
50
+
51
+ return functionMetadataResult . Functions ;
52
+ }
53
+
54
+ private async Task < ImmutableArray < FunctionMetadata > > GetMetadataFromHostProvider ( IEnumerable < RpcWorkerConfig > workerConfigs , IEnvironment environment , bool forceRefresh = false )
55
+ {
56
+ var functions = await _hostFunctionMetadataProvider ? . GetFunctionMetadataAsync ( workerConfigs , environment , forceRefresh ) ;
57
+ FunctionErrors = _hostFunctionMetadataProvider . FunctionErrors ;
58
+ return functions ;
59
+ }
60
+ }
61
+ }
0 commit comments