Skip to content

Managed dependency download on background thread #193

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 11 commits into from
Apr 24, 2019
Merged
54 changes: 52 additions & 2 deletions src/DependencyManagement/DependencyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

namespace Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement
{
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Threading;

internal class DependencyManager
{
Expand All @@ -28,6 +31,8 @@ internal class DependencyManager
// This is the location where the dependent modules will be installed.
internal static string DependenciesPath { get; private set; }

internal Exception DependencyError { get => _dependencyError; }

// Az module name.
private const string AzModuleName = "Az";

Expand All @@ -51,6 +56,8 @@ internal class DependencyManager
// Managed Dependencies folder name.
private const string ManagedDependenciesFolderName = "ManagedDependencies";

private volatile Exception _dependencyError;

// This flag is used to figure out if we need to install/reinstall all the function app dependencies.
// If we do, we use it to clean up the module destination path.
private bool _shouldUpdateFunctionAppDependencies;
Expand All @@ -60,6 +67,49 @@ internal DependencyManager()
Dependencies = new List<DependencyInfo>();
}

internal void ProcessDependencies(
MessagingStream msgStream,
StreamingMessage request)
{
try
{
_dependencyError = null;
var functionLoadRequest = request.FunctionLoadRequest;
if (functionLoadRequest.ManagedDependencyEnabled)
{
Initialize(functionLoadRequest);
}

if (_shouldUpdateFunctionAppDependencies)
{
var initialSessionState = InitialSessionState.CreateDefault();
initialSessionState.ThreadOptions = PSThreadOptions.UseCurrentThread;
// Setting the execution policy on macOS and Linux throws an exception so only update it on Windows
if (Platform.IsWindows)
{
initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
}

using (PowerShell PowerShellInstance = PowerShell.Create(initialSessionState))
{
RequestProcessor.IsDependencyDownloadInProgress = true;
var rpcLogger = new RpcLogger(msgStream);
rpcLogger.SetContext(request.RequestId, null);
InstallFunctionAppDependencies(PowerShellInstance, rpcLogger);
RequestProcessor.IsDependencyDownloadInProgress = false;
}
}
}
catch (Exception e)
{
_dependencyError = e;
}
finally
{
RequestProcessor.IsDependencyDownloadInProgress = false;
}
}

/// <summary>
/// Initializes the dependency manger and performs the following:
/// - Parse functionAppRoot\requirements.psd1 file and create a list of dependencies to install.
Expand All @@ -83,8 +133,8 @@ internal void Initialize(FunctionLoadRequest request)
foreach (DictionaryEntry entry in entries)
{
// A valid entry is of the form: 'ModuleName'='MajorVersion.*"
string name = (string) entry.Key;
string version = (string) entry.Value;
string name = (string)entry.Key;
string version = (string)entry.Value;

// Validates that the module name is a supported dependency.
ValidateModuleName(name);
Expand Down
10 changes: 6 additions & 4 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ internal class PowerShellManager
/// </summary>
internal ILogger Logger => _logger;

/// <summary>
/// Gets the associated PowerShell instance
/// </summary>
internal PowerShell PowerShellInstance => _pwsh;

static PowerShellManager()
{
// Set the type accelerators for 'HttpResponseContext' and 'HttpResponseContext'.
Expand All @@ -45,7 +50,7 @@ static PowerShellManager()
addMethod.Invoke(null, new object[] { "HttpRequestContext", typeof(HttpRequestContext) });
}

internal PowerShellManager(ILogger logger, Action<PowerShell, ILogger> initAction = null)
internal PowerShellManager(ILogger logger)
{
if (FunctionLoader.FunctionAppRootPath == null)
{
Expand Down Expand Up @@ -77,9 +82,6 @@ internal PowerShellManager(ILogger logger, Action<PowerShell, ILogger> initActio
_pwsh.Streams.Verbose.DataAdding += streamHandler.VerboseDataAdding;
_pwsh.Streams.Warning.DataAdding += streamHandler.WarningDataAdding;

// Install function app dependent modules
initAction?.Invoke(_pwsh, logger);

// Initialize the Runspace
InvokeProfile(FunctionLoader.FunctionAppProfilePath);
}
Expand Down
6 changes: 3 additions & 3 deletions src/PowerShell/PowerShellManagerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ internal PowerShellManagerPool(MessagingStream msgStream)
/// 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(string requestId, Action<PowerShell, ILogger> initAction = null)
internal void Initialize(string requestId)
{
var logger = new RpcLogger(_msgStream);

try
{
logger.SetContext(requestId, invocationId: null);
_pool.Add(new PowerShellManager(logger, initAction));
_pool.Add(new PowerShellManager(logger));
_poolSize = 1;
}
finally
Expand Down Expand Up @@ -98,7 +98,7 @@ internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFuncti
}
}

// Register the function with the Runspace before returning the idle PowerShellManager.
// Register the function with the Runspace PowerShellManager.
FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, functionInfo);
psManager.Logger.SetContext(requestId, invocationId);
return psManager;
Expand Down
60 changes: 35 additions & 25 deletions src/RequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;

namespace Microsoft.Azure.Functions.PowerShellWorker
namespace Microsoft.Azure.Functions.PowerShellWorker
{
internal class RequestProcessor
{
Expand All @@ -35,6 +35,9 @@ internal class RequestProcessor
private Dictionary<StreamingMessage.ContentOneofCase, Func<StreamingMessage, StreamingMessage>> _requestHandlers =
new Dictionary<StreamingMessage.ContentOneofCase, Func<StreamingMessage, StreamingMessage>>();

internal static volatile bool IsDependencyDownloadInProgress;
private volatile Task _dependencyDownloadTask;

internal RequestProcessor(MessagingStream msgStream)
{
_msgStream = msgStream;
Expand All @@ -57,7 +60,7 @@ internal RequestProcessor(MessagingStream msgStream)

// Host sends required metadata to worker to load function
_requestHandlers.Add(StreamingMessage.ContentOneofCase.FunctionLoadRequest, ProcessFunctionLoadRequest);

// Host requests a given invocation
_requestHandlers.Add(StreamingMessage.ContentOneofCase.InvocationRequest, ProcessInvocationRequest);

Expand Down Expand Up @@ -183,7 +186,6 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)

try
{
// Load the metadata of the function.
_functionLoader.LoadFunction(functionLoadRequest);
}
catch (Exception e)
Expand All @@ -209,6 +211,28 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
functionInfo = _functionLoader.GetFunctionInfo(request.InvocationRequest.FunctionId);
psManager = _powershellPool.CheckoutIdleWorker(request, functionInfo);

if (_dependencyDownloadTask != null
&& ((_dependencyDownloadTask.Status != TaskStatus.Canceled)
|| _dependencyDownloadTask.Status != TaskStatus.Faulted
|| _dependencyDownloadTask.Status != TaskStatus.RanToCompletion))
{
psManager.Logger.Log(LogLevel.Information, PowerShellWorkerStrings.DependencyDownloadInProgress, null, true);
psManager.Logger.Log(LogLevel.Information, PowerShellWorkerStrings.DependencyDownloadInProgress, null);
_dependencyDownloadTask.Wait();
}

if (_dependencyManager?.DependencyError != null)
{
StreamingMessage response = NewStreamingMessageTemplate(request.RequestId,
StreamingMessage.ContentOneofCase.InvocationResponse,
out StatusResult status);
status.Status = StatusResult.Types.Status.Failure;
status.Exception = _dependencyManager.DependencyError.ToRpcException();
response.InvocationResponse.InvocationId = request.InvocationRequest.InvocationId;
return response;
}

//ProcessInvocationRequestImpl(request, functionInfo, psManager);
if (_powershellPool.UpperBound == 1)
{
// When the concurrency upper bound is 1, we can handle only one invocation at a time anyways,
Expand All @@ -225,7 +249,6 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
catch (Exception e)
{
_powershellPool.ReclaimUsedWorker(psManager);

StreamingMessage response = NewStreamingMessageTemplate(
request.RequestId,
StreamingMessage.ContentOneofCase.InvocationResponse,
Expand All @@ -234,7 +257,6 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
response.InvocationResponse.InvocationId = request.InvocationRequest.InvocationId;
status.Status = StatusResult.Types.Status.Failure;
status.Exception = e.ToRpcException();

return response;
}

Expand Down Expand Up @@ -299,27 +321,15 @@ internal StreamingMessage ProcessFunctionEnvironmentReloadRequest(StreamingMessa
private void InitializeForFunctionApp(StreamingMessage request, StreamingMessage response)
{
var functionLoadRequest = request.FunctionLoadRequest;

// If 'ManagedDependencyEnabled' is true, process the function app dependencies as defined in FunctionAppRoot\requirements.psd1.
// These dependencies will be installed via 'Save-Module' when the first PowerShellManager instance is being created.
if (functionLoadRequest.ManagedDependencyEnabled)
{
_dependencyManager.Initialize(functionLoadRequest);
}

// Setup the FunctionApp root path and module path.
FunctionLoader.SetupWellKnownPaths(functionLoadRequest);

// Constructing the first PowerShellManager instance for the Pool.
if (DependencyManager.Dependencies.Count > 0)
{
// Do extra work to install the specified dependencies.
_powershellPool.Initialize(request.RequestId, _dependencyManager.InstallFunctionAppDependencies);
response.FunctionLoadResponse.IsDependencyDownloaded = true;
}
else
FunctionLoader.SetupWellKnownPaths(request.FunctionLoadRequest);
if (functionLoadRequest.ManagedDependencyEnabled)
{
_powershellPool.Initialize(request.RequestId);
//Start dependency download on a separate thread
_dependencyDownloadTask = Task.Run(() => _dependencyManager.ProcessDependencies(_msgStream, request)).ContinueWith((task) =>
{
_dependencyDownloadTask = null;
});
}
}

Expand Down Expand Up @@ -425,7 +435,7 @@ private void BindOutputFromResult(InvocationResponse response, AzFunctionInfo fu
TypedData dataToUse = transformedValue.ToTypedData();

// if one of the bindings is '$return' we need to set the ReturnValue
if(string.Equals(outBindingName, AzFunctionInfo.DollarReturn, StringComparison.OrdinalIgnoreCase))
if (string.Equals(outBindingName, AzFunctionInfo.DollarReturn, StringComparison.OrdinalIgnoreCase))
{
response.ReturnValue = dataToUse;
continue;
Expand Down
67 changes: 35 additions & 32 deletions src/resources/PowerShellWorkerStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,41 +26,41 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="https://www.w3.org/XML/1998/namespace" />
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
Expand Down Expand Up @@ -166,7 +166,7 @@
<data name="SpecifiedCustomPipeName" xml:space="preserve">
<value>Custom pipe name specified. You can attach to the process by using vscode or by running `Enter-PSHostProcess -CustomPipeName {0}`</value>
</data>
<data name="CannotFindModuleVersion" xml:space="preserve">
<data name="CannotFindModuleVersion" xml:space="preserve">
<value>Cannot find a supported version for module '{0}' with major version '{0}'.</value>
</data>
<data name="FunctionAppDoesNotHaveDependentModulesToInstall" xml:space="preserve">
Expand Down Expand Up @@ -194,7 +194,7 @@
<value>Invalid major version for module '{0}'. The maximum available major version is '{1}'.</value>
</data>
<data name="FailToInstallFuncAppDependencies" xml:space="preserve">
<value>Fail to install function app dependencies. Error: '{0}'</value>
<value>Fail to install function app dependencies. Restarting the app may resolve the error. Error: '{0}'</value>
</data>
<data name="FailToResolveHomeDirectory" xml:space="preserve">
<value>Fail to resolve '{0}' path in App Service.</value>
Expand All @@ -211,4 +211,7 @@
<data name="LogNewPowerShellManagerCreated" xml:space="preserve">
<value>A new PowerShell manager instance is added to the pool. Current pool size '{0}'.</value>
</data>
</root>
<data name="DependencyDownloadInProgress" xml:space="preserve">
<value>Managed dependency download is in progress, function execution will continue when it's done.</value>
</data>
</root>