-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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)) | ||
{ | ||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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