@@ -31,10 +31,53 @@ public async static Task Main(string[] args)
31
31
LogLevel . Information ,
32
32
string . Format ( PowerShellWorkerStrings . PowerShellWorkerVersion , typeof ( Worker ) . Assembly . GetName ( ) . Version ) ) ;
33
33
34
- WorkerArguments arguments = null ;
35
- Parser . Default . ParseArguments < WorkerArguments > ( args )
36
- . WithParsed ( ops => arguments = ops )
37
- . WithNotParsed ( err => Environment . Exit ( 1 ) ) ;
34
+ var workerOptions = new WorkerOptions ( ) ;
35
+
36
+ var parser = new Parser ( settings =>
37
+ {
38
+ settings . EnableDashDash = true ;
39
+ settings . IgnoreUnknownArguments = true ;
40
+ } ) ;
41
+ parser . ParseArguments < WorkerArguments > ( args )
42
+ . WithParsed ( workerArgs =>
43
+ {
44
+ // TODO: Remove parsing old command-line arguments that are not prefixed with functions-<argumentname>
45
+ // for more information, see https://github.com/Azure/azure-functions-powershell-worker/issues/995
46
+ workerOptions . WorkerId = workerArgs . FunctionsWorkerId ?? workerArgs . WorkerId ;
47
+ workerOptions . RequestId = workerArgs . FunctionsRequestId ?? workerArgs . RequestId ;
48
+
49
+ if ( ! string . IsNullOrWhiteSpace ( workerArgs . FunctionsUri ) )
50
+ {
51
+ try
52
+ {
53
+ // TODO: Update WorkerOptions to have a URI property instead of host name and port number
54
+ // for more information, see https://github.com/Azure/azure-functions-powershell-worker/issues/994
55
+ var uri = new Uri ( workerArgs . FunctionsUri ) ;
56
+ workerOptions . Host = uri . Host ;
57
+ workerOptions . Port = uri . Port ;
58
+ }
59
+ catch ( UriFormatException formatEx )
60
+ {
61
+ var message = $ "Invalid URI format: { workerArgs . FunctionsUri } . Error message: { formatEx . Message } ";
62
+ throw new ArgumentException ( message , nameof ( workerArgs . FunctionsUri ) ) ;
63
+ }
64
+ }
65
+ else
66
+ {
67
+ workerOptions . Host = workerArgs . Host ;
68
+ workerOptions . Port = workerArgs . Port ;
69
+ }
70
+
71
+ // Validate workerOptions
72
+ ValidateProperty ( "WorkerId" , workerOptions . WorkerId ) ;
73
+ ValidateProperty ( "RequestId" , workerOptions . RequestId ) ;
74
+ ValidateProperty ( "Host" , workerOptions . Host ) ;
75
+
76
+ if ( workerOptions . Port <= 0 )
77
+ {
78
+ throw new ArgumentException ( "Port number has not been initialized" , nameof ( workerOptions . Port ) ) ;
79
+ }
80
+ } ) ;
38
81
39
82
// Create the very first Runspace so the debugger has the target to attach to.
40
83
// This PowerShell instance is shared by the first PowerShellManager instance created in the pool,
@@ -44,14 +87,14 @@ public async static Task Main(string[] args)
44
87
LogPowerShellVersion ( pwshVersion ) ;
45
88
WarmUpPowerShell ( firstPowerShellInstance ) ;
46
89
47
- var msgStream = new MessagingStream ( arguments . Host , arguments . Port ) ;
90
+ var msgStream = new MessagingStream ( workerOptions . Host , workerOptions . Port ) ;
48
91
var requestProcessor = new RequestProcessor ( msgStream , firstPowerShellInstance , pwshVersion ) ;
49
92
50
93
// Send StartStream message
51
94
var startedMessage = new StreamingMessage ( )
52
95
{
53
- RequestId = arguments . RequestId ,
54
- StartStream = new StartStream ( ) { WorkerId = arguments . WorkerId }
96
+ RequestId = workerOptions . RequestId ,
97
+ StartStream = new StartStream ( ) { WorkerId = workerOptions . WorkerId }
55
98
} ;
56
99
57
100
msgStream . Write ( startedMessage ) ;
@@ -81,23 +124,48 @@ private static void LogPowerShellVersion(string pwshVersion)
81
124
var message = string . Format ( PowerShellWorkerStrings . PowerShellVersion , pwshVersion ) ;
82
125
RpcLogger . WriteSystemLog ( LogLevel . Information , message ) ;
83
126
}
127
+
128
+ private static void ValidateProperty ( string name , string value )
129
+ {
130
+ if ( string . IsNullOrWhiteSpace ( value ) )
131
+ {
132
+ throw new ArgumentException ( $ "{ name } is null or empty", name ) ;
133
+ }
134
+ }
84
135
}
85
136
86
137
internal class WorkerArguments
87
138
{
88
- [ Option ( "host" , Required = true , HelpText = "IP Address used to connect to the Host via gRPC." ) ]
139
+ [ Option ( "host" , Required = false , HelpText = "IP Address used to connect to the Host via gRPC." ) ]
89
140
public string Host { get ; set ; }
90
141
91
- [ Option ( "port" , Required = true , HelpText = "Port used to connect to the Host via gRPC." ) ]
142
+ [ Option ( "port" , Required = false , HelpText = "Port used to connect to the Host via gRPC." ) ]
92
143
public int Port { get ; set ; }
93
144
94
- [ Option ( "workerId" , Required = true , HelpText = "Worker ID assigned to this language worker." ) ]
145
+ [ Option ( "workerId" , Required = false , HelpText = "Worker ID assigned to this language worker." ) ]
95
146
public string WorkerId { get ; set ; }
96
147
97
- [ Option ( "requestId" , Required = true , HelpText = "Request ID used for gRPC communication with the Host." ) ]
148
+ [ Option ( "requestId" , Required = false , HelpText = "Request ID used for gRPC communication with the Host." ) ]
98
149
public string RequestId { get ; set ; }
99
150
100
- [ Option ( "grpcMaxMessageLength" , Required = false , HelpText = "[Deprecated and ignored] gRPC Maximum message size." ) ]
101
- public int MaxMessageLength { get ; set ; }
151
+ [ Option ( "functions-uri" , Required = false , HelpText = "URI with IP Address and Port used to connect to the Host via gRPC." ) ]
152
+ public string FunctionsUri { get ; set ; }
153
+
154
+ [ Option ( "functions-workerid" , Required = false , HelpText = "Worker ID assigned to this language worker." ) ]
155
+ public string FunctionsWorkerId { get ; set ; }
156
+
157
+ [ Option ( "functions-requestid" , Required = false , HelpText = "Request ID used for gRPC communication with the Host." ) ]
158
+ public string FunctionsRequestId { get ; set ; }
159
+ }
160
+
161
+ internal class WorkerOptions
162
+ {
163
+ public string Host { get ; set ; }
164
+
165
+ public int Port { get ; set ; }
166
+
167
+ public string WorkerId { get ; set ; }
168
+
169
+ public string RequestId { get ; set ; }
102
170
}
103
171
}
0 commit comments