Skip to content

Create the first PowerShellManager instance when processing the first FunctionLoad request #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal class PowerShellManager
{
private readonly ILogger _logger;
private readonly PowerShell _pwsh;
private bool _runspaceInited;

/// <summary>
/// Gets the Runspace InstanceId.
Expand All @@ -45,7 +46,7 @@ static PowerShellManager()
addMethod.Invoke(null, new object[] { "HttpRequestContext", typeof(HttpRequestContext) });
}

internal PowerShellManager(ILogger logger)
internal PowerShellManager(ILogger logger, bool delayInit = false)
{
if (FunctionLoader.FunctionAppRootPath == null)
{
Expand All @@ -65,7 +66,22 @@ internal PowerShellManager(ILogger logger)
_pwsh.Streams.Warning.DataAdding += streamHandler.WarningDataAdding;

// Initialize the Runspace
InvokeProfile(FunctionLoader.FunctionAppProfilePath);
if (!delayInit)
{
Initialize();
}
}

/// <summary>
/// Extra initialization of the Runspace.
/// </summary>
internal void Initialize()
{
if (!_runspaceInited)
{
InvokeProfile(FunctionLoader.FunctionAppProfilePath);
_runspaceInited = true;
}
}

/// <summary>
Expand All @@ -76,7 +92,8 @@ internal void InvokeProfile(string profilePath)
Exception exception = null;
if (profilePath == null)
{
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.FileNotFound, "profile.ps1", FunctionLoader.FunctionAppRootPath));
string noProfileMsg = string.Format(PowerShellWorkerStrings.FileNotFound, "profile.ps1", FunctionLoader.FunctionAppRootPath);
_logger.Log(LogLevel.Trace, noProfileMsg);
return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/PowerShell/PowerShellManagerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ internal PowerShellManagerPool(MessagingStream msgStream)
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.LogConcurrencyUpperBound, _upperBound.ToString()));
}

/// <summary>
/// Initialize the pool and populate it with PowerShellManager instances.
/// We instantiate PowerShellManager instances in a lazy way, starting from size 1 and increase the number of workers as needed.
/// </summary>
internal void Initialize()
{
_pool.Add(new PowerShellManager(new RpcLogger(_msgStream), delayInit: true));
_poolSize = 1;
}

/// <summary>
/// Checkout an idle PowerShellManager instance in a non-blocking asynchronous way.
/// </summary>
Expand Down Expand Up @@ -78,9 +88,14 @@ internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFuncti
}
}

// Finish the initialization if not yet.
// This applies only to the very first PowerShellManager instance, whose initialization was deferred.
psManager.Initialize();

// Register the function with the Runspace before returning the idle PowerShellManager.
FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, functionInfo);
psManager.Logger.SetContext(requestId, invocationId);

return psManager;
}

Expand Down
5 changes: 5 additions & 0 deletions src/RequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
// Setup the FunctionApp root path and module path.
FunctionLoader.SetupWellKnownPaths(functionLoadRequest);
_dependencyManager.ProcessDependencyDownload(_msgStream, request);

// Create the first Runspace so that the debugger has the target to attach to.
// The further initialization of the Runspace (e.g. invoking profile.ps1) is delayed until
// the first invocation and completion of the dependency download.
_powershellPool.Initialize();
}
catch (Exception e)
{
Expand Down
25 changes: 23 additions & 2 deletions test/Unit/PowerShell/PowerShellManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ static TestUtils()

// Have a single place to get a PowerShellManager for testing.
// This is to guarantee that the well known paths are setup before calling the constructor of PowerShellManager.
internal static PowerShellManager NewTestPowerShellManager(ConsoleLogger logger)
internal static PowerShellManager NewTestPowerShellManager(ConsoleLogger logger, bool delayInit = false)
{
return new PowerShellManager(logger);
return new PowerShellManager(logger, delayInit);
}

internal static AzFunctionInfo NewAzFunctionInfo(string scriptFile, string entryPoint)
Expand Down Expand Up @@ -235,5 +235,26 @@ public void ProfileWithNonTerminatingError()
Assert.Equal("Error: ERROR: help me!", _testLogger.FullLog[0]);
Assert.Matches("Error: Fail to run profile.ps1. See logs for detailed errors. Profile location: ", _testLogger.FullLog[1]);
}

[Fact]
public void PSManagerCtorRunsProfileByDefault()
{
//initialize fresh log
_testLogger.FullLog.Clear();
TestUtils.NewTestPowerShellManager(_testLogger);

Assert.Single(_testLogger.FullLog);
Assert.Equal($"Trace: No 'profile.ps1' is found at the FunctionApp root folder: {FunctionLoader.FunctionAppRootPath}.", _testLogger.FullLog[0]);
}

[Fact]
public void PSManagerCtorDoesNotRunProfileIfDelayInit()
{
//initialize fresh log
_testLogger.FullLog.Clear();
TestUtils.NewTestPowerShellManager(_testLogger, delayInit: true);

Assert.Empty(_testLogger.FullLog);
}
}
}