Skip to content
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

Bug fixes and code refactoring to get it ready for durable function support #72

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 44 additions & 9 deletions src/FunctionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
{
internal class FunctionLoader
{
private readonly MapField<string, FunctionInfo> _loadedFunctions = new MapField<string, FunctionInfo>();
private readonly MapField<string, AzFunctionInfo> _loadedFunctions = new MapField<string, AzFunctionInfo>();

internal FunctionInfo GetFunctionInfo(string functionId)
internal AzFunctionInfo GetFunctionInfo(string functionId)
{
if (_loadedFunctions.TryGetValue(functionId, out FunctionInfo funcInfo))
if (_loadedFunctions.TryGetValue(functionId, out AzFunctionInfo funcInfo))
{
return funcInfo;
}
Expand All @@ -27,20 +27,35 @@ internal void Load(FunctionLoadRequest request)
{
// TODO: catch "load" issues at "func start" time.
// ex. Script doesn't exist, entry point doesn't exist
_loadedFunctions.Add(request.FunctionId, new FunctionInfo(request.Metadata));
_loadedFunctions.Add(request.FunctionId, new AzFunctionInfo(request.Metadata));
}
}

internal class FunctionInfo
internal enum AzFunctionType
{
None = 0,
RegularFunction = 1,
OrchestrationFunction = 2,
ActivityFunction = 3
}

internal class AzFunctionInfo
{
private const string OrchestrationTrigger = "orchestrationTrigger";
private const string ActivityTrigger = "activityTrigger";

internal const string TriggerMetadata = "TriggerMetadata";
internal const string DollarReturn = "$return";

internal readonly string Directory;
internal readonly string EntryPoint;
internal readonly string FunctionName;
internal readonly string ScriptPath;
internal readonly AzFunctionType Type;
internal readonly MapField<string, BindingInfo> AllBindings;
internal readonly MapField<string, BindingInfo> OutputBindings;

public FunctionInfo(RpcFunctionMetadata metadata)
internal AzFunctionInfo(RpcFunctionMetadata metadata)
{
FunctionName = metadata.Name;
Directory = metadata.Directory;
Expand All @@ -52,12 +67,32 @@ public FunctionInfo(RpcFunctionMetadata metadata)

foreach (var binding in metadata.Bindings)
{
AllBindings.Add(binding.Key, binding.Value);
string bindingName = binding.Key;
BindingInfo bindingInfo = binding.Value;

AllBindings.Add(bindingName, bindingInfo);

// PowerShell doesn't support the 'InOut' type binding
if (binding.Value.Direction == BindingInfo.Types.Direction.Out)
if (bindingInfo.Direction == BindingInfo.Types.Direction.In)
{
switch (bindingInfo.Type)
{
case OrchestrationTrigger:
Type = AzFunctionType.OrchestrationFunction;
break;
case ActivityTrigger:
Type = AzFunctionType.ActivityFunction;
break;
default:
Type = AzFunctionType.RegularFunction;
break;
}
continue;
}

if (bindingInfo.Direction == BindingInfo.Types.Direction.Out)
{
OutputBindings.Add(binding.Key, binding.Value);
OutputBindings.Add(bindingName, bindingInfo);
}
}
}
Expand Down
24 changes: 19 additions & 5 deletions src/PowerShell/PowerShellExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ internal static class PowerShellExtensions
{
public static void InvokeAndClearCommands(this PowerShell pwsh)
{
pwsh.Invoke();
pwsh.Commands.Clear();
try
{
pwsh.Invoke();
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}

public static Collection<T> InvokeAndClearCommands<T>(this PowerShell pwsh)
{
var result = pwsh.Invoke<T>();
pwsh.Commands.Clear();
return result;
try
{
var result = pwsh.Invoke<T>();
return result;
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}
}
}
144 changes: 83 additions & 61 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell

internal class PowerShellManager
{
private const string _TriggerMetadataParameterName = "TriggerMetadata";

private ILogger _logger;
private PowerShell _pwsh;
private readonly ILogger _logger;
private readonly PowerShell _pwsh;

internal PowerShellManager(ILogger logger)
{
Expand Down Expand Up @@ -88,104 +86,128 @@ internal void InitializeRunspace()
// Add HttpResponseContext namespace so users can reference
// HttpResponseContext without needing to specify the full namespace
_pwsh.AddScript($"using namespace {typeof(HttpResponseContext).Namespace}").InvokeAndClearCommands();

// Set the PSModulePath
Environment.SetEnvironmentVariable("PSModulePath", Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules"));

AuthenticateToAzure();
}

/// <summary>
/// Execution a function fired by a trigger or an activity function scheduled by an orchestration.
/// </summary>
internal Hashtable InvokeFunction(
string scriptPath,
string entryPoint,
AzFunctionInfo functionInfo,
Hashtable triggerMetadata,
IList<ParameterBinding> inputData)
{
string scriptPath = functionInfo.ScriptPath;
string entryPoint = functionInfo.EntryPoint;
string moduleName = null;

try
{
Dictionary<string, ParameterMetadata> parameterMetadata;
// If an entry point is defined, we load the script as a module and invoke the function with that name.
// We also need to fetch the ParameterMetadata to know what to pass in as arguments.
var parameterMetadata = RetriveParameterMetadata(functionInfo, out moduleName);
_pwsh.AddCommand(String.IsNullOrEmpty(entryPoint) ? scriptPath : entryPoint);

// We need to take into account if the user has an entry point.
// If it does, we invoke the command of that name. We also need to fetch
// the ParameterMetadata so that we can tell whether or not the user is asking
// for the $TriggerMetadata
using (ExecutionTimer.Start(_logger, "Parameter metadata retrieved."))
// Set arguments for each input binding parameter
foreach (ParameterBinding binding in inputData)
{
if (entryPoint != "")
{
parameterMetadata = _pwsh
.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameter("Name", scriptPath)
.AddStatement()
.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddParameter("Name", entryPoint)
.InvokeAndClearCommands<FunctionInfo>()[0].Parameters;

_pwsh.AddCommand(entryPoint);

}
else
if (parameterMetadata.ContainsKey(binding.Name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now... but we should do the validation to make sure that the param block has all the input bindings.

tracked in #15

{
parameterMetadata = _pwsh.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddParameter("Name", scriptPath)
.InvokeAndClearCommands<ExternalScriptInfo>()[0].Parameters;

_pwsh.AddCommand(scriptPath);
_pwsh.AddParameter(binding.Name, binding.Data.ToObject());
}
}

// Sets the variables for each input binding
foreach (ParameterBinding binding in inputData)
// Gives access to additional Trigger Metadata if the user specifies TriggerMetadata
if(parameterMetadata.ContainsKey(AzFunctionInfo.TriggerMetadata))
{
_pwsh.AddParameter(binding.Name, binding.Data.ToObject());
_logger.Log(LogLevel.Debug, "Parameter '-TriggerMetadata' found.");
_pwsh.AddParameter(AzFunctionInfo.TriggerMetadata, triggerMetadata);
}

// Gives access to additional Trigger Metadata if the user specifies TriggerMetadata
if(parameterMetadata.ContainsKey(_TriggerMetadataParameterName))
Collection<object> pipelineItems = null;
using (ExecutionTimer.Start(_logger, "Execution of the user's function completed."))
{
_pwsh.AddParameter(_TriggerMetadataParameterName, triggerMetadata);
_logger.Log(LogLevel.Debug, $"TriggerMetadata found. Value:{Environment.NewLine}{triggerMetadata.ToString()}");
pipelineItems = _pwsh.InvokeAndClearCommands<object>();
}

PSObject returnObject = null;
using (ExecutionTimer.Start(_logger, "Execution of the user's function completed."))
var result = _pwsh.AddCommand("Microsoft.Azure.Functions.PowerShellWorker\\Get-OutputBinding")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change the var to the actual returned type for clarity

.AddParameter("Purge")
.InvokeAndClearCommands<Hashtable>()[0];

if (pipelineItems != null && pipelineItems.Count > 0)
{
// Log everything we received from the pipeline and set the last one to be the ReturnObject
Collection<PSObject> pipelineItems = _pwsh.InvokeAndClearCommands<PSObject>();
if (pipelineItems.Count > 0)
foreach (var item in pipelineItems)
{
foreach (var psobject in pipelineItems)
{
_logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}");
}
returnObject = pipelineItems[pipelineItems.Count - 1];
_logger.Log(LogLevel.Information, $"OUTPUT: {item.ToString()}");
}
result.Add(AzFunctionInfo.DollarReturn, pipelineItems[pipelineItems.Count - 1]);
}

var result = _pwsh.AddCommand("Microsoft.Azure.Functions.PowerShellWorker\\Get-OutputBinding")
.AddParameter("Purge")
.InvokeAndClearCommands<Hashtable>()[0];

if(returnObject != null)
{
result.Add("$return", returnObject);
}
return result;
}
finally
{
ResetRunspace(scriptPath);
ResetRunspace(moduleName);
}
}

/// <summary>
/// Helper method to convert the result returned from a function to JSON.
/// </summary>
internal string ConvertToJson(object fromObj)
{
return _pwsh.AddCommand("Microsoft.PowerShell.Utility\\ConvertTo-Json")
.AddParameter("InputObject", fromObj)
.AddParameter("Depth", 10)
.AddParameter("Compress", true)
.InvokeAndClearCommands<string>()[0];
}

private Dictionary<string, ParameterMetadata> RetriveParameterMetadata(
AzFunctionInfo functionInfo,
out string moduleName)
{
moduleName = null;
string scriptPath = functionInfo.ScriptPath;
string entryPoint = functionInfo.EntryPoint;

using (ExecutionTimer.Start(_logger, "Parameter metadata retrieved."))
{
if (String.IsNullOrEmpty(entryPoint))
{
return _pwsh.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddParameter("Name", scriptPath)
.InvokeAndClearCommands<ExternalScriptInfo>()[0].Parameters;
}
else
{
moduleName = Path.GetFileNameWithoutExtension(scriptPath);
return _pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameter("Name", scriptPath)
.AddStatement()
.AddCommand("Microsoft.PowerShell.Core\\Get-Command").AddParameter("Name", entryPoint)
.InvokeAndClearCommands<FunctionInfo>()[0].Parameters;
}
}
}

private void ResetRunspace(string scriptPath)
private void ResetRunspace(string moduleName)
{
// Reset the runspace to the Initial Session State
_pwsh.Runspace.ResetRunspaceState();

// If the function had an entry point, this will remove the module that was loaded
var moduleName = Path.GetFileNameWithoutExtension(scriptPath);
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
.AddParameter("Name", moduleName)
.AddParameter("ErrorAction", "SilentlyContinue")
.InvokeAndClearCommands();
if (!String.IsNullOrEmpty(moduleName))
{
// If the function had an entry point, this will remove the module that was loaded
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
.AddParameter("Name", moduleName)
.AddParameter("Force", true)
.AddParameter("ErrorAction", "SilentlyContinue")
.InvokeAndClearCommands();
}
}
}
}
Loading