@@ -19,23 +19,44 @@ internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
1919 {
2020 List < RpcFunctionMetadata > indexedFunctions = new List < RpcFunctionMetadata > ( ) ;
2121
22+ // This is not the correct way to deal with getting a runspace for the cmdlet.
23+
24+ // Firstly, creating a runspace is expensive. If we are going to generate a runspace, it should be done on
25+ // the function load request so that it can be created while the host is processing.
26+
27+ // Secondly, this assumes that the AzureFunctions.PowerShell.SDK module is present on the machine/VM's
28+ // PSModulePath. On an Azure instance, it will not be. What we need to do here is move the call
29+ // to SetupAppRootPathAndModulePath in RequestProcessor to the init request, and then use the
30+ // _firstPwshInstance to invoke the Get-FunctionsMetadata command. The only issue with this is that
31+ // SetupAppRootPathAndModulePath needs the initial function init request in order to know if managed
32+ // dependencies are enabled in this function app.
33+
34+ // Proposed solutions:
35+ // 1. Pass ManagedDependencyEnabled flag in the worker init request
36+ // 2. Change the flow, so that _firstPwshInstance is initialized in worker init with the PSModulePath
37+ // assuming that managed dependencies are enabled, and then revert the PSModulePath in the first function
38+ // init request should the managed dependencies not be enabled.
39+ // 3. Continue using a new runspace for invoking Get-FunctionsMetadata, but initialize it in worker init and
40+ // point the PsModulePath to the module path bundled with the worker.
41+
42+
2243 InitialSessionState initial = InitialSessionState . CreateDefault ( ) ;
2344 Runspace runspace = RunspaceFactory . CreateRunspace ( initial ) ;
2445 runspace . Open ( ) ;
25- System . Management . Automation . PowerShell ps = System . Management . Automation . PowerShell . Create ( ) ;
26- ps . Runspace = runspace ;
46+ System . Management . Automation . PowerShell _powershell = System . Management . Automation . PowerShell . Create ( ) ;
47+ _powershell . Runspace = runspace ;
2748
28- ps . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
49+ _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
2950 string outputString = string . Empty ;
30- foreach ( PSObject rawMetadata in ps . Invoke ( ) )
51+ foreach ( PSObject rawMetadata in _powershell . Invoke ( ) )
3152 {
3253 if ( outputString != string . Empty )
3354 {
3455 throw new Exception ( PowerShellWorkerStrings . GetFunctionsMetadataMultipleResultsError ) ;
3556 }
3657 outputString = rawMetadata . ToString ( ) ;
3758 }
38- ps . Commands . Clear ( ) ;
59+ _powershell . Commands . Clear ( ) ;
3960
4061 List < FunctionInformation > functionInformations = JsonConvert . DeserializeObject < List < FunctionInformation > > ( outputString ) ;
4162
0 commit comments