5
5
using System . Collections . Generic ;
6
6
using System . Collections . ObjectModel ;
7
7
using System . Diagnostics ;
8
+ using System . Linq ;
8
9
using System . Net . Http ;
9
10
using System . Threading ;
10
11
using System . Threading . Tasks ;
21
22
using Microsoft . Azure . WebJobs . Script . WebHost . Management ;
22
23
using Microsoft . Azure . WebJobs . Script . WebHost . Models ;
23
24
using Microsoft . Azure . WebJobs . Script . WebHost . Security . Authorization . Policies ;
25
+ using Microsoft . Azure . WebJobs . Script . Workers . Rpc ;
24
26
using Microsoft . Extensions . DependencyInjection ;
25
27
using Microsoft . Extensions . Logging ;
26
28
using Microsoft . Extensions . Options ;
@@ -32,7 +34,7 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Controllers
32
34
{
33
35
/// <summary>
34
36
/// Controller responsible for handling all administrative requests for host operations
35
- /// example host status, ping, log, etc
37
+ /// example host status, ping, log, etc.
36
38
/// </summary>
37
39
public class HostController : Controller
38
40
{
@@ -107,6 +109,77 @@ public async Task<IActionResult> GetHostStatus([FromServices] IScriptHostManager
107
109
return Ok ( status ) ;
108
110
}
109
111
112
+ /// <summary>
113
+ /// Currently, anyone in Reader role can access this information.
114
+ /// If this API is extended to include any secrets, it will need to be
115
+ /// locked down to only Contributor roles.
116
+ /// </summary>
117
+ [ HttpGet ]
118
+ [ Route ( "admin/host/processes" ) ]
119
+ [ Authorize ( Policy = PolicyNames . AdminAuthLevelOrInternal ) ]
120
+ public async Task < IActionResult > GetWorkerProcesses ( [ FromServices ] IScriptHostManager scriptHostManager )
121
+ {
122
+ if ( ! Utility . TryGetHostService ( scriptHostManager , out IWebHostRpcWorkerChannelManager webHostLanguageWorkerChannelManager ) )
123
+ {
124
+ return StatusCode ( StatusCodes . Status503ServiceUnavailable ) ;
125
+ }
126
+
127
+ var hostProcess = Process . GetCurrentProcess ( ) ;
128
+ List < FunctionProcesses . FunctionProcessInfo > processes = new ( )
129
+ {
130
+ new FunctionProcesses . FunctionProcessInfo ( )
131
+ {
132
+ ProcessId = hostProcess . Id ,
133
+ DebugEngine = RpcWorkerConstants . DotNetCoreDebugEngine ,
134
+ IsEligibleForOpenInBrowser = false ,
135
+ ProcessName = hostProcess . ProcessName
136
+ }
137
+ } ;
138
+
139
+ string workerRuntime = _environment . GetFunctionsWorkerRuntime ( ) ;
140
+
141
+ List < IRpcWorkerChannel > channels = null ;
142
+ if ( Utility . TryGetHostService ( scriptHostManager , out IJobHostRpcWorkerChannelManager jobHostLanguageWorkerChannelManager ) )
143
+ {
144
+ channels = jobHostLanguageWorkerChannelManager . GetChannels ( workerRuntime ) . ToList ( ) ;
145
+ }
146
+
147
+ var webhostChannelDictionary = webHostLanguageWorkerChannelManager . GetChannels ( workerRuntime ) ;
148
+
149
+ List < Task < IRpcWorkerChannel > > webHostchannelTasks = new List < Task < IRpcWorkerChannel > > ( ) ;
150
+ if ( webhostChannelDictionary is not null )
151
+ {
152
+ foreach ( var pair in webhostChannelDictionary )
153
+ {
154
+ var workerChannel = pair . Value . Task ;
155
+ webHostchannelTasks . Add ( workerChannel ) ;
156
+ }
157
+ }
158
+
159
+ var webHostchannels = await Task . WhenAll ( webHostchannelTasks ) ;
160
+ channels = channels ?? new List < IRpcWorkerChannel > ( ) ;
161
+ channels . AddRange ( webHostchannels ) ;
162
+
163
+ foreach ( var channel in channels )
164
+ {
165
+ var processInfo = new FunctionProcesses . FunctionProcessInfo ( )
166
+ {
167
+ ProcessId = channel . WorkerProcess . Process . Id ,
168
+ ProcessName = channel . WorkerProcess . Process . ProcessName ,
169
+ DebugEngine = Utility . GetDebugEngineInfo ( channel . WorkerConfig , workerRuntime ) ,
170
+ IsEligibleForOpenInBrowser = false
171
+ } ;
172
+ processes . Add ( processInfo ) ;
173
+ }
174
+
175
+ var functionProcesses = new FunctionProcesses ( )
176
+ {
177
+ Processes = processes
178
+ } ;
179
+
180
+ return Ok ( functionProcesses ) ;
181
+ }
182
+
110
183
[ HttpPost ]
111
184
[ Route ( "admin/host/drain" ) ]
112
185
[ Authorize ( Policy = PolicyNames . AdminAuthLevelOrInternal ) ]
@@ -262,7 +335,7 @@ public async Task<IActionResult> GetScaleStatus([FromBody] ScaleStatusContext co
262
335
[ HttpPost ]
263
336
[ Route ( "admin/host/log" ) ]
264
337
[ Authorize ( Policy = PolicyNames . AdminAuthLevelOrInternal ) ]
265
- public IActionResult Log ( [ FromBody ] IEnumerable < HostLogEntry > logEntries )
338
+ public IActionResult Log ( [ FromBody ] IEnumerable < HostLogEntry > logEntries )
266
339
{
267
340
if ( logEntries == null )
268
341
{
0 commit comments