Skip to content

Allow worker to read results directly from the external SDK #777

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 5 commits into from
Mar 17, 2022
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
9 changes: 4 additions & 5 deletions src/DurableSDK/ExternalInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
{
using System;
using System.Collections;
using System.Management.Automation;

internal class ExternalInvoker : IExternalInvoker
{
private readonly Func<PowerShell, object> _externalSDKInvokerFunction;
private readonly IPowerShellServices _powerShellServices;

public ExternalInvoker(Func<PowerShell, object> invokerFunction, IPowerShellServices powerShellServices)
public ExternalInvoker(Func<PowerShell, object> invokerFunction)
{
_externalSDKInvokerFunction = invokerFunction;
_powerShellServices = powerShellServices;
}

public void Invoke()
public Hashtable Invoke(IPowerShellServices powerShellServices)
{
_externalSDKInvokerFunction.Invoke(_powerShellServices.GetPowerShell());
return (Hashtable)_externalSDKInvokerFunction.Invoke(powerShellServices.GetPowerShell());
}
}
}
4 changes: 3 additions & 1 deletion src/DurableSDK/IExternalInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
{
using System.Collections;

// Represents a contract for the
internal interface IExternalInvoker
{
// Method to invoke an orchestration using the external Durable SDK
void Invoke();
Hashtable Invoke(IPowerShellServices powerShellServices);
}
}
12 changes: 0 additions & 12 deletions src/DurableSDK/OrchestrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ public class OrchestrationContext

internal OrchestrationActionCollector OrchestrationActionCollector { get; } = new OrchestrationActionCollector();

internal object ExternalSDKResult;

internal bool ExternalSDKIsError;

// Called by the External DF SDK to communicate its orchestration result
// back to the worker.
internal void SetExternalResult(object result, bool isError)
{
this.ExternalSDKResult = result;
this.ExternalSDKIsError = isError;
}

internal object CustomStatus { get; set; }
}
}
25 changes: 9 additions & 16 deletions src/DurableSDK/OrchestrationInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
internal class OrchestrationInvoker : IOrchestrationInvoker
{
private IExternalInvoker _externalInvoker;
internal static string isOrchestrationFailureKey = "IsOrchestrationFailure";

public Hashtable Invoke(
OrchestrationBindingInfo orchestrationBindingInfo,
Expand All @@ -25,32 +26,24 @@ public Hashtable Invoke(
{
if (powerShellServices.UseExternalDurableSDK())
{
return InvokeExternalDurableSDK(orchestrationBindingInfo, powerShellServices);
return InvokeExternalDurableSDK(powerShellServices);
}
return InvokeInternalDurableSDK(orchestrationBindingInfo, powerShellServices);
}
catch (Exception ex)
{
ex.Data.Add(isOrchestrationFailureKey, true);
throw;
}
finally
{
powerShellServices.ClearStreamsAndCommands();
}
}

public Hashtable InvokeExternalDurableSDK(
OrchestrationBindingInfo orchestrationBindingInfo,
IPowerShellServices powerShellServices)
public Hashtable InvokeExternalDurableSDK(IPowerShellServices powerShellServices)
{

_externalInvoker.Invoke();
var result = orchestrationBindingInfo.Context.ExternalSDKResult;
var isError = orchestrationBindingInfo.Context.ExternalSDKIsError;
if (isError)
{
throw (Exception)result;
}
else
{
return (Hashtable)result;
}
return _externalInvoker.Invoke(powerShellServices);
}

public Hashtable InvokeInternalDurableSDK(
Expand Down
3 changes: 1 addition & 2 deletions src/DurableSDK/PowerShellServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ public OrchestrationBindingInfo SetOrchestrationContext(
// The external SetFunctionInvocationContextCommand expects a .json string to deserialize
// and writes an invoker function to the output pipeline.
.AddParameter("OrchestrationContext", context.Data.String)
.AddParameter("SetResult", (Action<object, bool>) orchestrationBindingInfo.Context.SetExternalResult)
.InvokeAndClearCommands<Func<PowerShell, object>>();
if (output.Count() == 1)
{
externalInvoker = new ExternalInvoker(output[0], this);
externalInvoker = new ExternalInvoker(output[0]);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ public Hashtable InvokeFunction(
Logger.Log(isUserOnlyLog: true, LogLevel.Error, GetFunctionExceptionMessage(e));
throw;
}
catch (OrchestrationFailureException e)
catch (Exception e)
{
if (e.InnerException is IContainsErrorRecord inner)
if (e.Data.Contains(OrchestrationInvoker.isOrchestrationFailureKey) && e.InnerException is IContainsErrorRecord inner)
{
Logger.Log(isUserOnlyLog: true, LogLevel.Error, GetFunctionExceptionMessage(inner));
}
Expand Down
1 change: 0 additions & 1 deletion test/Unit/Durable/DurableControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public void TryGetInputBindingParameterValue_RetrievesOrchestrationContextParame
{
CreateParameterBinding(_contextParameterName, _orchestrationContext)
};

_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalInvoker>.IsAny))
Expand Down