1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
+ using System . Collections . Generic ;
5
+ using System . IO ;
4
6
using System . Net ;
5
7
using System . Net . Sockets ;
6
8
using Microsoft . Azure . WebJobs . Script . Configuration ;
7
9
using Microsoft . Azure . WebJobs . Script . Diagnostics ;
8
10
using Microsoft . Extensions . Configuration ;
9
11
using Microsoft . Extensions . Logging ;
10
12
using Microsoft . Extensions . Options ;
13
+ using Newtonsoft . Json ;
14
+ using Newtonsoft . Json . Linq ;
11
15
12
16
namespace Microsoft . Azure . WebJobs . Script . Workers . Http
13
17
{
@@ -17,6 +21,8 @@ internal class HttpWorkerOptionsSetup : IConfigureOptions<HttpWorkerOptions>
17
21
private ILogger _logger ;
18
22
private IMetricsLogger _metricsLogger ;
19
23
private ScriptJobHostOptions _scriptJobHostOptions ;
24
+ private string argumentsSectionName = $ "{ WorkerConstants . WorkerDescription } :arguments";
25
+ private string workerArgumentsSectionName = $ "{ WorkerConstants . WorkerDescription } :workerArguments";
20
26
21
27
public HttpWorkerOptionsSetup ( IOptions < ScriptJobHostOptions > scriptJobHostOptions , IConfiguration configuration , ILoggerFactory loggerFactory , IMetricsLogger metricsLogger )
22
28
{
@@ -30,27 +36,92 @@ public void Configure(HttpWorkerOptions options)
30
36
{
31
37
IConfigurationSection jobHostSection = _configuration . GetSection ( ConfigurationSectionNames . JobHost ) ;
32
38
var httpWorkerSection = jobHostSection . GetSection ( ConfigurationSectionNames . HttpWorker ) ;
39
+ var customHandlerSection = jobHostSection . GetSection ( ConfigurationSectionNames . CustomHandler ) ;
40
+
41
+ if ( httpWorkerSection . Exists ( ) && customHandlerSection . Exists ( ) )
42
+ {
43
+ _logger . LogWarning ( $ "Both { ConfigurationSectionNames . HttpWorker } and { ConfigurationSectionNames . CustomHandler } sections are spefified in { ScriptConstants . HostMetadataFileName } file. { ConfigurationSectionNames . CustomHandler } takes precedence.") ;
44
+ }
45
+
46
+ if ( customHandlerSection . Exists ( ) )
47
+ {
48
+ _metricsLogger . LogEvent ( MetricEventNames . CustomHandlerConfiguration ) ;
49
+ ConfigureWorkerDescription ( options , customHandlerSection ) ;
50
+ return ;
51
+ }
52
+
33
53
if ( httpWorkerSection . Exists ( ) )
34
54
{
35
- _metricsLogger . LogEvent ( MetricEventNames . HttpWorker ) ;
36
- httpWorkerSection . Bind ( options ) ;
37
- HttpWorkerDescription httpWorkerDescription = options . Description ;
55
+ // TODO: Add aka.ms/link to new docs
56
+ _logger . LogWarning ( $ "Section { ConfigurationSectionNames . HttpWorker } will be deprecated. Please use { ConfigurationSectionNames . CustomHandler } section.") ;
57
+ ConfigureWorkerDescription ( options , httpWorkerSection ) ;
58
+ // Explicity set this empty to differentiate between customHandler and httpWorker options.
59
+ options . Type = string . Empty ;
60
+ }
61
+ }
62
+
63
+ private void ConfigureWorkerDescription ( HttpWorkerOptions options , IConfigurationSection workerSection )
64
+ {
65
+ workerSection . Bind ( options ) ;
66
+ HttpWorkerDescription httpWorkerDescription = options . Description ;
67
+
68
+ if ( httpWorkerDescription == null )
69
+ {
70
+ throw new HostConfigurationException ( $ "Missing worker Description.") ;
71
+ }
72
+
73
+ var argumentsList = GetArgumentList ( workerSection , argumentsSectionName ) ;
74
+ if ( argumentsList != null )
75
+ {
76
+ httpWorkerDescription . Arguments = argumentsList ;
77
+ }
78
+
79
+ var workerArgumentList = GetArgumentList ( workerSection , workerArgumentsSectionName ) ;
80
+ if ( workerArgumentList != null )
81
+ {
82
+ httpWorkerDescription . WorkerArguments = workerArgumentList ;
83
+ }
84
+
85
+ httpWorkerDescription . ApplyDefaultsAndValidate ( _scriptJobHostOptions . RootScriptPath , _logger ) ;
38
86
39
- if ( httpWorkerDescription == null )
87
+ // Set default working directory to function app root.
88
+ if ( string . IsNullOrEmpty ( httpWorkerDescription . WorkingDirectory ) )
89
+ {
90
+ httpWorkerDescription . WorkingDirectory = _scriptJobHostOptions . RootScriptPath ;
91
+ }
92
+ else
93
+ {
94
+ // Compute working directory relative to fucntion app root.
95
+ if ( ! Path . IsPathRooted ( httpWorkerDescription . WorkingDirectory ) )
40
96
{
41
- throw new HostConfigurationException ( $ "Missing WorkerDescription for HttpWorker" ) ;
97
+ httpWorkerDescription . WorkingDirectory = Path . Combine ( _scriptJobHostOptions . RootScriptPath , httpWorkerDescription . WorkingDirectory ) ;
42
98
}
43
- httpWorkerDescription . ApplyDefaultsAndValidate ( _scriptJobHostOptions . RootScriptPath , _logger ) ;
44
- options . Arguments = new WorkerProcessArguments ( )
45
- {
46
- ExecutablePath = options . Description . DefaultExecutablePath ,
47
- WorkerPath = options . Description . DefaultWorkerPath
48
- } ;
99
+ }
100
+
101
+ options . Arguments = new WorkerProcessArguments ( )
102
+ {
103
+ ExecutablePath = options . Description . DefaultExecutablePath ,
104
+ WorkerPath = options . Description . DefaultWorkerPath
105
+ } ;
106
+
107
+ options . Arguments . ExecutableArguments . AddRange ( options . Description . Arguments ) ;
108
+ options . Port = GetUnusedTcpPort ( ) ;
109
+ }
49
110
50
- options . Arguments . ExecutableArguments . AddRange ( options . Description . Arguments ) ;
51
- options . Port = GetUnusedTcpPort ( ) ;
52
- _logger . LogDebug ( "Configured httpWorker with {DefaultExecutablePath}: {exepath} with arguments {args}" , nameof ( options . Description . DefaultExecutablePath ) , options . Description . DefaultExecutablePath , options . Arguments ) ;
111
+ private static List < string > GetArgumentList ( IConfigurationSection httpWorkerSection , string argumentSectionName )
112
+ {
113
+ var argumentsSection = httpWorkerSection . GetSection ( argumentSectionName ) ;
114
+ if ( argumentsSection . Exists ( ) && argumentsSection ? . Value != null )
115
+ {
116
+ try
117
+ {
118
+ return JsonConvert . DeserializeObject < List < string > > ( argumentsSection . Value ) ;
119
+ }
120
+ catch
121
+ {
122
+ }
53
123
}
124
+ return null ;
54
125
}
55
126
56
127
internal static int GetUnusedTcpPort ( )
0 commit comments