From 3bd961c61f03af126c18bc4ba42309c64fb5299b Mon Sep 17 00:00:00 2001 From: David Justo Date: Wed, 9 Mar 2022 08:17:21 -0800 Subject: [PATCH] Return results from Start-DurableExternalEventListener (#685) (#753) Co-authored-by: Greg Roll --- src/Durable/Commands/GetDurableTaskResult.cs | 36 +++++++++++++++++++ src/Durable/DurableTaskHandler.cs | 17 +++++++++ src/Durable/Tasks/ActivityInvocationTask.cs | 6 ++-- src/Durable/Tasks/DurableTask.cs | 4 +-- src/Durable/Tasks/DurableTimerTask.cs | 4 +-- src/Durable/Tasks/ExternalEventTask.cs | 6 ++-- ...soft.Azure.Functions.PowerShellWorker.psd1 | 1 + ...soft.Azure.Functions.PowerShellWorker.psm1 | 16 +++++++-- .../Durable/ActivityInvocationTaskTests.cs | 4 +-- 9 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/Durable/Commands/GetDurableTaskResult.cs diff --git a/src/Durable/Commands/GetDurableTaskResult.cs b/src/Durable/Commands/GetDurableTaskResult.cs new file mode 100644 index 00000000..74f5493b --- /dev/null +++ b/src/Durable/Commands/GetDurableTaskResult.cs @@ -0,0 +1,36 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +#pragma warning disable 1591 // Missing XML comment for publicly visible type or member 'member' + +namespace Microsoft.Azure.Functions.PowerShellWorker.Durable.Commands +{ + using System.Collections; + using System.Management.Automation; + using Microsoft.Azure.Functions.PowerShellWorker.Durable.Tasks; + + [Cmdlet("Get", "DurableTaskResult")] + public class GetDurableTaskResultCommand : PSCmdlet + { + [Parameter(Mandatory = true)] + [ValidateNotNull] + public DurableTask[] Task { get; set; } + + private readonly DurableTaskHandler _durableTaskHandler = new DurableTaskHandler(); + + protected override void EndProcessing() + { + var privateData = (Hashtable)MyInvocation.MyCommand.Module.PrivateData; + var context = (OrchestrationContext)privateData[SetFunctionInvocationContextCommand.ContextKey]; + + _durableTaskHandler.GetTaskResult(Task, context, WriteObject); + } + + protected override void StopProcessing() + { + _durableTaskHandler.Stop(); + } + } +} diff --git a/src/Durable/DurableTaskHandler.cs b/src/Durable/DurableTaskHandler.cs index 3981b686..818a5b99 100644 --- a/src/Durable/DurableTaskHandler.cs +++ b/src/Durable/DurableTaskHandler.cs @@ -164,6 +164,7 @@ public void WaitAny( if (scheduledHistoryEvent != null) { scheduledHistoryEvent.IsProcessed = true; + scheduledHistoryEvent.IsPlayed = true; } if (completedHistoryEvent != null) @@ -179,6 +180,7 @@ public void WaitAny( } completedHistoryEvent.IsProcessed = true; + completedHistoryEvent.IsPlayed = true; } } @@ -195,6 +197,21 @@ public void WaitAny( } } + public void GetTaskResult( + IReadOnlyCollection tasksToQueryResultFor, + OrchestrationContext context, + Action output) + { + foreach (var task in tasksToQueryResultFor) { + var scheduledHistoryEvent = task.GetScheduledHistoryEvent(context, true); + var processedHistoryEvent = task.GetCompletedHistoryEvent(context, scheduledHistoryEvent, true); + if (processedHistoryEvent != null) + { + output(GetEventResult(processedHistoryEvent)); + } + } + } + public void Stop() { _waitForStop.Set(); diff --git a/src/Durable/Tasks/ActivityInvocationTask.cs b/src/Durable/Tasks/ActivityInvocationTask.cs index 87bf61c2..a0ba49b4 100644 --- a/src/Durable/Tasks/ActivityInvocationTask.cs +++ b/src/Durable/Tasks/ActivityInvocationTask.cs @@ -37,15 +37,15 @@ internal ActivityInvocationTask(string functionName, object functionInput) { } - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.TaskScheduled && e.Name == FunctionName && - !e.IsProcessed); + e.IsProcessed == processed); } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed) { return scheduledHistoryEvent == null ? null diff --git a/src/Durable/Tasks/DurableTask.cs b/src/Durable/Tasks/DurableTask.cs index af71f36d..228e769b 100644 --- a/src/Durable/Tasks/DurableTask.cs +++ b/src/Durable/Tasks/DurableTask.cs @@ -11,9 +11,9 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Durable.Tasks public abstract class DurableTask { - internal abstract HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context); + internal abstract HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed = false); - internal abstract HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent); + internal abstract HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed = false); internal abstract OrchestrationAction CreateOrchestrationAction(); } diff --git a/src/Durable/Tasks/DurableTimerTask.cs b/src/Durable/Tasks/DurableTimerTask.cs index 86a75abe..4bb357ef 100644 --- a/src/Durable/Tasks/DurableTimerTask.cs +++ b/src/Durable/Tasks/DurableTimerTask.cs @@ -28,7 +28,7 @@ internal DurableTimerTask( Action = new CreateDurableTimerAction(FireAt); } - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.TimerCreated && @@ -36,7 +36,7 @@ internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext con !e.IsProcessed); } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed) { return scheduledHistoryEvent == null ? null diff --git a/src/Durable/Tasks/ExternalEventTask.cs b/src/Durable/Tasks/ExternalEventTask.cs index 1bd1fc58..5d12ad66 100644 --- a/src/Durable/Tasks/ExternalEventTask.cs +++ b/src/Durable/Tasks/ExternalEventTask.cs @@ -21,17 +21,17 @@ public ExternalEventTask(string externalEventName) } // There is no corresponding history event for an expected external event - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return null; } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent taskScheduled) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent taskScheduled, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.EventRaised && e.Name == ExternalEventName && - !e.IsProcessed); + e.IsPlayed == processed); } internal override OrchestrationAction CreateOrchestrationAction() diff --git a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 index 7ae51c2b..7bed18b9 100644 --- a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 +++ b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 @@ -59,6 +59,7 @@ FunctionsToExport = @( # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @( 'Get-OutputBinding', + 'Get-DurableTaskResult' 'Invoke-DurableActivity', 'Push-OutputBinding', 'Set-DurableCustomStatus', diff --git a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 index 1d4d00ad..0fc66666 100644 --- a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 +++ b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 @@ -235,6 +235,8 @@ function New-DurableOrchestrationCheckStatusResponse { The TaskHubName of the orchestration instance that will handle the external event. .PARAMETER ConnectionName The name of the connection string associated with TaskHubName +.PARAMETER AppCode + The Azure Functions system key #> function Send-DurableExternalEvent { [CmdletBinding()] @@ -263,12 +265,16 @@ function Send-DurableExternalEvent { [Parameter( ValueFromPipelineByPropertyName=$true)] - [string] $ConnectionName + [string] $ConnectionName, + + [Parameter( + ValueFromPipelineByPropertyName=$true)] + [string] $AppCode ) $DurableClient = GetDurableClientFromModulePrivateData - $RequestUrl = GetRaiseEventUrl -DurableClient $DurableClient -InstanceId $InstanceId -EventName $EventName -TaskHubName $TaskHubName -ConnectionName $ConnectionName + $RequestUrl = GetRaiseEventUrl -DurableClient $DurableClient -InstanceId $InstanceId -EventName $EventName -TaskHubName $TaskHubName -ConnectionName $ConnectionName -AppCode $AppCode $Body = $EventData | ConvertTo-Json -Compress @@ -280,7 +286,8 @@ function GetRaiseEventUrl( [string] $InstanceId, [string] $EventName, [string] $TaskHubName, - [string] $ConnectionName) { + [string] $ConnectionName, + [string] $AppCode) { $RequestUrl = $DurableClient.BaseUrl + "/instances/$InstanceId/raiseEvent/$EventName" @@ -291,6 +298,9 @@ function GetRaiseEventUrl( if ($null -eq $ConnectionName) { $query += "connection=$ConnectionName" } + if ($null -eq $AppCode) { + $query += "code=$AppCode" + } if ($query.Count -gt 0) { $RequestUrl += "?" + [string]::Join("&", $query) } diff --git a/test/Unit/Durable/ActivityInvocationTaskTests.cs b/test/Unit/Durable/ActivityInvocationTaskTests.cs index c96cc755..530711ca 100644 --- a/test/Unit/Durable/ActivityInvocationTaskTests.cs +++ b/test/Unit/Durable/ActivityInvocationTaskTests.cs @@ -225,8 +225,8 @@ public void GetCompletedHistoryEvent_ReturnsTaskCompletedOrTaskFailed(bool succe var orchestrationContext = new OrchestrationContext { History = history }; var task = new ActivityInvocationTask(FunctionName, FunctionInput); - var scheduledEvent = task.GetScheduledHistoryEvent(orchestrationContext); - var completedEvent = task.GetCompletedHistoryEvent(orchestrationContext, scheduledEvent); + var scheduledEvent = task.GetScheduledHistoryEvent(orchestrationContext, false); + var completedEvent = task.GetCompletedHistoryEvent(orchestrationContext, scheduledEvent, false); Assert.Equal(scheduledEvent.EventId, completedEvent.TaskScheduledId); }